View Full Version : digitalWrite doesn't work after analogWrite on the same pin
pixelk
10-25-2012, 07:10 PM
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.
pixelk
10-25-2012, 07:15 PM
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);
Yes, analogWrite is putting the pin into a different mode, which digitalWrite can't use. I'll work on digitalWrite next week.
keithg
01-18-2013, 03:34 AM
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
pixelk
01-18-2013, 11:37 AM
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.
happyinmotion
05-29-2015, 11:23 PM
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.
jwmelvin
06-19-2015, 04:01 PM
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.
paynterf
12-14-2019, 06:44 PM
Yes, analogWrite is putting the pin into a different mode, which digitalWrite can't use. I'll work on digitalWrite next week.
This still seems to be a problem, with the latest Teensyduino installation.
Frank
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.