Reading external voltage (ADC on an STM32F103) does not work. Help needed.

Hello!

I'm setting up a small project where I need to read a voltage (between 0 and 1.56V) and display it on a small OLED screen.
Everything's fine if I want to measure the STM32F103C8T6 3.3V power supply. The ADC (PA0 pin) is perfectly working. The PA0 pin is connected to a 47k resistor which itself is connected to GND.

But what I want is to measure an external voltage. So I took a small AAA battery to test its voltage and see if it would be properly displayed on the OLED screen.
The Battery positive side is connected to PA0 (and The PA0 pin is connected to a 47k resistor which itself is connected to GND) and the Battery negative side is connected to GND.
But the ADC does not detect any voltage in this case.... :frowning:

Any idea what is wrong ? I must be missing something which is very obvious but I don't see what exactly ...

Thanks!

Stéphane

Did you check the actual voltage on the PA0 pin with a multimeter while doing this? Perhaps there's a
short to ground on that pin.

Please draw the circuit, It sounds like you are using 1 resistor.

missdrew:
Please draw the circuit, It sounds like you are using 1 resistor.

I'm indeed using one resistor. This is working fine (with one resistor) if I connect the PA0 pin to 3.3V pin (as a test). In this case the ADC reports value as 4096 (12 bits).
If I replace the 3.3V pin with the AAA battery + side, I don't get anything.
And if I disconnect the resistor, I get random values, whatever I connect to the PA0 pin, 3.3V or battery ...
Really weird.

Stéphane

Still waiting for a drawing...

aarg:
Still waiting for a drawing...

Sorry, I don't have any tool here for that and given the simplicity of my circuit I don't feel the need to draw a schematic.
There's nothing more than the STM32, the connections to the OLED screen (using i2c), and the PA0 pin connected to the Battery "+" side and the GND pin connected to the Battery "-" side.
==> This gives random values.

and If I add a 47k resistor from PA0 to GND, ADC gives me correct numbers if I wire the PA0 pin to the 3.3V pin.
But the ADC pin will always gives 0 if I connect the "AAA" battery ...

Stéphane

Well, "given the simplicity" of your circuit, maybe you don't need any help with it. Or at least, I "don't feel the need" to help you with it. Are you saying you can't make a quick pen and paper drawing and take a cell phone picture of it?

Or perhaps given the small mystery here, images of the actual board and battery connection would be better. My reasoning is that we know that the ADC works on STM32 boards. I've certainly used it.

Not to be too obvious, but have you tested the battery with a meter? Did you test the input with a connection to Vdd again to make sure you didn't damage the input with the battery? It's not cool to connect a battery directly to a CPU input.

connect the AAA Battery holder + to the ADC, and the - to GND, see what the reading is, do this initially without the resistor in series.

I am assuming you are using a holder and not just 'holding and hoping' the connection.

Again, I strongly advise against connecting any battery directly to an Arduino I/O pin.

I have no problem reading external voltages using an STM32, so either you have damaged the input, you have connected it incorrectly or the code is incorrect. There is not enough information in your post to guess which mistake you might have made.

I add a 47k resistor from PA0 to GND

What is your theory about the purpose of this resistor?

Connecting a battery directly to a pin is a sure way to kill an un-powered processor.
Leo..

missdrew:
connect the AAA Battery holder + to the ADC, and the - to GND, see what the reading is, do this initially without the resistor in series.

I am assuming you are using a holder and not just 'holding and hoping' the connection.

I used a proper battery holder and the results are now correct. It was indeed the reason why I didn't have expected results.
Thanks :slight_smile:

Wawa:
Connecting a battery directly to a pin is a sure way to kill an un-powered processor.
Leo..

So what do you suggest, in this case ?
PS: I was able to have correct results now (i tested using a battery holder).
My final design will have to monitor voltage taken from another board (a chinese power supply), by connecting 2 of the the power supply potentiometer pins (used to limit current) to the STM32 ADC and GND pins. So I want to make sure it won't damage the STM32.

So what do you suggest, in this case ?

Avoid connecting a voltage source to a pin on an unpowered processor.

If it is impossible to avoid that situation, have a 10K (or larger) resistor in series between the source and the pin.

jremington:
Avoid connecting a voltage source to a pin on an unpowered processor.

If it is impossible to avoid that situation, have a 10K (or larger) resistor in series between the source and the pin.

Yes that's indeed what I should do.
And a friend of mine suggested to use 2 protection diodes as well.
Thanks.