Chat freely about anything...

User avatar
By elhamkh
#82111 i2c communication between nodemcu esp 8266 and sensors (BH1750 light intensity sensor and SHT11 )
Hi , i want to establish i2c communication in order to receive sensor information on nodemcu esp 8266
i have one BH1750 light intensity sensor and one SHT11 sensor that i connected them to D1 as SCL and D2 as SDA
connections is as follows :
for BH1750 light intensity sensor:
VCC ---> 5 v or 3v3
SCL ---> D1
SDA ----> D2
AD0 -----> Gnd
Gnd -----> Gnd

for SHT11 sensor :
S ---> D1
+ ------> 5 V or 3v3
- --------> Gnd
D ------> D2

SDA and SCL lines also are connected to pull up resistors 10 k ohm and vcc.
sensors works fine separately but two sensors with each other in code do not work
can you help me ?
best regards ....
code is :


// light intensity and sht11 with nodemcu wifi module

#include <Wire.h> //BH1750 IIC Mode
#include <math.h>
#include <BH1750.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SHT1x.h>

int BH1750_address = 0x23; // i2c Addresse
byte buff[2];

//.........................................................
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 4
#define clockPin 5
SHT1x sht1x(dataPin, clockPin);

void setup(){


Wire.begin();
Serial.begin(115200);

BH1750_Init(BH1750_address);


}

void loop(){

float temp_c;
float temp_f;
float humidity;

// Read values from the sensor
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();

// Print the values to the serial port
Serial.print("Temperature: ");
Serial.print(temp_c, DEC);
Serial.print("C / ");
Serial.print(temp_f, DEC);
Serial.print("F. Humidity: ");
Serial.print(humidity);
Serial.println("%");

delay(1000);
//.....................................................



float valf=0;

if(BH1750_Read(BH1750_address)==2){

valf=((buff[0]<<8)|buff[1])/1.2;

if(valf<0)Serial.print("> 65535");
else Serial.print((int)valf,DEC);

Serial.println(" lx");

}
delay(1000);


}

void BH1750_Init(int address){

Wire.beginTransmission(address);
Wire.write(0x10); // 1 [lux] aufloesung
Wire.endTransmission();
}

byte BH1750_Read(int address){

byte i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()){
buff[i] = Wire.read();
i++;
}
Wire.endTransmission();
return i;
}