Pages: [1]   Go Down
Author Topic: What happens when I digitalRead() a pin that is in PWM output mode?  (Read 996 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there, I'm wondering what value would I get when I digitalRead() from a pin that is currently set to output in PWM mode (analogWrite)? Would I expect to read whatever value that is being driven by the PWM output, or would I get a fixed value?

Thanks much,
- K.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You will turn that output pin into an input pin, and your reading depends on what is connected to the pin.
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@liudr,
have you a reference to confirm that?

I was thinking it would return the last value written to it... so it can return either a 1 or a 0  just depends....
NB digitalRead() does not call inputMode(...) ?

Note: there is some blinking led code like:  digitalWrite(LED, !digitalRead(LED));

just wondering...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4243
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would tend to agree with Rob.  I certainly don't think that the data direction register would be changed (making the pin an input pin) since there are perfectly legitimate reasons to read from an output port and to write to an input port.

Don
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12737
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I vaguely recall that the PWM module has its own pin driver.  If that's true, then digitalRead returns the last value written to the pin before PWM was enabled.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17254
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You will turn that output pin into an input pin, and your reading depends on what is connected to the pin.

Not as I understand it.

Where a pin was last used with a PWM comman, a digital read of the pin without first performing a mode change statement will just read the last value written to the pin's data register. The voltage at the pin will remain to be the last value written by the pwm command, and the pin remains as a output pin because the pin's DDRx register bit was not changed, because of the lack of a mode change command prior to performing the digitRead command.

  
« Last Edit: June 25, 2011, 07:19:40 pm by retrolefty » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Where a pin was last used with a PWM comman[d], a digital read of the pin without first performing a mode change statement will just read the last value written to the pin's data register. The voltage at the pin will remain to be the last value written by the pwm command
So far so good.

And here's the thing:

The PWM timer mode and prescale values for all timers are set up in the Arduino init() function (in wiring.c).
A call to analogWrite() for a PWM pin sets the duty cycle and makes the PWM signal connection to that pin.  (See Footnote.)

The PWM signal itself is generated by the timer circuitry and does not go through the pin's output register.

If the value is zero the analogWrite() function writes a zero to the PWM pin's bit in that port's output register.  (The timer signal is disconnected from the pin at this point.)

If the value is 255, the analogWrite() function writes a one to the PWM pin's bit in that port's output register. (The timer signal is disconnected from the pin at this point.)

Otherwise, the analogWrite() function connects the timer signal to the PWM pin and does not write anything to that pin's bit in the output register.

Bottom line: The output register bit values for all I/O pins are initialized to zero.  If you have executed an analogWrite() function for a particular PWM pin and you have not subsequently executed a pinMode() function for that that pin to make it an input,  digitalRead() will read a value of zero for that pin unless and until you have executed either a digitalWrite() with a value of one to that pin or an analogWrite() value of 255.

Post-bottom-line note: The digitalRead() function disconnects the timer from the pin, so PWM is no longer applied to the output. It definitely does not automatically set the pin mode to input, and the result is that the pin's bit value in the output register is applied to the pin.  Subsequent analogWrite() function calls can be used to connect the timer signal to the PWM pin again.


Regards,

Dave


Footnote:

The analogWrite() function sets a pin to output mode.  If the pin is a PWM pin it applies a signal from a timer.  If you later perform a digitalWrite() to that pin, it disconnects the timer signal and makes the pin act as a normal output pin.  Of course you can call the pinMode() function if you want to use the pin as a digital input.  (After changing to input mode, executing digitalRead() for that pin turns off the timer.)

Finally, note that if you execute an analogWrite() function to a pin that is not one of the PWM pins, the function writes a zero if the byte value is less than 128, and writes a one if the byte value is greater than or equal to 128.
« Last Edit: June 26, 2011, 12:45:47 am by davekw7x » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Dave, as allways excellent explanation!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry. I thought I knew this but apparently I didn't. Thanks for correcting me.
Logged


Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post-bottom-line note: The digitalRead() function disconnects the timer from the pin, so PWM is no longer applied to the output. It definitely does not automatically set the pin mode to input, and the result is that the pin's bit value in the output register is applied to the pin.  Subsequent analogWrite() function calls can be used to connect the timer signal to the PWM pin again.

Finally, note that if you execute an analogWrite() function to a pin that is not one of the PWM pins, the function writes a zero if the byte value is less than 128, and writes a one if the byte value is greater than or equal to 128.

Wow, thanks everyone, especially Dave. Some very interesting tidbits!!

So if I am resding this correctly, it seems like if a pin is sending PWM output and an attempt to digitalRead() from that pin will cause the PWM output to stop. Does the pin output revert to the last value that was set for this output pin, or just back to 0? I'm assuming the former.

It's also interesting to see the behavior of analogWrite() to a non-PWM pin - definitely not what I expected!

Thanks and kudos,
- K.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's also interesting to see the behavior of analogWrite() to a non-PWM pin - definitely not what I expected!
What did you expect?
Logged

Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd expect it to output 1 if the write value is anything but zero, like the digitalWrite() behavior.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd expect it to output 1 if the write value is anything but zero, like the digitalWrite() behavior.
You could make it do that, if that's what you'd like.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: