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?

    1. Am I doing something wrong with reading the voltage? (connections etc)
    2. Does it matter if the usb cable is connected? (doesn't matter for my problem though)


  • @robert-hh

    Oke, will make a test program.

    When USB is connected it indeed has a higher voltage :').
    That was another mistake/issue I had :).

    Thanks a lot, again!



  • @mmarkvoort Other sensors do not hurt. When USB is connected, the battery voltage is higher, since it will be charged at that time.
    And yes, if the voltage changes when you touch the connector, then you have a bad connector. By maybe that was just coincidence with the other fluctuations.



  • @robert-hh

    Aha, so it isn't me :)
    Thanks, I thaught I did something wrong.

    Does it matter if you have sensors connected to the LoPy?



  • @mmarkvoort The ADC of the esp32 is known to be very bad. If if you apply a perfectly good signal (stable, low impedance), the reading will fluctuate a lot. That's why in the sample code the average is used.
    A capacitor will only help if placed directly at P16. and it must be a low impedance capacitor (ceramic, short leads).



  • @robert-hh

    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?



  • @robert-hh

    Robert thank you very much for your help.
    I finally get it!



  • @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-hh

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



  • @robert-hh

    But you can't get the Voltage_at_battery through code, right?

    Thanks a lot by the way, you helped me a lot! :)



  • @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.



  • @robert-hh

    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.25

    This is good right??

    But how can I get the voltage of the battery? I can't because I dont know R right?



  • @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



  • @robert-hh

    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 V

    When 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.3944969177246

    When 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.1072750091552

    Which one is good? Is it even good? I am a bit lost what to do.



  • @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/



  • @robert-hh

    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 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?



  • 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!



  • You should be able to post more frequently now -

    Hmm ok let me give this a test!


Log in to reply
 

Pycom on Twitter