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. Troubleshooting
  3. EasyPcb BH1750 Batterie sketch

EasyPcb BH1750 Batterie sketch

Scheduled Pinned Locked Moved Troubleshooting
13 Posts 3 Posters 1.4k Views 2 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.
  • T The-Holgi

    Hello,
    i´m using the EasyPcb with 2AA Batteries and a BH1750 Sensor.
    In fhem the Batterie state is at 150 to 153%, here is my sketch, Maybe somebody can look at this:

    // 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 <BH1750.h>
    #include <Wire.h>
    
    #define CHILD_ID_LIGHT 0
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    BH1750 lightSensor;
    
    // V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%.
    // If your controller supports the new V_LEVEL variable, use this instead for
    // transmitting LUX light level.
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    // MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);  
    uint16_t lastlux;
    
    //=========================
    // 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.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    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()  
    { 
      lightSensor.begin();
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Light Lux Sensor", "1.0");
    
      // Register all sensors to gateway (they will be created as child devices)
      present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()      
    {     
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      Serial.println(lux);
      if (lux != lastlux) {
          send(msg.set(lux));
          lastlux = lux;
      }
       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);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    
      sleep(SLEEP_TIME);
    }
    mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by mfalkvidd
    #2

    Welcome to the MySensors forum @the-holgi

    The sketch will never report higher than 100% so the problem is probably some setting in fhem. I don't have experience with fhem but hopefully someone else can help, in case you can't find it yourself.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      The-Holgi
      wrote on last edited by
      #3

      Hello,
      thank you for quick reply.
      Here is the output of serial output:

      Battery percent: -81 %
      Battery Average (Send): -105 %
      794046 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:151
      794054 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
      794060 TSF:TDI:TSL```
      
      It sends 151 to fhem.
      Best regards Holger
      sundberg84S 1 Reply Last reply
      0
      • T The-Holgi

        Hello,
        thank you for quick reply.
        Here is the output of serial output:

        Battery percent: -81 %
        Battery Average (Send): -105 %
        794046 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:151
        794054 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
        794060 TSF:TDI:TSL```
        
        It sends 151 to fhem.
        Best regards Holger
        sundberg84S Offline
        sundberg84S Offline
        sundberg84
        Hardware Contributor
        wrote on last edited by
        #4

        @the-holgi it seems very wrong it is measuring a negative value. Could be the calculation gets messed up because of that? Start checking why you have a negative value? Connection issue ?

        Controller: Proxmox VM - Home Assistant
        MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
        MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
        RFLink GW - Arduino Mega + RFLink Shield, 433mhz

        1 Reply Last reply
        0
        • T Offline
          T Offline
          The-Holgi
          wrote on last edited by
          #5

          Hello,
          there was a mistake in the code. The negative value in serial console now fixed, but the output is to high.
          Now i have change the value of #define VMAX 3.0 to #define VMAX 3.5, at 2,9V batt output
          in fhem is the output now 91%.
          But i´l think this is the wrong way.
          By the way, thanks for quick sending the PCB
          Here is the code:

          // 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 <BH1750.h>
          #include <Wire.h>
          
          #define CHILD_ID_LIGHT 0
          unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
          
          BH1750 lightSensor;
          
          // V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%.
          // If your controller supports the new V_LEVEL variable, use this instead for
          // transmitting LUX light level.
          MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
          // MyMessage msg(CHILD_ID_LIGHT, V_LEVEL);  
          uint16_t lastlux;
          
          //=========================
          // 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.003363075  
          #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
          #define VMAX 3.5                                  //  Vmax = (2xAA bat)=3.0V (892v)
          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
            
            lightSensor.begin();
          }
          
          void presentation()  {
            // Send the sketch version information to the gateway and Controller
            sendSketchInfo("Light Lux Sensor", "1.0");
          
            // Register all sensors to gateway (they will be created as child devices)
            present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
          }
          
          void loop()      
          {     
            uint16_t lux = lightSensor.readLightLevel();// Get Lux value
            Serial.println(lux);
            if (lux != lastlux) {
                send(msg.set(lux));
                lastlux = lux;
            }
             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);
                 batLoop = 0;
                }
               else 
               {
               batLoop++;
               }
          
          }
          
          1 Reply Last reply
          0
          • T Offline
            T Offline
            The-Holgi
            wrote on last edited by
            #6

            Here the output in the console:

            3
            51789 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=23,pt=3,l=2,sg=0,ft=0,st=OK:3
            Battery percent: 68 %
            Battery Average (Send): 91 %
            52801 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:91
            52809 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
            52815 TSF:TDI:TSL
            52817 MCO:SLP:WUP=-1
            52819 TSF:TRI:TSB```
            sundberg84S 2 Replies Last reply
            0
            • T The-Holgi

              Here the output in the console:

              3
              51789 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=23,pt=3,l=2,sg=0,ft=0,st=OK:3
              Battery percent: 68 %
              Battery Average (Send): 91 %
              52801 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:91
              52809 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
              52815 TSF:TDI:TSL
              52817 MCO:SLP:WUP=-1
              52819 TSF:TRI:TSB```
              sundberg84S Offline
              sundberg84S Offline
              sundberg84
              Hardware Contributor
              wrote on last edited by
              #7

              @the-holgi what kind of batteries are you using ? 2xAA ?

              Controller: Proxmox VM - Home Assistant
              MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
              MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
              RFLink GW - Arduino Mega + RFLink Shield, 433mhz

              mfalkviddM 1 Reply Last reply
              0
              • sundberg84S sundberg84

                @the-holgi what kind of batteries are you using ? 2xAA ?

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

                @sundberg84 first post says so :)

                1 Reply Last reply
                0
                • T The-Holgi

                  Here the output in the console:

                  3
                  51789 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=23,pt=3,l=2,sg=0,ft=0,st=OK:3
                  Battery percent: 68 %
                  Battery Average (Send): 91 %
                  52801 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:91
                  52809 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
                  52815 TSF:TDI:TSL
                  52817 MCO:SLP:WUP=-1
                  52819 TSF:TRI:TSB```
                  sundberg84S Offline
                  sundberg84S Offline
                  sundberg84
                  Hardware Contributor
                  wrote on last edited by
                  #9

                  @the-holgi @mfalkvidd - oh, sorry missed that.
                  The code seems to be coming from my example and it works fine, so Im thinking it should be something with the voltage hense my question. But the next question would be if you can measure the voltage on A0?

                  0_1548011510679_b551d99e-4339-4bee-b453-77e6884ef2ca-image.png

                  Controller: Proxmox VM - Home Assistant
                  MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                  MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                  RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    The-Holgi
                    wrote on last edited by
                    #10

                    Hello,
                    on A0 are 0,8V. Batterie voltage is 2,6V.

                    sundberg84S 1 Reply Last reply
                    1
                    • T The-Holgi

                      Hello,
                      on A0 are 0,8V. Batterie voltage is 2,6V.

                      sundberg84S Offline
                      sundberg84S Offline
                      sundberg84
                      Hardware Contributor
                      wrote on last edited by sundberg84
                      #11

                      @the-holgi - hi! Sounds about right :)
                      This is an extended version: https://github.com/sundberg84/HomeAutomation/blob/master/BatteryMeasurer/BatteryMeasurer.ino

                      If you use this in the "//Battery inital calc" you will get a detailed debug which can help you measure. Can you use this code to test.

                      I btw notised in your first post you forgot analogreference() and this might cause the issue - but you have that in your second code. Maybe you could try to remove everything except the battery code and try. You can exclude the loop function in my code above and just do setup() and den reset the node if you need to read again.

                      Controller: Proxmox VM - Home Assistant
                      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        The-Holgi
                        wrote on last edited by
                        #12

                        Hello,
                        the extended code works with right value . Every second the node sends the Batterie value and voltage to Controller.
                        Thanks vor the code.

                        1 Reply Last reply
                        2
                        • mfalkviddM Offline
                          mfalkviddM Offline
                          mfalkvidd
                          Mod
                          wrote on last edited by
                          #13

                          In case anyone finds this thread: an error was found in the battery measurement code. See https://forum.mysensors.org/post/97409 for details. Sundberg84 has updated the code on github with a fix.

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


                          8

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.0k

                          Posts


                          Copyright 2019 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