Analog Read on A5 creates random numbers

I’m using a BH1750 light sensor, I’ve got the sensor reading correctly through the Serial Monitor but when I try to read those same results over a virtual pin from the analog read on Blynk what should be a value similar to “153” comes up on my Blynk App screen (on V2) as “1328” blah-blah-blah, it just keeps bouncing with big swings. Do I need to reference something else other than A5?

Thank you …

(code below compiled on Particle Web IDE)


// This #include statement was automatically added by the Particle IDE.
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

// This #include statement was automatically added by the Particle IDE.
#include "BH1750Lib/BH1750Lib.h"


char auth[] = "b2784b3b3c59??94f8752709100fd";

// door 1 sensor and control
const int magSwitch1 = A3;
const int relaySwitch1 = D6;
// door 2 sensor and control
const int magSwitch2 = A4;
const int relaySwitch2 = D7;

// door 1 status
int magStatus1 = 0;
int ledStatus1 = 0;
// door 2 status
int magStatus2 = 0;
int ledStatus2 = 0;

// fan controls
const int fan1 = D4;
const int fan2 = D5;

// Lumens "LUX" Light Sensor
BH1750Lib lightMeter;
int luxRead = A5;
float luxTemp;

bool result = Blynk.connected();

// timeout in milliseconds
SparkCorePolledTimer updateTimer(1000);

void OnTimer(void) {
    sendDoorStatus();
}

void setup() {
    Serial.begin(9600);
    lightMeter.begin(BH1750LIB_MODE_CONTINUOUSHIGHRES);    
    Blynk.begin(auth);
    while (Blynk.connect() == false) {
        // Wait until connected
    }

    pinMode(relaySwitch1, OUTPUT);
    pinMode(relaySwitch2, OUTPUT);
    pinMode(magSwitch1, INPUT_PULLDOWN);
    pinMode(magSwitch2, INPUT_PULLDOWN);
    pinMode(fan1, OUTPUT);
    pinMode(fan2, OUTPUT);
    
// The next two lines shutdown the relay so when the power bumps your doors don't pop open. 
  digitalWrite( 6 , LOW ); //Double Garage Door Switch
  digitalWrite( 7 , LOW ); //Single Garage Door Switch
  digitalWrite( 0 , HIGH ); //Whole House Fan #1 D0
  digitalWrite( 1 , HIGH ); //Whole House Fan #2 D1

    updateTimer.SetCallback(OnTimer);
    Serial.println("Running...");
}

void sendDoorStatus() {
    Blynk.virtualWrite(V0, ledStatus1);
    Blynk.virtualWrite(V1, ledStatus2);
    Blynk.virtualWrite(V2, luxTemp);

}

void loop() {
    Blynk.run();
    // Process LUX Light Meter
    uint16_t lux = lightMeter.lightLevel();
    Serial.print("Light: ");
    Serial.print(lux);
    Serial.println(" lx");
    delay(2000);
    
    luxTemp = analogRead(luxRead);
    
    //constantly monitor the door magnetic switch status (garage door open or closed)
    magStatus1 = digitalRead(magSwitch1);
    magStatus2 = digitalRead(magSwitch2);

    if (magStatus1 == LOW) {
        ledStatus1 = 1023; // 100% brightness
        //Serial.println("LED1: high");
    } else {
        ledStatus1 = 1;
        //Serial.println("LED1: low");
    }
    if (magStatus2 == LOW) {
        ledStatus2 = 1023;
    } else {
        ledStatus2 = 1;
    }

    updateTimer.Update();
}

@laf0138, I’m a bit confused. You are using an I2C light sensor and reading the lux level from it. So what do you have connected to pin A5 that you are trying to read?

1 Like

I'm just grasping at straws here. I'm trying to get a valid, recognizable number to report using V2 to the Blynk Analog widget. I'm seeing on my Analog Widget in Blynk numbers like 1749 that should be 123 for the real LUX value of the sensor. The reason I guessed A5 might be applicable was because the device has:

Vcc => Vcc
Gnd => Gnd
SCL => D1
SDA => D2
ADD => A5 (HEX address) (so I'm told)

Thanks for taking a crack at this. :slight_smile:

@laf0138, is it not this piece of code that reads the light level?

    // Process LUX Light Meter
    uint16_t lux = lightMeter.lightLevel();
    Serial.print("Light: ");
    Serial.print(lux);
    Serial.println(" lx");

Which library are you using the the BH1750? I have no idea why you would be using A5 on the ADD pin. The ADD pin is used to select the I2C address of the device and should be left unconnected. :wink: