Micro:Bit analog read pin - random values

Hi there,

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.

1 Like

Hi Vitaly,

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:


site

2 Likes

Hi Liam
Isn’t Vitaly talking about an analog input. Pull up / down resistors won’t help much there surely.
Cheers Bob

2 Likes

Hi Bob,
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

1 Like

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 :slight_smile:

2 Likes

Hi Vitaly
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Ω.
Cheers Bob

4 Likes

Hi Bob,
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! :man_facepalming:
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)

3 Likes

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.
Cheers Bob

3 Likes

Yes, Micro:Bit’s 1023 would point to 3V, I believe.

Thanks again, very helpful!

3 Likes