Measuring battery voltage, which way is best?
-
Hello all, may be I did not get the point of this thread, but a very similar code to measure the AVR CPU voltage is already part of the MySensors library: MyHwAVR.cpp#L289
I use it in some of my PIR sensors and it works fine. Simply call hwCPUVoltage() to get it.
This is from my code:void sendBatteryLevel() { // This calls the internal voltage measurement uint16_t voltage = hwCPUVoltage(); // Li AAA Cell Voltage range: discharged - full 2.6V - 3.0V uint16_t batteryPcnt = map(voltage, 2600, 3000, 0, 100); batteryPcnt = constrain(batteryPcnt, 0, 100); // This MySensors function sends the "internal battery info" to the gateway sendBatteryLevel(batteryPcnt); }BR Immo
@virtualmkr Thanks - I did not know that - it is really helpful! :)
The original purpose was that the 'build' section on 'battery power' was missing anything about the 'internal voltmeter' and only mentioned the external components method.
-
@virtualmkr Thanks - I did not know that - it is really helpful! :)
The original purpose was that the 'build' section on 'battery power' was missing anything about the 'internal voltmeter' and only mentioned the external components method.
@skywatch Thank you for clarifying. And nice that you like my implementation :-)
I also find it very good and helpful for beginners to show the internal battery measurement method on the "Battery Power" page from MySensors. When I started with MySensors I followed the instructions for measuring via analog pin and 2 resistors. The more simple internal measurement I discovered way much later.
BTW there are some more built in functions, e.g. for the current CPU frequency and the current free heap size. With the MYSController you can query these values from the individual nodes:

You need only to add the #define for this functionality in your Node sketch:
// Enable support for I_DEBUG messages. #define MY_SPECIAL_DEBUG -
@skywatch Thank you for clarifying. And nice that you like my implementation :-)
I also find it very good and helpful for beginners to show the internal battery measurement method on the "Battery Power" page from MySensors. When I started with MySensors I followed the instructions for measuring via analog pin and 2 resistors. The more simple internal measurement I discovered way much later.
BTW there are some more built in functions, e.g. for the current CPU frequency and the current free heap size. With the MYSController you can query these values from the individual nodes:

You need only to add the #define for this functionality in your Node sketch:
// Enable support for I_DEBUG messages. #define MY_SPECIAL_DEBUG@virtualmkr Thanks - this is really useful stuff which is 'hidden away' and should really be seen by all coming to mysensors for the first time. I hope @mfalkvidd will find a way to get this into the battery section.
I modified your implementation a little to better suit my needs, as follows....
void sendBatteryLevel() { // This calls the internal voltage measurement and converts to Volts. wait(250); //Allow power lines to settle. float voltage = hwCPUVoltage()/1000; wdt_reset(); batteryPcnt = map(voltage, brown_out_voltage, BattMax, 0, 100); //batteryPcnt = constrain(batteryPcnt, 0, 100); // This MySensors function sends the "internal battery info" to the gateway if(batteryPcnt != oldBatteryPcnt){ sendBatteryLevel(batteryPcnt); oldBatteryPcnt = batteryPcnt; } }I wanted it all in volts and not mV and also not send unless there is a change in % level to further reduce battery usage. For some reason the 'constrain' function gives a warning in minicore so I leave it out for now.
The image you posted shows frequency as "cHz". I am not familiar with that term, do you know what it is?
-
@virtualmkr Thanks - this is really useful stuff which is 'hidden away' and should really be seen by all coming to mysensors for the first time. I hope @mfalkvidd will find a way to get this into the battery section.
I modified your implementation a little to better suit my needs, as follows....
void sendBatteryLevel() { // This calls the internal voltage measurement and converts to Volts. wait(250); //Allow power lines to settle. float voltage = hwCPUVoltage()/1000; wdt_reset(); batteryPcnt = map(voltage, brown_out_voltage, BattMax, 0, 100); //batteryPcnt = constrain(batteryPcnt, 0, 100); // This MySensors function sends the "internal battery info" to the gateway if(batteryPcnt != oldBatteryPcnt){ sendBatteryLevel(batteryPcnt); oldBatteryPcnt = batteryPcnt; } }I wanted it all in volts and not mV and also not send unless there is a change in % level to further reduce battery usage. For some reason the 'constrain' function gives a warning in minicore so I leave it out for now.
The image you posted shows frequency as "cHz". I am not familiar with that term, do you know what it is?
@skywatch Thank you for the minicore tip. I was not aware of this project.
Implementation of constrain() in minicore differs from constrain() implementation in the original Arduino core.
So maybe it is a minicore issue. Or may be your batteryPcnt type is float?
Neverless one should prevent battery percentage below 0% or above 100% because it makes no much sense."cHz" is actually strange but it means 0.1 x MHz, so for my ESP8266 I get 1600 x 0.1 MHz = 160 MHz:

This way the frequency value fits well into uint16_t type.
-
@skywatch Thank you for the minicore tip. I was not aware of this project.
Implementation of constrain() in minicore differs from constrain() implementation in the original Arduino core.
So maybe it is a minicore issue. Or may be your batteryPcnt type is float?
Neverless one should prevent battery percentage below 0% or above 100% because it makes no much sense."cHz" is actually strange but it means 0.1 x MHz, so for my ESP8266 I get 1600 x 0.1 MHz = 160 MHz:

This way the frequency value fits well into uint16_t type.
@virtualmkr Thanks for the information, very interesting.
My batteryPcnt is an int. The voltage is defined as a float but as I understand it any math between a float and an int will always result in an int, expecially if the variable it is being stored in is an int.
cHz! Well I learn something new, so that is good! ;)
As for minicore, it does get a mention in the bootloader page on this site and I like it as it is easy to use and I don't need to keep going to the command line or avrdudess all the time, it all works within arduino ide. As a big plus it was upgraded to version 2.1.0 today and the constrain issue has been fixed!
-
My pull request has now been merged to the MySensors development branch. I have updated the battery page to show the example sketch for internal measurement. Note that the internal measurement method does not work on all supported mcus (not esp8266 for example) but I don't think it matter much since the battery page recommends a Pro Mini which works.
Special thanks to @hek for tracking down a tricky display problem with the sketch inclusion, and to @Yveaux, @user2684 and @tekka for feedback on the pull request.
-
My pull request has now been merged to the MySensors development branch. I have updated the battery page to show the example sketch for internal measurement. Note that the internal measurement method does not work on all supported mcus (not esp8266 for example) but I don't think it matter much since the battery page recommends a Pro Mini which works.
Special thanks to @hek for tracking down a tricky display problem with the sketch inclusion, and to @Yveaux, @user2684 and @tekka for feedback on the pull request.
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
-
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
@skywatch Rounding up to the nearest int ?
-
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
@skywatch as @zboblamont said, it makes sure we have ”proper” rounding. In C, floating point numbers are always rounded down Ehrn converted to an integer. So 42.8 becomes 42. By adding 0.5, we get the rounding expected by most humans in most cases.
To read about different rounding methods, see https://en.wikipedia.org/wiki/Rounding#Rounding_to_the_nearest_integer
My personal favorite is called ”banker’s rounding” but I did not use it in this sketch.
-
@zboblamont & @mfalkvidd - Thank you for that. It never occured to me that rounding might need some help. But now it makes logical sense.
@mfalkvidd I guess if you used 'bankers rounding' you would always end up with 0.