Strange behaviour in reading analog input

Hello everyone,

I’m new to Arduino, but not new in programming, and I came to you to for and help.
As you can see from the files attached, I’m working on a very simple project.
What’s happening is that I’m not able to read any value from analog Input port A0 (or any other AI port); to be more specific, if I chose to use AI-0 port, the system monitor shows me zero on AI-0, Vout and R2, but if i disconnect the cable to read the AI port something happen (see pictures example and example_2).

This behaviour occurs for any AI port I decide to use!

There is obviously something I cannot see here!

Thans in advanced for your help.

Vincenzo

03_04_Photoresistor Circuit.pdf (1020 KB)

EXAMPLE_03_04.ino (625 Bytes)

I bread boarded your circuit and loaded your code to my Uno. Below are the results copied from serial monitor.

Vout = 1.47 [V]; R2 = 1.96[kOhm] adcValue = 301
Vout = 1.46 [V]; R2 = 1.93[kOhm] adcValue = 298
Vout = 4.14 [V]; R2 = 22.77[kOhm] adcValue = 848
Vout = 4.26 [V]; R2 = 26.93[kOhm] adcValue = 871
Vout = 4.32 [V]; R2 = 29.89[kOhm] adcValue = 884
Vout = 1.53 [V]; R2 = 2.08[kOhm] adcValue = 314

So. it works for me. Please post a clear photo of your wiring.

but if i disconnect the cable to read the AI port something happen (see pictures example and example_2).

It may be helpful if you tell us what "something happen" means. An input that has noting connected is "floating" and the output is undefined (could be any voltage between ground and Vcc).

edit: It is better if you post images inline. Instructions to upload images.
Please read the "how to use this forum-please read" stickies to see how to format and post code.

My breadboard setup is also working!

Hi groundFungus,

you are right, my explanation was a bit vague.
What I meant was that when I run the code, all the output are zero (I expect at least some fluctuation given by some electric disturb), but when I disconnect the wiring for the AI0, I can see the value for Vout, R2 and adcValue fluctuating: actually that's what I do not understand, if I do not have input to AI0, I should not have any consistent value for Vout and R2 (I'm not considering electric disturb)!
Given the code, if the signal form AI0 is zero, Vout and R2 must be zero.


I uploaded other script on the board, and it looks working fine.

what I do not understand, if I do not have input to AI0, I should not have any consistent value for Vout and R2 (I'm not considering electric disturb)!

Actually no, you can get disturbances but you don’t have to get them it depends on the EM environment you have.
With a wire to ground on the analogue input you effectively discharge the sample and hold capacitor on the A/D input. So if your code reads this you will get a cleared out input which is much more difficult for the interference to affect.

Hi Mike,

I'm sorry but I'm not sure I got what you said.
Can you make please an example?
Actually I wrote the same code and made the same wirings as per my first post, but actually I can't see any reason why I doesn't work.

Vincenzo

I'm sorry but I'm not sure I got what you said

It was the reason why you observed what you did.

Can you make please an example?

What how can I make an example of a reason?