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. Development
  3. Light sensor with battery level problem

Light sensor with battery level problem

Scheduled Pinned Locked Moved Development
5 Posts 2 Posters 1.3k 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.
  • derksuhD Offline
    derksuhD Offline
    derksuh
    wrote on last edited by
    #1

    Hello all,

    I made a light sensor node and uploaded the example sketch. This is working fine!.
    But the node will be supplies by 2 AA batteries so i would like to see the battery level. I tried it like this but it does not work can anyone help me?

    #include <SPI.h>
    #include <MySensor.h>  
    
    #define NODE_ID 1
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 0
    
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    int lastLightLevel;
    
    void setup()  
    { 
      gw.begin(NULL, NODE_ID, false);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Light Sensor", "1.0");
    
      // Register all sensors to gateway (they will be created as child devices)
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()      
    {     
      int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
      Serial.println(lightLevel);
      if (lightLevel != lastLightLevel) {
          gw.send(msg.set(lightLevel));
          lastLightLevel = lightLevel;
      }
      gw.sleep(SLEEP_TIME);
    }
    
    #define MIN_V 2700
    #define MAX_V 3200
    int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100); // Convert voltage to percentage
    
    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
    0
    • B Offline
      B Offline
      boozz
      wrote on last edited by
      #2

      Hi derksuh,

      The code for the battery level should be in the loop, you've put it somewhere outside, although the sketch compiles....
      I think you should have a look at an specific example that comes with the mysensors library. Look at BatteryPoweredSensor. In that you'll find the code for sending the battery level. Now combine that with your sketch.

      good luck,

      Boozz

      1 Reply Last reply
      0
      • B Offline
        B Offline
        boozz
        wrote on last edited by
        #3

        I've made some alterations to your code:
        Haven't tested is, but it compiles....:smirk:

            #include <SPI.h>
            #include <MySensor.h>  
        
            #define NODE_ID 1
            #define CHILD_ID_LIGHT 0
            #define LIGHT_SENSOR_ANALOG_PIN 0
        
            int BATTERY_SENSE_PIN = A1;  // select the input pin for the battery sense point
            int oldBatteryPcnt = 0;
        
            unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
        
            MySensor gw;
            MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
            int lastLightLevel;
        
            void setup()  
            { 
              gw.begin(NULL, NODE_ID, false);
        
              // Send the sketch version information to the gateway and Controller
              gw.sendSketchInfo("Light Sensor", "1.0");
        
              // Register all sensors to gateway (they will be created as child devices)
              gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
        
              
        
            }
        
            void loop()      
            {     
              int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
              Serial.println(lightLevel);
              if (lightLevel != lastLightLevel) {
                  gw.send(msg.set(lightLevel));
                  lastLightLevel = lightLevel;
              }
              // get the battery Voltage
              int sensorValue = analogRead(BATTERY_SENSE_PIN);
        
              //2AA = approx. 3VDC --> sensorValue: 618  (3/5*1023)
              //sensorValue / 6.18 = 100  (%); rough estimate...
           
              int batteryPcnt = sensorValue / 6.18;
              if (batteryPcnt != oldBatteryPcnt) {
                  gw.sendBatteryLevel(batteryPcnt);
                  oldBatteryPcnt = batteryPcnt;
              }
              gw.sleep(SLEEP_TIME);
            }
        
        
        1 Reply Last reply
        0
        • derksuhD Offline
          derksuhD Offline
          derksuh
          wrote on last edited by derksuh
          #4

          @boozz

          I see what you did but that means that have to put a resistor in line with the battery and put that on A1. The sketch i meant measured the voltage supplied on vcv in the atmega 328

          1 Reply Last reply
          0
          • B Offline
            B Offline
            boozz
            wrote on last edited by
            #5

            @derksuh

            Ok, but it still needs your code to find a place within the loop if I'm not mistaken?

            I'm not a c-programmer, so now and then I'm somewhat confused by the code of others. I learn by doing, which is now and then a process of copy-paste and at other moments analysing how others did it.

            BR,

            Boozz

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


            19

            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