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

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

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

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

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

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

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

• @Hans-Paijmans no, it's avr only

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

• @MiKa no, it's avr only

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

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

Thanks ! It works on SAMD21E board

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

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

• @Yveaux Thanks, that was exactly the insight I was looking, for my door sensor with 2aa battery, your way worked great. For a scene controller with a CR2032 a had to not send also decreases greater then 10%, but hey, 10 steps is more enough.

• @andredts cr2032 is a totally different story. The voltage level fluctuates significantly when sending messages compared to AA powered sensors.
The 10% is only an example value and used for an increasing voltage level (that normally should only happen when replacing batteries). For a decreasing voltage (regular battery usage) you can just report the level and get much higher resolution. Question is if it will really be useful for cr2032 though...

• How about storing last 10 values and send an average every time?

• @Yveaux Thanks, that was exactly the insight I was looking, for my door sensor with 2aa battery, your way worked great. For a scene controller with a CR2032 a had to not send also decreases greater then 10%, but hey, 10 steps is more enough.

For having consistent measurements with a CR2032 you should measure voltage as first action after waking up from sleep.
I keep the value in a variable and process it after "action" message of the node.

If possible, run at 1Mhz on internal oscillator so the power consumption of the atmega stays very low (around 1mA instead of 3 at 8MHz/3V).