Just trying my first steps with hobby electronics, sorry if it’s a very basic question.
I was doing the “battery tester” tutorial on MakeCode Micro:bit web site, which is based on connecting GND and Pin0 to “-” and “+” on an AA battery, respectively, and reading the analog value of Pin0.
I was super surprised to see that the readings vary a lot. I disconnected the clips from the battery and reading from Pin0 still returns random values in the “400…800” range, when there is no signal.
Am I missing something, or should not it be 0 when the pin is not connected? Tried pins 0, 1, 2 to the same effect.
It sounds like the pin is ‘floating’ that means it isnt tied to any voltage and will behave like that.
Take a look at the message below and the Arduino forum before designing a circuit with a pull up/down resistor.
To get it to default at a particular state you can use a resistor to pull the pin up(to the maximum voltage the pin can read, but dont go higher!) or pull it down(to the micro:bits or another microcontrollers ground).
Here’s a good image that goes shows the circuit:
Isn’t Vitaly talking about an analog input. Pull up / down resistors won’t help much there surely.
Yeah good catch, the first bit about a pin floating is right but I’ll make sure I’m not spreading mis-info. Back to the books!
EDIT: it looks like it should be all good, but should be considered on a case by case basis. There was an excellent discussion over on the Arduino forums that go into detail: stopping floating analog pins - General Electronics - Arduino Forum
Thank you Liam,
Good point about the “floating pin” - This makes total sense!
For example, just connecting Pin0 to GND (so there is no current whatsoever) produces a stable reading of 0.
I am still learning, so thanks for your patient explanations
That’s just what should happen. Also when connected to the reference voltage it should read 1023 or whatever the maximum is for those particular ADCs. I am not familiar with Microbit so I don’t know how many bits the ADCs are.
With the battery how much variation is there? It could be the battery changing a little bit or slightly changing connection. If the ADC is 12 bit or so it could be quite sensitive. The analog input should be very high impedance probably 2 to 10 TΩ.
Thank you for the explanation.
Micro:bit is documented to return 0…1023 from analog inputs, so I suppose it’s 10 bits?
I think I’ve figured the issue.
Would you believe that… The battery under test was dead!
So it probably acted as a perfect resistor and introduced that “floating pin” problem.
Tried with a fresh battery, and analog reading returned 490 (which maps to 1.43V, I think)
Yes, I would believe that. You would not be the first, That’s why I suggested the battery may not be OK. Would be acting like an antenna on the high impedance input.
490 should be just short of 2.4V if your reference is 5V or 1.58V if the reference is 3.3V so your reference is not one of these. Your reference would appear to be 3V. To measure any voltage using the ADC you need to know what your reference is.
Yes, Micro:Bit’s 1023 would point to 3V, I believe.
Thanks again, very helpful!