Always reading same battery voltage
-
Hello,
I have a LoPy (firmware version = 1.6.9.b1) with a lipo battery.
Now I want to read the voltage of my battery, so I can send a message when the battery is below x. The battery is a 3.7V 1200Mah battery.I tried to get the value by using this code, but I only get the same value everytime (5598.633 -> Mean of ADC readings (0-1400 mV)).
When I just try to read the adc value (like they say in the docs), it is also always the same value.When I use a voltage meter on pin 16 I get: 1.27V, which is good right?
- Am I doing something wrong with reading the voltage? (connections etc)
- Does it matter if the usb cable is connected? (doesn't matter for my problem though)
-
try use attenuator (i do not know why it is not documented)
apin=adc.channel(pin='P13', attn=3)do you use it like in post you link?
-
When using the code that was given in the forum post (which uses attenuator 1) + changing to what you said (so changing it to 3).
I get this:
Mean of ADC readings (0-1023) = 1509.5199584960937
Mean of ADC readings (0-1400 mV) = 2063.7967586517333
Variance of ADC readings = 27.4036669731140
10* * 6 * Variance/(Mean**2) of ADC readings = 12.0000004768372When I use the adc from docs.
I get:
1520 and the next read 1530.It does change something, but still doesn't look good to me.
Does the USB cable matter?
-
Hi @mmarkvoort,
Are you trying to measure the voltage while you have the board powered over USB, i.e. using the REPL to check?
-
Sorry, can only post every 600s.
I am using pymakr (I know it isn't a good idea). What other options do I have to check?
But even when writing the value to a text file, without the usb, it is giving me the same value.
-
You should be able to post more frequently now -
Hmm ok let me give this a test!
-
Hey @livius, the attenuator wasn't previously documented because of on-going changes from Espressif regarding the ADC. We'll make sure it gets added shortly!
-
@mmarkvoort The ADC range was set a few weeks ago to 12 bit. So in the sample code, you have to divide by 4096 instead of 1024, which yields 1400. You should see the ADC reading of 4096, which means, the ADC input is overloaded. Are you sure, that you set the attenuation right?
These should be the right code:print("Mean of ADC readings (0-4096) = %15.13f" % meanADC) print("Mean of ADC readings (0-1400 mV) = %15.13f" % (meanADC*1400/4096))Also, the 1400 is just a guess for the point of the full range. Using a good Voltage meter, you should adjust this value.
Update: With battery I can read reasonable values, w/p battery I get 4095 in most cases. Do you have a battery connected? -
Thanks for the reply.
But I can't really understand what is going on. When I have the USB connected and the battery with attenuation on 1. I am always reading 4095. When I have the USB connected and the battery with attenuation on 3. I am reading 1503, 1508, 1533 etc.
What does these values mean? How is it related to the battery?
What does attenuation even mean? How can I determine the value of that?When reading the value with the multimeter it is saying 3.90V. Which I totally understand.
Is this correct?
100.00% 4.2
90 4.13
80 4.06
70 3.99
60 3.92
50 3.85
40 3.78
30 3.71
20 3.64
10 3.57
0.00% 3.5 -
@mmarkvoort I'm wondering whether you use the expansion board or not.
The basic range of the adc is 0..1V. With 3 db attenuation it is:
attn 1 = 3db: 10**(3/20) * 1 -> 0.. 1.41
attn 2 = 6db: 10**(6/20) * 1 -> 0.. 1.995
attn 3 = 11 db: 10**(11/20) * 1 -> 0..3.548
The expansion board has a voltage divider between battery and P16, consisting of a 56k/100k resistor, reducing the battery voltage by 56/(56+100) = 0.359.
So the 3.9 V of the battery are seen at P16 as 1.4 V. At attn 3 that should give a reading of 1.4/3.548 * 4096 = 1616. Since your value are different, I assume that the voltage divider has a different set of resistors. You could take the voltage directly at P16 to verify, and then you know the compensation factor.
Update, since my board's values differ from the documentation.
The capacity of the battery does not follow linear to the voltage. It's a kind of lying ' S' kind shape. http://www.thedroneinfo.com/2015/05/13/how-to-care-and-feeding-of-your-lipo-battery/ -
Thanks again for your repley.
I have a LoPy and a Expansion board.When I measure on pin 16:
With only USB I get: 1.4 V
With USB and Battery I get: 1.3 V
With only Battery I get: 1.2 VWhen I measure battery directly I get: 3.79 V
When I use attn 3 and the code from the post I get:
Mean of ADC readings (0-4096) = 1581.0400009155273
Mean of ADC readings (0-1400 mV) = 540.3944969177246When I use attn3 and the code from the post with (meanADC * 3548/4096) instead of (meanADC*1400/4096) I get:
Mean of ADC readings = 1401.6300201416015
Mean of ADC readings = 1214.1072750091552Which one is good? Is it even good? I am a bit lost what to do.
-
@mmarkvoort There are two factors top be considered.
a) the resistor divider. You said that with bettery only you have 1.2 V at P16. Could you measure the battery directly in that mode, connected to lopy and running. Then this ratio, Vp16/Vbat = R is known, and the battery voltage can for attn = 3 be calculated as:Vmeas = (ADCmean /4096) * 3.548 /R
Example:
ADCmean = 1401
R = 1.2/3.79 = 0.316
Vmeas = (1581/4096) * 3.548 / 0.316 = 3.804 -
Wow wait a minute...
I used 3548 instead of 3.548 in my code :')
Now when I read the val of pin 16 I get 1439.
So this means it is 1439*3.548/4096 = 1.246477
Measure pin 16 directly with multimeter gives me: 1.25This is good right??
But how can I get the voltage of the battery? I can't because I dont know R right?
-
@mmarkvoort R is the ratio:
Voltage_at_P16 / Voltage_at_battery
According to the schematics, it is 0.3275, which gives ~3.8 V = 1.246/ 0..3275 at the battery, but better check. -
But you can't get the Voltage_at_battery through code, right?
Thanks a lot by the way, you helped me a lot! :)
-
@mmarkvoort Yes, you can. Since R is a fixed value, you can get determine the Voltage at P16 by code and calculate the battery voltage as:
Voltage_at_P16/R -
But doesn't the value of R changes everytime?
You said R = Voltage_at_P16 / Voltage_at_battery, but how can you get Voltage_at_battery in the code.
Or in other words, how did you get the 0.3275 as a value?
EDIT: 0.3275 seems to be a good value indeed.
-
@mmarkvoort No, R is defined by the circuitry of the expansion board, and is fixed. The schematics show a voltage divider consisting of two resistors, one 56k, the other 115k, and so R can be calculated as:
R = 56 / (56 + 115)
If you want to find R by measuring, you have to take a Voltmeter and measure the real values at the battery and at P16, while the device is running from battery.
-
Robert thank you very much for your help.
I finally get it! -
Just some questions.
Is it a good idea to put a capacitor between battery and LoPy to reduce noise?
Now when I am reading the battery value it really fluctuates.Also sometimes when connecting the battery it is always reading low value. When move the connector a bit it is changing. What can this be? Is it the startup that needs a bit more?
And lastly, does it make a difference if I have a sensor connected?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login