6 years, 1 month ago.

AnalogIn values wrong

Hi there!

I'm trying to measure the voltage of my battery pack (which is a LiPo) for safety reasons. I'm using Analog pin A6 on the Nucleo F303K8 (A7 crashed the board). When I try to read the voltage, the voltage is significantly lower than the voltage I read using a multimeter (and the voltage the charger of the LiPo says). I'm using a simple voltage divider with R1 = 470k and R2 = 220k to scale the LiPo's voltage from 8.4 max to 2.7V max (to give my pins some headroom. The reference voltage is 3.3V. Both the multimeter and the charger say it should read 2.33V at the measuring point, but the analog in says it's 1.33V. Any idea what this could be?

sample code

#include <mbed.h>

Ticker radioTicker;
AnalogIn throttlePin(A1), rollPin(A3), pitchPin(A2), yawPin(A0), battery(A6);

float throttle, roll, pitch, yaw, battery;

void radioLoop(void)
{
   // Read all analog inputs
   throttle = throttlePin.read();
   roll = rollPin.read();
   pitch = pitchPin.read();
   yaw = yawPin.read();
   battery = batteryPin.read() * 3.3;
}

int main()
{
   radioTicker.attach(&radioLoop, 0.01);
}

A7 wouldn't work because that pin is also used for the virtual com port.

A6 should however work fine. Can you check the readings at 0V and 3.3V and also check the voltage on the reverence voltage pin (You can pick it up on SB10, L1 or C24).

posted by Andy A 12 Mar 2018

Thank you for your answer. When testing the board without the IIC and SPI connections active, I noticed that the board was displaying the correct values (3V3 was 100, 0V was 0, battery voltage was 2.35V). When I reenabled either the SPI or IIC connections however, the read value dropped. The multimeter did again confirm that the voltage on the pin itself did not show the drop. Any idea how this is possible and if it can be fixed?

posted by Sander Swart 12 Mar 2018

One possibility. there is usually a maximum recommended input impedance for any AD converter. When the AD channel samples you are pulling a bit of current into the microcontroller. If your resistor values are too large, this current changes the voltage on that node. Usually you want to see around, maybe 2kohm maximum in. Your resistor values are probably too high. Can improve this by adding a capacitor in parallel with low side resistor so the AD draws current from that. Or just try lower value resistors, keeping both values under, say 10kohms and see if that helps. A lot of times people will install an opamp between the resistor divider and the AD input pin - the opamp provides high input impedance and so leaves your resistor divider undisturbed and then provides low output impedance so can drive current into the AD pin when it takes a sample.

posted by Graham S. 12 Mar 2018

That was it indeed. I chose the high values for the resistors to minimize the current leak from the LiPo. A 10 nF cap did the job perfectly.

posted by Sander Swart 12 Mar 2018
Be the first to answer this question.