Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: digitalWrite doesn't work after analogWrite on the same pin

  1. #1

    digitalWrite doesn't work after analogWrite on the same pin

    Unlike on vanilla arduino digitalWrite doesn't work after analogWrite, you cant set a pin to HIGH or LOW after using analogWrite(pin,value) on it.

  2. #2
    Found a workaround :

    unlike

    pinMode(pin, OUTPUT);
    analogWrite(pin,value);
    //stuff
    digitalWrite(pin,HIGH);
    this works as expected :
    pinMode(pin, OUTPUT);
    analogWrite(pin,value);
    //stuff
    pinMode(pin, OUTPUT);
    digitalWrite(pin,HIGH);

  3. #3
    Administrator Paul's Avatar
    Join Date
    Oct 2012
    Posts
    306
    Yes, analogWrite is putting the pin into a different mode, which digitalWrite can't use. I'll work on digitalWrite next week.

  4. #4
    Digging up an old thread.

    I have a sketch that needs to turn a pin full on (digitalWrite) then in another mode, it needs to be a PWM (analogWrite). It worked on an Arduino, but analogWrite happens later in the code, after it has already been digitalWritten and any PWM value in analogWrite results in full on (3v3).

    How do I mix these 2 modes? I do not understand the example snippet? Do I define the pin again every time I need to switch modes between analopgWrite and digitalWrite? Do I need to define the frequency or is there a default value similar to previous arduino?

    Keith

  5. #5
    I don't know if it wasn't fixed in a recent update, but the sketch I'm working on does exactly that (define the pin again every time I need to switch modes) to address this issue (which may or may not be corrected since). I never changed PWM frequency, but I know it can be done in the latest teensyduino releases.

  6. #6
    Yup, this is still a problem.

    Tested with Arduino IDE 1.0.5 and 1.6.1, on a Teensy 3.1, using Teensy Loader 1.21.

    Ok, it's probably a low-priority problem given that there's a bearable work-around, but I'm hoping it's helpful to flag such unexpected behaviour.

  7. #7
    This just got me. Is there any reason to redefine the pin to turn the output off? I was using digitalWrite(pin,LOW), which didn't work so I tried analogWrite(pin,0), which did. That seems fine but I just want to make sure there's no reason to do something different.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •