claws / BH1750

An Arduino library for the digital light sensor breakout boards containing the BH1750FVI IC
MIT License
248 stars 107 forks source link

Trouble using 2 sensors #64

Closed Coelhomatias closed 4 years ago

Coelhomatias commented 4 years ago

Hi, using this code:

#include<Arduino.h>
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter1(0x23);
BH1750 lightMeter2(0x5C);
void setup(){
  Serial.begin(9600);
  Wire.begin(D1, D2);
  lightMeter1.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);
  lightMeter2.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2);
  Serial.println(F("BH1750 Test"));
}
void loop() {
  uint16_t lux1 = lightMeter1.readLightLevel();
  uint16_t lux2 = lightMeter2.readLightLevel();
  Serial.print("Light1: ");
  Serial.print(lux1);
  Serial.print(" lx. Light2: ");
  Serial.print(lux2);
  Serial.println(" lx.");
  delay(1000);
}

in a nodeMCU, like sugested in #25 but the output is the same for both, only reads from 0x23

Coelhomatias commented 4 years ago

Device finds both sensors

image

Coelhomatias commented 4 years ago

Found the issue!! the begin() function also has a default address. Problem corrected with this:

#include<Arduino.h>
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter1(0x23);
BH1750 lightMeter2(0x5C);
void setup(){
  Serial.begin(9600);
  Wire.begin(D1, D2);
  lightMeter1.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2, 0x23);
  lightMeter2.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2, 0x5C);
  Serial.println(F("BH1750 Test"));
}
void loop() {
  uint16_t lux1 = lightMeter1.readLightLevel();
  uint16_t lux2 = lightMeter2.readLightLevel();
  Serial.print("Light1: ");
  Serial.print(lux1);
  Serial.print(" lx. Light2: ");
  Serial.print(lux2);
  Serial.println(" lx.");
  delay(1000);
}

@claws you should fix this. Closing the issue

claws commented 4 years ago

For future reference, this use case is demonstrated by the example showing how different I2C and Wire instances can be used. This particular use case could potentially be made clearer by adding something to the README.