0

I'm new to Arduino Uno, When I run this piece of code, I get numbers 340 to 370.

This is my question, why it returns 340 to 370 which correspond to 1.66 to 1.80 actual voltage? since I don't have any input equipment plugged into these pins so it must return zero voltage if i'm right in other words why default values in these pins are in this range of numbers ?

void loop() {
int val = analogRead(A0) ; //A1, A2 ...
Serial.println(val) ;

}
2

You will get varying values on each read. A pin’s input is said to be floating if nothing is connected, which in simple terms can be explained as it just acting as an antenna for whatever electric noise is present in your environment. Its state is unknown, and utterly meaningless. If you hold a spinning electric motor close to the pin, you can see the values rise and fall more clearly.

Use a high value (10K or more) pull-down or pull-up resistor to partially mitigate this jitter. A pull-down would bring the read value down to around 0, and a pull-up would bring it up to around 1023.

Furthermore, it’s a good idea to always smoothen out analogRead (5-10 reads, ~25-50ms apart, average the values out) to get a good reading if precision is important. This is especially valid if you use longer wires, have an unshielded case, or use a switched mode power supply. Twisting the wires (GND and wiper IIRC, otherwise correct me) to a potentiometer helps.

Edit: You can use pinMode( A0, INPUT_PULLUP ); - it provides a 20-50Kohm pull-up, though I don’t trust this and always use 1/4W resistors instead. It saves space though, and is quite handy.

5
  • Thanks for your clear explanation, however as I said I'm new to electronic fields, and I have another question, what does happen when I use a 1 mega ohm resistors. On my case I wanna use knock sensor and I found it in every tutorial to use a 1 m ohm resistor to work properly. but I use 6k ohm resistor and that does not work correctly. and signals are falling up and down accidently and when I plug my 6k resistor to the ground and A0 its voltage goes to 0 May 17 '18 at 8:13
  • @shahingh Get a 1Mohm resistor
    – user2497
    May 17 '18 at 9:46
  • 1
    @shahingh ... or connect 167 6Kohm resistors in series:/
    – user2497
    May 17 '18 at 9:48
  • I think I got : ) May 17 '18 at 12:01
  • Please note that INPUT_PULLDOWN is not available on AVR-based boards. INPUT_PULLUP works both on AVR and SAMD boards. May 17 '18 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.