reporting battery to domoticZ
-
thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
.// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Enable debug prints #define MY_DEBUG #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define CHILD_ID_BATTERY 4 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds) #define SKETCH_NAME "Temp_Hum_Batt" // Change to a fancy name you like #define SKETCH_VERSION "2.1" // Your version DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.0041055718475073313782991202346 // 4.20/1023 volt #define VMIN 3.6 // Vmin = 3.6 lipo empty #define VMAX 4.2 // Vmax = 4.2 volt lipo full int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { analogReference(INTERNAL); // For battery sensing delay(500); // Allow time for radio if power used as reset dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); metric = getControllerConfig().isMetric; } void presentation() { // Send the Sketch Version Information to the Gateway // Send the Sketch Version Information to the Gateway sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_BATTERY, S_MULTIMETER); present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { delay(500); // Allow time for radio if power used as reset delay(dht.getMinimumSamplingPeriod()); // Fetch temperatures from DHT sensor float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } // Fetch humidity from DHT sensor float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } batM(); sleep(SLEEP_TIME); //sleep a bit } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); send(msgbatt.set(batteryPcnt ,2)); batLoop = 0; } else { batLoop++; } }```And my serial monitor is going wild .lol
.41815 MCO:SLP:WUP=-1 Battery percent: 95 % Battery Average (Send): -12 % 45359 !TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=NACK:244 45403 !TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=1,st=NACK:-12.00 45410 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 45415 MCO:SLP:TPD 45417 MCO:SLP:WUP=-1 47962 !TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=2,st=NACK:20.0 T: 20.00 48007 !TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=3,st=NACK:49.6 H: 49.60 Battery percent: -49 % 49014 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 49020 MCO:SLP:TPD 49022 MCO:SLP:WUP=-1 51567 !TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=4,st=NACK:49.2 H: 49.20 Battery percent: -26 % 52575 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 52582 MCO:SLP:TPD -
thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
.// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Enable debug prints #define MY_DEBUG #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define CHILD_ID_BATTERY 4 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds) #define SKETCH_NAME "Temp_Hum_Batt" // Change to a fancy name you like #define SKETCH_VERSION "2.1" // Your version DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.0041055718475073313782991202346 // 4.20/1023 volt #define VMIN 3.6 // Vmin = 3.6 lipo empty #define VMAX 4.2 // Vmax = 4.2 volt lipo full int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { analogReference(INTERNAL); // For battery sensing delay(500); // Allow time for radio if power used as reset dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); metric = getControllerConfig().isMetric; } void presentation() { // Send the Sketch Version Information to the Gateway // Send the Sketch Version Information to the Gateway sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_BATTERY, S_MULTIMETER); present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { delay(500); // Allow time for radio if power used as reset delay(dht.getMinimumSamplingPeriod()); // Fetch temperatures from DHT sensor float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } // Fetch humidity from DHT sensor float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } batM(); sleep(SLEEP_TIME); //sleep a bit } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); send(msgbatt.set(batteryPcnt ,2)); batLoop = 0; } else { batLoop++; } }``` -
I still cant get it to work, No voltage reading or logging.
I there nobody trying to build a equal sensor.maybe someone who could rework my sketch. or having a working sketch where i could delete some sensors from.
kind regards and greetings from the Netherlands.
-
I still cant get it to work, No voltage reading or logging.
I there nobody trying to build a equal sensor.maybe someone who could rework my sketch. or having a working sketch where i could delete some sensors from.
kind regards and greetings from the Netherlands.
@Rene046 I would be happy to help you but need some information on your circuit:
- How are you powering your Arduino? Through an (onboard) regulator or directly from battery?
- The internal reference can only be used up to 1.1V, you seem to be having a voltage divider which has a resulting voltage of > 1.1 V ?
- I do not understand your averaging algorithm, why are you not taking an average of the latest x readings?
My suggestion would be to use a different external reference (output of the dc-dc boost converter/ Vcc?) to measure the voltage.
-
Hi Awi
Thanks that you want to help,
Im powering it by a lipo battery 1 cell using an dc-dc boost converter to get 5 volts
i would like to see what this lipo is doing during a day. this lipo will be charged by an solar panel 6 volt 300ma still waiting for that panel.You are absolutely right with my voltage divider it's at 1.9 volt while lipo is at 4.2 volt
I'm not a hero in elektronics , just starting to play with it. so to start i should make an voltage divider that gives me aprox 1,1 volt max at 4,2 volt.
I changed the voltages divider now from 270k-100k
giving me 1.07 volt at 4.2 volt lipoi'm totally open for any other design you could give, i just used some code from others that was excepted by demoticz.
If you like you could send me an sample how you would do it or change my sketch ..
today i also received my BMP180 and BH1750 i want to include in the future.. -
my serial monitor is now making a bit more cense voltage is about 0,10 volt off
but maybe i do something wrong with calculation.
percentage is still something wrong withby the way i'm not shure with the low voltage of an 16850 lipo at 3.6 volt ...
20412 MCO:SLP:WUP=-1 22921 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:17.8 T: 17.80 22929 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:35.4 H: 35.40 23939 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.53 Battery voltage: 3.53 V Battery percent: -10 % 23946 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 23954 MCO:SLP:TPD 23955 MCO:SLP:WUP=-1 26465 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:36.1 H: 36.10 27473 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.52 Battery voltage: 3.52 V Battery percent: -12 % 27481 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 27489 MCO:SLP:TPDand here the latest sketch
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Enable debug prints #define MY_DEBUG #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define CHILD_ID_BATTERY 4 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds) #define SKETCH_NAME "Temp_Hum_Batt" // Change to a fancy name you like #define SKETCH_VERSION "2.1" // Your version DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.0041055718475073313782991202346 // 4.20/1023 volt #define VMIN 3.6 // Vmin = 3.6 lipo empty #define VMAX 4.2 // Vmax = 4.2 volt lipo full int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { analogReference(INTERNAL); // For battery sensing delay(500); // Allow time for radio if power used as reset dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); metric = getControllerConfig().isMetric; } void presentation() { // Send the Sketch Version Information to the Gateway // Send the Sketch Version Information to the Gateway sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_BATTERY, S_MULTIMETER); present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { delay(500); // Allow time for radio if power used as reset delay(dht.getMinimumSamplingPeriod()); // Fetch temperatures from DHT sensor float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } // Fetch humidity from DHT sensor float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } batM(); sleep(SLEEP_TIME); //sleep a bit } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; send(msgbatt.set(Vbat ,2)); Serial.print("Battery voltage: "); Serial.print(Vbat); Serial.println(" V"); int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); batLoop = 0; } else { batLoop++; } } -
my serial monitor is now making a bit more cense voltage is about 0,10 volt off
but maybe i do something wrong with calculation.
percentage is still something wrong withby the way i'm not shure with the low voltage of an 16850 lipo at 3.6 volt ...
20412 MCO:SLP:WUP=-1 22921 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:17.8 T: 17.80 22929 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:35.4 H: 35.40 23939 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.53 Battery voltage: 3.53 V Battery percent: -10 % 23946 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 23954 MCO:SLP:TPD 23955 MCO:SLP:WUP=-1 26465 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:36.1 H: 36.10 27473 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.52 Battery voltage: 3.52 V Battery percent: -12 % 27481 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 27489 MCO:SLP:TPDand here the latest sketch
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Enable debug prints #define MY_DEBUG #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define CHILD_ID_BATTERY 4 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds) #define SKETCH_NAME "Temp_Hum_Batt" // Change to a fancy name you like #define SKETCH_VERSION "2.1" // Your version DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.0041055718475073313782991202346 // 4.20/1023 volt #define VMIN 3.6 // Vmin = 3.6 lipo empty #define VMAX 4.2 // Vmax = 4.2 volt lipo full int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { analogReference(INTERNAL); // For battery sensing delay(500); // Allow time for radio if power used as reset dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); metric = getControllerConfig().isMetric; } void presentation() { // Send the Sketch Version Information to the Gateway // Send the Sketch Version Information to the Gateway sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_BATTERY, S_MULTIMETER); present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { delay(500); // Allow time for radio if power used as reset delay(dht.getMinimumSamplingPeriod()); // Fetch temperatures from DHT sensor float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } // Fetch humidity from DHT sensor float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } batM(); sleep(SLEEP_TIME); //sleep a bit } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; send(msgbatt.set(Vbat ,2)); Serial.print("Battery voltage: "); Serial.print(Vbat); Serial.println(" V"); int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); batLoop = 0; } else { batLoop++; } }@Rene046 said in reporting battery to domoticZ:
my serial monitor is now making a bit more cense voltage is about 0,10 volt off
but maybe i do something wrong with calculation.
percentage is still something wrong withim feeding with 3.60 volts at that moment.
-
@Rene046 said in reporting battery to domoticZ:
my serial monitor is now making a bit more cense voltage is about 0,10 volt off
but maybe i do something wrong with calculation.
percentage is still something wrong withim feeding with 3.60 volts at that moment.
@Rene046 The 1.1V internal reference is not calibrated and can be 10% off, So you need to do some calibration yourself.
(b.t.w. I cleaned your postings a little. Please make sure you use the code markings in the right way, i.e. put them on a new line). To get you going I rewrote the battery routine. Changes:- no real need to average the readings if you give it time to stabilize.
- got rid of the "volt per bit" constant
- added a "reference value:" for calibration, you can calculate it from the measured voltage and reported voltage.
// battery calculations void batM(){ const float vRef = 4.2/ 1.1 ; // actual value for max reading, adjust with help of measured voltage delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); // sensorValue is 0..1023 where 1023 == vRef float vBat = sensorValue * vRef / 1024 ; send(msgbatt.set(vBat ,2)); Serial.print("Battery voltage: "); Serial.print(vBat); Serial.println(" V"); // Calculate the battery in % int batteryPcnt = static_cast<int>(((vBat-VMIN)/(VMAX-VMIN))*100.); sendBatteryLevel( batteryPcnt) ; Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); } -
Thanks that looks way better.
I just changed
const float vRef = 4.2/ 1.1 ;
into
const float vRef = 4.2/ 1.07 ;
because this is what i measure with my multimeter with 4.2 volt battery and measuring my voltage divider i have 1.07 volt
.
Giving me strange results something must be bad ..... my multimeter ?? it should be closer to 4.2 ? i guessBattery voltage: 3.92 V 14842 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:53 Battery percent: 53 % 14850 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 14856 MCO:SLP:TPD 14858 MCO:SLP:WUP=-1 17367 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.4 T: 19.40 17376 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:48.5 H: 48.50 17885 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.92 Battery voltage: 3.92 V 17893 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:53 Battery percent: 53 % 17900 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 17906 MCO:SLP:TPD``` -
Thanks that looks way better.
I just changed
const float vRef = 4.2/ 1.1 ;
into
const float vRef = 4.2/ 1.07 ;
because this is what i measure with my multimeter with 4.2 volt battery and measuring my voltage divider i have 1.07 volt
.
Giving me strange results something must be bad ..... my multimeter ?? it should be closer to 4.2 ? i guessBattery voltage: 3.92 V 14842 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:53 Battery percent: 53 % 14850 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 14856 MCO:SLP:TPD 14858 MCO:SLP:WUP=-1 17367 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.4 T: 19.40 17376 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:48.5 H: 48.50 17885 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.92 Battery voltage: 3.92 V 17893 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:53 Battery percent: 53 % 17900 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255 17906 MCO:SLP:TPD``` -
thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
.// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Enable debug prints #define MY_DEBUG #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define CHILD_ID_BATTERY 4 unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds) #define SKETCH_NAME "Temp_Hum_Batt" // Change to a fancy name you like #define SKETCH_VERSION "2.1" // Your version DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.0041055718475073313782991202346 // 4.20/1023 volt #define VMIN 3.6 // Vmin = 3.6 lipo empty #define VMAX 4.2 // Vmax = 4.2 volt lipo full int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { analogReference(INTERNAL); // For battery sensing delay(500); // Allow time for radio if power used as reset dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); metric = getControllerConfig().isMetric; } void presentation() { // Send the Sketch Version Information to the Gateway // Send the Sketch Version Information to the Gateway sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_BATTERY, S_MULTIMETER); present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { delay(500); // Allow time for radio if power used as reset delay(dht.getMinimumSamplingPeriod()); // Fetch temperatures from DHT sensor float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } // Fetch humidity from DHT sensor float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } batM(); sleep(SLEEP_TIME); //sleep a bit } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); send(msgbatt.set(batteryPcnt ,2)); batLoop = 0; } else { batLoop++; } }```@Rene046 what resistors do you use to measure battery. VBAT_PER_BITS is not just 4.2/1023. Personally I am using 1m and 300k it gives me up to 4.77V
-
@Rene046 what resistors do you use to measure battery. VBAT_PER_BITS is not just 4.2/1023. Personally I am using 1m and 300k it gives me up to 4.77V
Hi im using at this moment what i had.
270k-100k giving me 1.07 volt at 4.2 batterySo i stay save , but ok reading is not giving my wanted 4.2 volt but around 3.92 volt
My nano is feeded by an dc -dc boost converter giving me 5 volt from this 4.2 battery.
@AWI
Sorry im still a noob in this, just understood how to make a voltage diver using 3:1 divider
.
Could you please give me a simple example how do this calculation. -
Hi im using at this moment what i had.
270k-100k giving me 1.07 volt at 4.2 batterySo i stay save , but ok reading is not giving my wanted 4.2 volt but around 3.92 volt
My nano is feeded by an dc -dc boost converter giving me 5 volt from this 4.2 battery.
@AWI
Sorry im still a noob in this, just understood how to make a voltage diver using 3:1 divider
.
Could you please give me a simple example how do this calculation.@Rene046
As I understand correct VBAT_PER_BITS is not just 4.2/1032. This depends what resistors do you use. If you use 1m and 470k VBAT_PER_BITS=0.003363075 made from formula and it will never reads more 3.44v.
#define VMIN and #define VMAX 4.2 used only for formula int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
If you use 270k and 100k maximum is 4.07V so will be VBAT_PER_BITS=0.0039784946236559 and not like in your sketch 0.0041055718475073313782991202346 -
Hi im using at this moment what i had.
270k-100k giving me 1.07 volt at 4.2 batterySo i stay save , but ok reading is not giving my wanted 4.2 volt but around 3.92 volt
My nano is feeded by an dc -dc boost converter giving me 5 volt from this 4.2 battery.
@AWI
Sorry im still a noob in this, just understood how to make a voltage diver using 3:1 divider
.
Could you please give me a simple example how do this calculation.@Rene046 You making it complex. The steps)
- Make sure that the input voltage is within the range of the reference. You did that with the voltage divider (max 1.07 V) :+1:
- set
vRefin the sketch to an expected value (4.2 / 1 , where 1 is the calibration value). You did (4.2/1.07) :+1: - Now you need to calibrate in the software:
- Measure the battery voltage (batteryVoltage) (not the voltage divider) with the multimeter. You did (4.2 Volt) :+1:
- Look at the voltage level reported by the sketch (reportedVoltage). You did (3.92 Volt) :+1:
- Calculate the calibration value and adjust vRef. It needs to report 4.2V now it reports 3.92V so you are off by 1.07. => (4.2/1.07) * 1.07 = 4.2 => set vRef to (4.2/ 1.0) and you will be fine.
There are a few reasons why you need to calibrate:
- The internal reference of the Arduino (1.1V) is not calibrated (can roughly vary from 1V to 1.2V)
- Your voltage divider is not always precise.
- Most multimeters are far more accurate...
-
@Rene046
As I understand correct VBAT_PER_BITS is not just 4.2/1032. This depends what resistors do you use. If you use 1m and 470k VBAT_PER_BITS=0.003363075 made from formula and it will never reads more 3.44v.
#define VMIN and #define VMAX 4.2 used only for formula int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
If you use 270k and 100k maximum is 4.07V so will be VBAT_PER_BITS=0.0039784946236559 and not like in your sketch 0.0041055718475073313782991202346@Arnold-Šlepetis Purist physics ;-) : Although you calculation is probably correct for your use case. I associate a
VBAT_PER_BITS=0.0039784946236559with almost atomic precision. As where the internal (band-gap) reference has +- 10% offset and drift with temperature and Vcc (although it is pretty stable for MySensors purposes).

-
What can i do to get a more stable reading now the voltage goes from 3.80 to 4.20 while feeding with an 4.20 volt
-
What can i do to get a more stable reading now the voltage goes from 3.80 to 4.20 while feeding with an 4.20 volt