Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. 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 ;-)

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

Scheduled Pinned Locked Moved Development
supplyvcc
90 Posts 20 Posters 143.3k Views 13 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • daulagariD daulagari

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

    AnticimexA Offline
    AnticimexA Offline
    Anticimex
    Contest Winner
    wrote on last edited by
    #57

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

    Do you feel secure today? No? Start requiring some signatures and feel better tomorrow ;)

    1 Reply Last reply
    0
    • daulagariD daulagari

      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.

      Z Offline
      Z Offline
      Zeph
      Hero Member
      wrote on last edited by
      #58

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

      1 Reply Last reply
      0
      • daulagariD Offline
        daulagariD Offline
        daulagari
        Hero Member
        wrote on last edited by
        #59

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

        1 Reply Last reply
        0
        • YveauxY Yveaux

          Hi there!

          Inspired by the Blog entry at http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ I decided to write a simple Arduino library to measure VCC level without any external components!
          This library can be used to measure the VCC level from e.g. battery powered sensors that do not use a voltage regulator but are powered directly from the batteries and send the battery level to the gateway.

          The trick is to use the AVR's internal 1.1V reference to measure AVcc. This does not require an external voltage divider.

          The Vcc component can report the VCC level either in volts, or in percentage. Reporting in percentage is a nice way to report the battery level in MySensors!
          For example:

          #include <Vcc.h>
          const float VccExpected   = 3.0;
          const float VccCorrection = 2.860/2.92;  // Measured Vcc by multimeter divided by reported Vcc
          Vcc vcc(VccCorrection);
          
          static int oldBatteryPcnt = 0;
          void loop()
          {
          	int batteryPcnt = (int)vcc.Read_Perc(VccExpected);
          	if (oldBatteryPcnt != batteryPcnt)
          	{
          		gw.sendBatteryLevel(batteryPcnt);
          		oldBatteryPcnt = batteryPcnt;
          	}
          }
          

          Deviations can easily be corrected for by running one of the example sketches and at the same time measure VCC with a multimeter.
          The correction factor should be entered as (VCC multimeter/VCC reported) in the constructor of the Vcc component (the VccCorrection parameter in the example above).
          See the example sketches and code for more info.

          The library can be found at: https://github.com/Yveaux/arduino_vcc
          Or download as ZIP: https://github.com/Yveaux/arduino_vcc/archive/master.zip

          Have fun!

          DammeD Offline
          DammeD Offline
          Damme
          Code Contributor
          wrote on last edited by
          #60

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

          YveauxY 1 Reply Last reply
          0
          • DammeD Damme

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

            YveauxY Offline
            YveauxY Offline
            Yveaux
            Mod
            wrote on last edited by Yveaux
            #61

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

            http://yveaux.blogspot.nl

            1 Reply Last reply
            0
            • RJ_MakeR Offline
              RJ_MakeR Offline
              RJ_Make
              Hero Member
              wrote on last edited by
              #62

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

              RJ_Make

              YveauxY 1 Reply Last reply
              0
              • RJ_MakeR RJ_Make

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

                YveauxY Offline
                YveauxY Offline
                Yveaux
                Mod
                wrote on last edited by
                #63

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

                http://yveaux.blogspot.nl

                RJ_MakeR 1 Reply Last reply
                0
                • YveauxY Yveaux

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

                  RJ_MakeR Offline
                  RJ_MakeR Offline
                  RJ_Make
                  Hero Member
                  wrote on last edited by
                  #64

                  @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. ;-)

                  RJ_Make

                  YveauxY 1 Reply Last reply
                  0
                  • RJ_MakeR RJ_Make

                    @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. ;-)

                    YveauxY Offline
                    YveauxY Offline
                    Yveaux
                    Mod
                    wrote on last edited by
                    #65

                    @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

                    http://yveaux.blogspot.nl

                    1 Reply Last reply
                    0
                    • Mark SwiftM Offline
                      Mark SwiftM Offline
                      Mark Swift
                      wrote on last edited by
                      #66

                      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!

                      1 Reply Last reply
                      1
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #67

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

                        Paai

                        YveauxY 1 Reply Last reply
                        0
                        • ? A Former User

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

                          Paai

                          YveauxY Offline
                          YveauxY Offline
                          Yveaux
                          Mod
                          wrote on last edited by
                          #68

                          @Hans-Paijmans no, it's avr only

                          http://yveaux.blogspot.nl

                          1 Reply Last reply
                          0
                          • mfalkviddM Offline
                            mfalkviddM Offline
                            mfalkvidd
                            Mod
                            wrote on last edited by mfalkvidd
                            #69

                            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.

                            1 Reply Last reply
                            0
                            • MiKaM Offline
                              MiKaM Offline
                              MiKa
                              wrote on last edited by
                              #70

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

                              YveauxY mfalkviddM 2 Replies Last reply
                              0
                              • MiKaM MiKa

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

                                YveauxY Offline
                                YveauxY Offline
                                Yveaux
                                Mod
                                wrote on last edited by
                                #71

                                @MiKa no, it's avr only

                                http://yveaux.blogspot.nl

                                1 Reply Last reply
                                0
                                • MiKaM MiKa

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

                                  mfalkviddM Offline
                                  mfalkviddM Offline
                                  mfalkvidd
                                  Mod
                                  wrote on last edited by mfalkvidd
                                  #72

                                  @MiKa use hwCPUVoltage(), this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.

                                  MiKaM 1 Reply Last reply
                                  1
                                  • mfalkviddM mfalkvidd

                                    @MiKa use hwCPUVoltage(), this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.

                                    MiKaM Offline
                                    MiKaM Offline
                                    MiKa
                                    wrote on last edited by
                                    #73

                                    @mfalkvidd said in 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 ;-):

                                    @MiKa use hwCPUVoltage() this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.

                                    Thanks ! It works on SAMD21E board :)

                                    MiKaM 1 Reply Last reply
                                    2
                                    • MiKaM MiKa

                                      @mfalkvidd said in 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 ;-):

                                      @MiKa use hwCPUVoltage() this works for AVR, SAMD and ESP8266. ESP8266 requires defining MY_SPECIAL_DEBUG though.

                                      Thanks ! It works on SAMD21E board :)

                                      MiKaM Offline
                                      MiKaM Offline
                                      MiKa
                                      wrote on last edited by
                                      #74

                                      It looks, its working also on NRF5 platform, tested with NRF51822 MCU ;) :+1:

                                      https://raw.githubusercontent.com/d00616/arduino-nRF5-boards/master/51822_ITC_PolyU_HK/51822_ITC_PolyU_HK.jpg

                                      1 Reply Last reply
                                      2
                                      • andredtsA Offline
                                        andredtsA Offline
                                        andredts
                                        wrote on last edited by
                                        #75

                                        @Yveaux Hi, thanks for your work. Maybe I´m going over something that was covered before, but I need some help, I´m using this code in a door sensor with 2 aa battery, and reporting if battery changes, when the door opens or closes. My problem is that I always get diferent readings from close to open, so I´m always reporting battery level and using more power than needed. Thanks

                                        YveauxY 1 Reply Last reply
                                        0
                                        • andredtsA andredts

                                          @Yveaux Hi, thanks for your work. Maybe I´m going over something that was covered before, but I need some help, I´m using this code in a door sensor with 2 aa battery, and reporting if battery changes, when the door opens or closes. My problem is that I always get diferent readings from close to open, so I´m always reporting battery level and using more power than needed. Thanks

                                          YveauxY Offline
                                          YveauxY Offline
                                          Yveaux
                                          Mod
                                          wrote on last edited by
                                          #76

                                          @andredts the voltage reference only has limited accuracy, therefore the reported battery voltage might vary slightly. Also the load on the battery can change, causing a variation in battery level reported. Not a lot you can do about that I'm afraid.
                                          I experimented with sending all decreases in battery level wrt the previous value, and only large increases (eg 10% or more to detect change of batteries). Works quite well.

                                          http://yveaux.blogspot.nl

                                          andredtsA 1 Reply Last reply
                                          2
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          13

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular