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. General Discussion
  3. Reporting Battery Level

Reporting Battery Level

Scheduled Pinned Locked Moved General Discussion
23 Posts 6 Posters 10.4k Views 8 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.
  • bjacobseB Offline
    bjacobseB Offline
    bjacobse
    wrote on last edited by bjacobse
    #2

    I don't have my sketch available, but I used this code below for measuring 2xAA. I have changed #define MIN_V 1900 (milliVolt) since I have disabled BOD, so Arduino runs to 1,8V, bur NRF24L01+ is working down t0 1,9V
    NOTE: This is only showing battery measuring, it's not a full sketch...

      void loop() 
      {
      #define MIN_V 2700
      #define MAX_V 3200
      int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100); // Convert voltage to percentage
      gw.sendBatteryLevel(batteryPcnt); // Send battery percentage to gateway
       
      } 
      long readVcc() {
        // Read 1.1V reference against AVcc
        // set the reference to Vcc and the measurement to the internal 1.1V reference
        #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
          ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
        #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
          ADMUX = _BV(MUX5) | _BV(MUX0);
        #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
          ADMUX = _BV(MUX3) | _BV(MUX2);
        #else
          ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
        #endif  
       
        delay(2); // Wait for Vref to settle
        ADCSRA |= _BV(ADSC); // Start conversion
        while (bit_is_set(ADCSRA,ADSC)); // measuring
       
        uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
        uint8_t high = ADCH; // unlocks both
       
        long result = (high<<8) | low;
       
        result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
        return result; // Vcc in millivolts
      }
    
    1 Reply Last reply
    1
    • Fat FlyF Offline
      Fat FlyF Offline
      Fat Fly
      wrote on last edited by
      #3

      Okay. I understand but how to add this piece from software to each sketch? Each what arduino powerted from batteryes.

      1 Reply Last reply
      0
      • Fat FlyF Fat Fly

        How to add to sketch this feature? I use Domoticz controller and serial gateway. Nodes is Arduino mini pro 3,3V with batteries. Today i use door open/close sensor, elecricity meter, moisture sensor.

        AWIA Offline
        AWIA Offline
        AWI
        Hero Member
        wrote on last edited by
        #4

        @Fat-Fly Easiest way is to use the Vcc library by @Yveaux

        1 Reply Last reply
        0
        • Fat FlyF Offline
          Fat FlyF Offline
          Fat Fly
          wrote on last edited by
          #5

          This is better. I need cut off Arduino voltage regulator and power led ?

          AWIA 1 Reply Last reply
          0
          • Fat FlyF Fat Fly

            This is better. I need cut off Arduino voltage regulator and power led ?

            AWIA Offline
            AWIA Offline
            AWI
            Hero Member
            wrote on last edited by
            #6

            @Fat-Fly To save power you can... but not needed for the Vcc library to work if you power your pro-mini from the vcc pin ;-)

            1 Reply Last reply
            0
            • Fat FlyF Offline
              Fat FlyF Offline
              Fat Fly
              wrote on last edited by
              #7

              Ok. I try to write sketch.

              1 Reply Last reply
              0
              • Fat FlyF Offline
                Fat FlyF Offline
                Fat Fly
                wrote on last edited by AWI
                #8
                 * The MySensors Arduino library handles the wireless radio link and protocol
                 * between your home built sensors/actuators and HA controller of choice.
                 * The sensors forms a self healing radio network with optional repeaters. Each
                 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                 * network topology allowing messages to be routed to nodes.
                 *
                 * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                 * Copyright (C) 2013-2015 Sensnology AB
                 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                 *
                 * Documentation: http://www.mysensors.org
                 * Support Forum: http://forum.mysensors.org
                 *
                 * This program is free software; you can redistribute it and/or
                 * modify it under the terms of the GNU General Public License
                 * version 2 as published by the Free Software Foundation.
                 *
                 *******************************
                 *
                 * DESCRIPTION
                 *
                 * Simple binary switch example 
                 * Connect button or door/window reed switch between 
                 * digitial I/O pin 3 (BUTTON_PIN below) and GND.
                 * http://www.mysensors.org/build/binary
                 */
                
                
                #include <MySensor.h>
                #include <SPI.h>
                #include <Bounce2.h>
                #include <Vcc.h>
                
                
                #define CHILD_ID 3
                #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
                
                MySensor gw;
                Bounce debouncer = Bounce(); 
                int oldValue=-1;
                
                // Change to V_LIGHT if you use S_LIGHT in presentation below
                MyMessage msg(CHILD_ID,V_TRIPPED);
                
                
                //battery voltage
                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 setup()  
                {  
                  gw.begin();
                
                 // Setup the button
                  pinMode(BUTTON_PIN,INPUT);
                  // Activate internal pull-up
                  digitalWrite(BUTTON_PIN,HIGH);
                  
                  // After setting up the button, setup debouncer
                  debouncer.attach(BUTTON_PIN);
                  debouncer.interval(5);
                  
                  // Register binary input sensor to gw (they will be created as child devices)
                  // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
                  // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                  gw.present(CHILD_ID, S_DOOR);  
                }
                
                
                //  Check if digital input has changed and send in new value
                void loop() 
                {
                  debouncer.update();
                  // Get the update value
                  int value = debouncer.read();
                 
                  if (value != oldValue) {
                     // Send in the new value
                     gw.send(msg.set(value==HIGH ? 1 : 0));
                     oldValue = value;
                  }
                  {
                    int batteryPcnt = (int)vcc.Read_Perc(VccExpected);
                    if (oldBatteryPcnt != batteryPcnt)
                    {
                        gw.sendBatteryLevel(batteryPcnt);
                        oldBatteryPcnt = batteryPcnt;
                    }
                }
                  
                  
                }'''
                1 Reply Last reply
                0
                • Fat FlyF Offline
                  Fat FlyF Offline
                  Fat Fly
                  wrote on last edited by
                  #9

                  Verification successful says codebender. I do not know working this or not. I can't try. Only in home after working day.

                  1 Reply Last reply
                  1
                  • Fat FlyF Offline
                    Fat FlyF Offline
                    Fat Fly
                    wrote on last edited by
                    #10

                    https://codebender.cc/sketch:337094 This is modified electricity meter sketch with battery level .

                    NuubiN 1 Reply Last reply
                    0
                    • Fat FlyF Fat Fly

                      https://codebender.cc/sketch:337094 This is modified electricity meter sketch with battery level .

                      NuubiN Offline
                      NuubiN Offline
                      Nuubi
                      wrote on last edited by
                      #11

                      @Fat-Fly Nice, interested to try this also.
                      Any idea how long the batteries will last? Electricity meters tend to blink quite often, so not that much sleep..

                      1 Reply Last reply
                      0
                      • Fat FlyF Offline
                        Fat FlyF Offline
                        Fat Fly
                        wrote on last edited by Fat Fly
                        #12

                        At the moment i try this with 2xAA recargeable batteryes but no luck. Gnd and vcc pin. Not working , not reported. if i find batterys.... I search. All remotes is AAA :( 2.68volts from recargeable AA's. where is the fish ?

                        1 Reply Last reply
                        0
                        • Fat FlyF Offline
                          Fat FlyF Offline
                          Fat Fly
                          wrote on last edited by
                          #13

                          From recargeable 2x AA and 2,68V not working.

                          1 Reply Last reply
                          0
                          • Fat FlyF Offline
                            Fat FlyF Offline
                            Fat Fly
                            wrote on last edited by
                            #14

                            I bought 2AA batteries. Working.Domoticz devices page report battery level 3. How to see battery level graph ?

                            AWIA 1 Reply Last reply
                            0
                            • Fat FlyF Fat Fly

                              I bought 2AA batteries. Working.Domoticz devices page report battery level 3. How to see battery level graph ?

                              AWIA Offline
                              AWIA Offline
                              AWI
                              Hero Member
                              wrote on last edited by
                              #15

                              @Fat-Fly to report battery voltage you have to create a S_MULTIMETER sensor and send the voltage with V_VOLTAGE.

                              Sendbatterylevel only changes the battery level in Domoticz in % and can not be shown separate.

                              Tot use your arduino with low voltages you need to change the fuse settings (BOD) with an ISP programmer.

                              1 Reply Last reply
                              0
                              • Fat FlyF Offline
                                Fat FlyF Offline
                                Fat Fly
                                wrote on last edited by
                                #16

                                How to i do this ?

                                AWIA 1 Reply Last reply
                                0
                                • Fat FlyF Fat Fly

                                  How to i do this ?

                                  AWIA Offline
                                  AWIA Offline
                                  AWI
                                  Hero Member
                                  wrote on last edited by
                                  #17

                                  @Fat-Fly I will post some code later when I get to a pc. :eyes:

                                  Fat FlyF 1 Reply Last reply
                                  0
                                  • AWIA AWI

                                    @Fat-Fly I will post some code later when I get to a pc. :eyes:

                                    Fat FlyF Offline
                                    Fat FlyF Offline
                                    Fat Fly
                                    wrote on last edited by
                                    #18

                                    @AWI said:

                                    @Fat-Fly I will post some code later when I get to a pc. :eyes:

                                    This is great. I do not find from google search. My mother tongue is Estonian :)

                                    AWIA 1 Reply Last reply
                                    0
                                    • Fat FlyF Fat Fly

                                      @AWI said:

                                      @Fat-Fly I will post some code later when I get to a pc. :eyes:

                                      This is great. I do not find from google search. My mother tongue is Estonian :)

                                      AWIA Offline
                                      AWIA Offline
                                      AWI
                                      Hero Member
                                      wrote on last edited by AWI
                                      #19

                                      @Fat-Fly "Estonia: Between East, West and the World" ;-)

                                      These are the lines of code which should do the trick... you need to put them in the right spot yourself

                                      #define VOLTAGE_CHILD_ID 		5
                                      
                                      MyMessage voltageMsg(VOLTAGE_CHILD_ID, V_VOLTAGE);	// Node voltage
                                      
                                      gw.present(VOLTAGE_CHILD_ID, S_MULTIMETER, "Battery " );
                                      	
                                      float voltage = vcc.Read_Volts() ;
                                      gw.send(voltageMsg.set(voltage,2));				//send battery in Volt 
                                      		
                                      
                                      
                                      1 Reply Last reply
                                      2
                                      • Fat FlyF Offline
                                        Fat FlyF Offline
                                        Fat Fly
                                        wrote on last edited by
                                        #20

                                        Estonia between world and russia. :)

                                        1 Reply Last reply
                                        0
                                        • alexsh1A Offline
                                          alexsh1A Offline
                                          alexsh1
                                          wrote on last edited by
                                          #21

                                          @Fat-Fly I am not sure how you intend to arrange your electricity pulse meter reading, but in my case I had to wire Pro Mini 5V to mains or otherwise recharge a battery every 5-7 days. I think you can do the total kWh and putting Pro Mini to sleep, but you cannot have total consumption kWh and current consumption kW with sleeping.

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


                                          22

                                          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