New library to read Arduino VCC supply level without resistors for battery powered sensor nodes that do not use a voltage regulator but connect directly to the batteries ;-)


  • Contest Winner

    Thanks guys,
    I'll happily elaborate more on the details, but perhaps it is best if you state some specific questions regarding parts of it that are difficult to comprehend. Imo, working with sensors should not require a degree in engineering, so nobody should "feel stupid" for asking questions 🙂


  • Mod

    @Anticimex said:

    Imo, working with sensors should not require a degree in engineering, so nobody should "feel stupid" for asking questions 🙂

    When I have questions I will ask 🙂
    But, like @hek, I need to let things 'sink in'.
    And I really want to understand.
    To be honest I have learned a lot (and spent a lot) since I stumbled upon MySensors.
    Wonderful new hobby!


  • Hero Member

    As long as you connect VCC direct to the battery I do not understand why people opt for using an external voltage divider optionally with a FET to reduce standby current. Additional components and current consumption while only a possible small improvement in accuracy.

    For the voltage divider the ADC reading is 1023 x R2 / (R1 + R2) x Vcc

    For the 1.1 Volt reference the reading is 1023 x 1.1/Vcc

    If you are using +/- 10% resistors (or the FET resistance is not measured correct or varies), the accuracy is more or less matching the (uncalibrated) bandgap reference method.


  • Mod

    @daulagari I guess the discussion depends on whether you have a software-mindset or a hardware-mindset.
    The software guys currently seem to form a minority on this board...
    I still think, after all discussions and distractions, that this way of measuring can be very usefull, when you understand the limitations.


  • Contest Winner

    I have not studied what support for measuring Vcc is built into the Arduino, but if there are support for doing that, I am sure it should be adequate. Regarding the external circuitry, my point is just that if you are going for an external solution, you might just as well design it to consume a minimum amount of current, as that is a one-shot optimization. Yes, it is a couple of extra components, but to me, that outweigh the limitations of the simple voltage divider in the long run.
    Personally, I would even consider a battery management unit. Such a thing should be able to handle both charging (if you want that feature) as well as readback (with battery health compensation).


  • Hero Member

    @Yveaux

    I guess the discussion depends on whether you have a software-mindset or a hardware-mindset.

    Being on this forum you are likely not having a hardware- or software-mindset only 😉

    @Anticimex:

    I have not studied what support for measuring Vcc is built into the Arduino

    I think a study is not needed, the ADC ref power is VCC and there is a 1.1 V bandgap in the Arduino that you can measure; that's the whole trick.

    Yes, somewhat more funky like a battery management unit can for sure make sense.


  • Contest Winner

    @daulagari I see. Then as the whole topic suggests, it should suffice to use internal functionality to determine battery level if batt level = vcc. But some form of external circuitry is required if vcc is regulated. And I don't see a big reason to put a lot of effort into making a high precision solution for monitoring battery of "our" small nodes. But the current proposal of a simple voltage divider is a bit too wasteful imo (off topic).


  • Hero Member

    @daulagari said:

    As long as you current VCC direct to the battery I do not understand why people opt for using an external voltage divider

    For the voltage divider the ADC reading is 1023 x R2 / (R1 + R2) x Vcc

    For the 1.1 Volt reference the reading is 1023 x 1.1/Vcc

    An external voltage divider is useful only if you are NOT connecting the measured battery directly to VCC (ie: useful only if you are using a regulator of some sort between VBatt and VCC - whether linear, buck or boost).

    Your first calculation doesn't take the reference in to account. The reading is really:
    1023/Vref x R2 / (R1+R2) * VBatt. If VBatt is also VCC and VRef is also VCC, then the ADC reading is based only on the constant resistor ratio, independent of battery power (VBatt and VRef cancel out if both are VCC).

    So when using the default VCC as Vref, the Vbatt voltage divider is only useful when VCC is NOT VBatt.. in that case, if VBatt is the same as VCC, using an external divider is not an alternate technique with wasted components, it's just a non-starter period.


  • Hero Member

    @Zeph: Fully agreed.

    Your first calculation doesn't take the reference in to account. The reading is really:
    1023/Vref x R2 / (R1+R2) * VBatt.

    My formula's were for the case VCC = Vbatt but yes your formula is more generic but reduces to the same when VCC = Vbatt.


  • Code Contributor

    @Yveaux The only comment i have to this lib now then I'm using it is that you should not use floats but int instead, and just have an imaginary decimal point and divide at later stage to save program memory..
    And talking about this, I should probably start a new thread talking about optimizations, There are some to be done in the mysensors-lib also..


  • Mod

    @Damme I'm fully aware of the use of floating point and the penalties that come with it, don't worry.
    But I just poored existing code into a library. I didn't put any effort in optimizing it.
    Btw Arduino sketches tend to be very inefficient on resource usage, starting by using a 16 bit int type...


  • Hero Member

    Thread Rival ... 😉

    So in trying to understand and improve battery life (currently using V div on Step Reg. Vin (VBatt) in my sensors, why can't I just measure the VCC on AO without the V div as VBAT will never exceed VCC,?

    Trying to figure out what fundamental I'm missing here....


  • Mod

    @ServiceXp So you want to connect VCC of the ATMega to ana analog input pin to read the supply level?
    If this is your idea, then the ATMega will measure the voltage on an analog input relative to the supply voltage. If the supply voltage of the ATMega starts to drop, the relative voltage measured on the analog input will not change w.r.t. VCC.
    By using a voltage divider you bring the voltage to be measured within 0,..,1.1V range (roughly). The ATMega has an internal 1.1V voltage reference which will remain stable when VCC drops, and thus can be used to meaure the supply level using a voltage divider.


  • Hero Member

    @Yveaux said:

    @ServiceXp So you want to connect VCC of the ATMega to ana analog input pin to read the supply level?
    If this is your idea, then the ATMega will measure the voltage on an analog input relative to the supply voltage. If the supply voltage of the ATMega starts to drop, the relative voltage measured on the analog input will not change w.r.t. VCC.
    By using a voltage divider you bring the voltage to be measured within 0,..,1.1V range (roughly). The ATMega has an internal 1.1V voltage reference which will remain stable when VCC drops, and thus can be used to meaure the supply level using a voltage divider.

    1. No; vBatt to AO; MCU will be powered by Step Up Reg. vOut.

    2. The MCU supply voltage will never be lower then vBatt. (in the case of 2 AA Batteries). vBatt will always be lower then MCU VCC, In all reality MCU VCC will never change in a significant way, until Step Up Reg drops out.

    3. think it's this v1.1 ref. that may be confusing me, but it just seems like this method should work with out the V div for sensors using 2 AA batteries or any <3.3v power source. 😉


  • Mod

    @ServiceXp OK, I didn't really get your description in the previous post them.
    When VCC is stable due to to the step up converter then it should also work to just reference analog input to vcc. This way your measuring range is 0,..,vcc, which is a lot more then 0,..,1.1v



  • Thread revival.

    I have a sensor powered by 2 AA batteries, does this still stand as a simple way to obtain their status? It was super easy to implement!



  • Would this library also work on a WEMOS D1, which is powered by a esp8266?

    Paai


  • Mod

    @Hans-Paijmans no, it's avr only


  • Mod

    On esp, ESP.getVcc() can be used. More info: https://www.letscontrolit.com/forum/viewtopic.php?t=130

    Edit: use MySensors' hwCPUVoltage() instead. See below for more information.



  • Hi,
    possible to read also internal VCC for SAMD21 and NRF5 platform core?
    MiKa


Log in to reply
 

367
Online

7.4k
Users

8.2k
Topics

88.9k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.