If I set an analog pin to OUTPUT the corresponding digital pin always reads 0

I have an UNO setup with nothing connected to any of the pins.

I was investigating the noise I expect to see on the pins if I read from them in an unconnected state.

I found something odd - if I set an analog pin to OUTPUT then its corresponding digital pin will only ever return 0 if I read from it.

E.g. if I set A0 to OUTPUT then reading from digital pin 0 will only ever return 0 (none of the other digital pins are affected). If I set A0 to INPUT then digital pin 0 will return the noisy values I was expecting.

What do I mean by an analog pin's corresponding digital pin?

Well A0 corresponds to digital pin 0, A1 to 1, ..., A5 to 5, A0 to 6, A1 to 7, ..., A5 to 11, A0 to 12 and A1 to 13.

There is no inverse relationship, i.e. if I set a digital pin to OUTPUT this will not affect whether I can read noisy values from its corresponding analog pin.

Also I note that when all other pins are set to INPUT I can read noise from any given digital pin whether its mode is set to INPUT or OUTPUT, but for any given analog pin it must be set to INPUT otherwise reading from it will only ever return 0.

This is all important as many people suggest seeding the standard Arduino random number generator with the noise read from pin 0.

This works OK but only as long as A0 is not set to OUTPUT.

Note: even then it doesn't actually work all that well as reading from pin 0 returns a very small range of values. If you're seeding the random number generator this way each time the Arduino is reset then it won't take very many resets before the same seed value is seen again. According to my numbers it should take on average only about 9 resets before you see one of the seed values again, i.e. you're getting around 6 bits of entropy.

I've attached a sketch that demonstrates this, it outputs lines like this to serial:

``````1 8 = OUTPUT 13 = OUTPUT
``````

Not super clear I admit - the second number (8 here) is the pin I'm trying to read noise from, the first digit (1 here) is either 1 or 0 depending on whether the pin was noisy (as opposed to always returning 0) and the final number (13 here) is the pin that I've set to OUTPUT (all unmentioned pins will have been set to INPUT). Every pin is tested as a read pin against every other pin being set to OUTPUT - from this we can see the behavior described above.

Before outputting lines like these it outputs a set of simpler lines to show if I can detect noise on each pin when set to INPUT and then OUTPUT mode when all other pins are set to INPUT.

NoiseTest.cpp (2.22 KB)

I was investigating the noise I expect to see on the pins if I read from them in an unconnected state.
I found something odd - if I set an analog pin to OUTPUT then its corresponding digital pin will only ever return 0 if I read from it.

If you set pin to OUTPUT, is non-sense to read. Always 0.
For noise on read you need to set in INPUT mode.

I think you've misunderstood me.

I am talking about the mode of an analog pin affecting the state of a digital pin.

E.g. if digital pin 0 is INPUT then the mode of no other pin should affect the values I read from it.

However if I set analog pin A0, i.e. pin 14, to OUTPUT then this will cause analogRead(0) to always return 0.

I.e. even if pin 0 is set to INPUT the fact that pin 14 (a seemingly unrelated analog pin) is set to OUTPUT makes pin 0 unusable as a source of noise.

I.e. even if pin 0 is set to INPUT the fact that pin 14 (a seemingly unrelated analog pin)

It isn't an unrelated pin. Analog pin 0 and digital pin 14 are the same physical pin, on the 328-based Arduinos.

ghawkins:
This is all important as many people suggest seeding the standard Arduino random number generator with the noise read from pin 0.

Using analogRead to seed the random number generator is a bad idea.

I think you can't use analogRead() on digital pin , so 0-5 are equal to A0-A5 for this function.
Different for digitalRead(). Number 0-5 are digital pin and A0-A5 is needed to read digitally from analog pin.

I'm waiting correction or an okay

On note section:
The analog input pins can be used as digital pins, referred to as A0, A1, etc.

ghawkins:
IWhat do I mean by an analog pin's corresponding digital pin?
Well A0 corresponds to digital pin 0, A1 to 1, ..., A5 to 5, A0 to 6, A1 to 7, ..., A5 to 11, A0 to 12 and A1 to 13.

Not for me: see ARDUINO UNO PINOUT:
http://forum.arduino.cc/index.php?topic=151646.msg1138645#msg1138645
On pink color pin numbers used for function digitalRead() and digitalWrite()
A0-A5 can be indicated also as 14-19 but are not physical pins, A0 is PC0 and digital pin 0 is PD0

Number 0-5 are digital pin and A0-A5 is needed to read digitally from analog pin.

Close. A0...Ax and so on are aliases for 0...x when used with analogRead, but if you want to read or write one of the analogue pins as a digital pin, you use A0...Ax, you obviously can't use 0...x

OK - I'm still a bit confused.

Are you saying that values 0 to 19 relate to different physical pins depending on whether I'm calling digitalRead() or analogRead()?

So if I'm using digitalRead() then all the values 0 to 19 make sense.

0 to 13 map to the row of digital pins I see on one side of my Arduino board.

14 to 19 map to the row of analog pins I see on the other side of the board.

But if I'm using analogRead() only values 0 to 5 make sense.

And here 0 (rather than 14) maps to the first of the analog pins on one side of the board.

A0 to A5 are defined to map to the values 14 to 19 in pins_arduino.h.

So I should only use A0 etc. if calling digitalRead() and I want to use the analog pins as if they were just additional digital pins?

If that's what you're saying that makes sense - it's just a little confusing that the same number, e.g. 3, maps to a different physical pin depending on whether you're using analogRead() or digitalRead().

It could be more clearly stated that A0 etc. are solely for use with the digital functions and absolutely not for use with the analog functions.

Anyway if that's the deal then thanks for getting me to realize it

A0 through A5 are #defined as 14 through 20. If analogRead() sees a number greater than 13, it subtracts 14 from it to make it 0 through 5. This is why both analogRead(0) and analogRead(A0) are the same.

Ignore the physical pin numbers on the '328 when talking about arduino, it just gets confusing.

There's a bunch of things in Arduino that are there to make things "just work" that can be confusing if you look halfway down -- if you just stay at the surface and do things that work, you're fine, and if you understand the libraries through and through it's fine, but if you start looking at some things and not others it can be confusing.

If you configure a pin (analog or digital) as an output then the result of digitalRead() for that pin will be the state that is being output. I would expect that to be LOW unless you have previously written HIGH to that pin.