EasyPcb BH1750 Batterie sketch



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

  • Mod

    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.



  • 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

  • Hardware Contributor

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



  • 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++;
         }
    
    }
    


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

  • Hardware Contributor

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


  • Mod

    @sundberg84 first post says so ๐Ÿ™‚


  • Hardware Contributor

    @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



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


  • Hardware Contributor

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



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


  • Mod

    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.


Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 2
  • 4
  • 2
  • 15

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts