analogRead() behaviour when pinMode is first set on INPUT; Reading "noise".

Hi,

I cannot figure out a behaviour about the analog pins.
I've been reading the ATmega 328p datasheet and got a lot from it but I think I've reached my limits with this one.

I didn't copy paste my code, I don't think it's relevant, cause this might be a ATmega behaviour that I don't understand not a technical problem.

If I set ;

pinMode(A0, INPUT);

Then read the result;

analogRead(A0);

While I don't input value in the pin, it will read what seems to be random values (Maybe noise ? Idk clarifying that might help too).
BUT
If I set it to pinMode(A0, output), THEN analogRead(A0), as long as I don't input a signal, it will read 0. (Which for my purpose is really convenient).

I'm writing a big code, and this behaviour could solve a lot of problem, but I want to make sure I understand it so it doesn't cause troubles that I won't be able to diagnosticate in the future.

Extra informations ;

  • For this project, I'm using a Elegoo Uno R3 that's new and appears to be working fine (But hey what do I know i'm on the arduino forum)

  • If this behaviour isn't common knowledge and you find it rather particuliar, then know that my code on the chip is rather complex and might be the cause of that, so i'll archive this post and do a more code oriented one.

  • for the simplicity of illustrating my point, I didn't mentionned that i'm doing port manipulation to enable the Ourput of the pin A0, but the behaviour is the same with the code i've written in my explanation.

  • And finally, this is my first post on the arduino forum, I hope everything's right about, pardon my lack of clarity if there is, and don't hesitate to correct my mistakes if i've done some.

G'day !

Known issue - pin is floating, so picking up EMF

missdrew:
Known issue - pin is floating, so picking up EMF

NICE.
Thank you so much.
You put a name on the issue and now I found useful documentation about it.

BTW if someone finds this post and wonders what missdrew meant, everything that needs explaination can be found on this existing post ;

https://forum.arduino.cc/index.php?topic=462315.0

Cheers.

If I set it to pinMode(A0, output), THEN analogRead(A0), as long as I don't input a signal, it will read 0.

No real surprise, as the pinMode() command is only relevant to digital pins, so you are reading a digital pin with analogRead()

Have you tried using analogRead() on a purely digital pin ?

I'm writing a big code, and this behaviour could solve a lot of problem

What problem are you trying to solve ? It sounds like you are trying to use analogRead() as a sort of fake digitalRead() on an analogue pin. If so, then please be aware that pins A0 to A5 on a 328 chip can be used as purely digital pins with the normal digital commands

What problem are you trying to solve ? It sounds like you are trying to use analogRead() as a sort of fake digitalRead() on an analogue pin. If so, then please be aware that pins A0 to A5 on a 328 chip can be used as purely digital pins with the normal digital commands

I have absolutely no problems in fact, I juste needed the informations given by the first answer to my post, but to explain how I encountered this behaviour;
Basically, I have a piece of software running on my computer that is a UI, generating an .ino file and sending it to the arduino.
And for the analog pins, it takes what the user asked to have (input or output) for each pins as a byte, and write it on the arduino like this DDRC = 0b00000001;
I did it that way cause I thought you had to precise the pinMode for analog pins, and also as I don't know beforehand how many pins the user would like to activate that saves a lot of lines of in the file.

And I noticed that by set beforehand the pinMode to INPUT, there was a random value when no voltage was send to the pin, and when it's set to OUTPUT, the value was 0 until you send a voltage to the pin.
Which was great for me, as I don't want the user to receive signals from pin he didn't specifically asked to be an input, an 0s can be filtered out easily.

So in the end, from my understanding, setting the pinmode for an analog pin isn't necesseray, but setting it to output actually turn on the pull-up resistor that would prevent the received value to be corrupted with EMF.

I actually had no problem other than not understanding how this would work, thank you both for your attention and informations :slight_smile: .

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.