I²C communication issues with BMP280, AHT10 & BH1750

I have been coding Esp32 Devkit V1 & NodeMCU V3 and trying to get data from sensors connected to i²c bus of those two MCU separately. But both cases the sensors are not initializing. But I have tried these sensors on my Arduino uno R3 which worked fine. But When it comes to ESP32 and NodeMCU V3 i²c bus don't work. I have checked with the i²c code and it detects the sensors, but when I upload the original code, it doesn't work.

Here are few steps that I took to make things work

  1. Added 3k ohm external pull-up resistor on SDA and SCL
  2. Changed to default pins like 21, 22 in case of ESP32-Devkit and 4, 5 in case of NodeMCU V3 to other pins, doesn't work!
  3. Checked the wifi connection which works fine and sends -2 lux value to thingspeak. But other two BMP280 & AHT10 sensor doesn't work.
  4. Connected a led to check whether sensors are getting power or not. The led shows the sensors are getting power.
  5. The code for reference
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <WiFi.h>
#include <Adafruit_BME280.h>
#include "ThingSpeak.h"
#include <BH1750.h>
#include <BME280.h>
//#include <ESP8266WiFi.h>
#define SEALEVELPRESSURE_HPA (1012)

//bme280 doesn't give the data of humidity 
Adafruit_BME280 bme;
BH1750 lightMeter;




#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,-1);

unsigned long delaytime;
const int led1 = 32;
const int LED2 = 33;

const char* ssid = "anika1984G";
const char* password = "1234";
WiFiClient client;

void setup() {
  Serial.begin(9600);
  Wire.begin(21,22,400000);
  dht.begin();
  pinMode(led1,OUTPUT);
  pinMode(LED2,OUTPUT);
  bool status;
  status = bme.begin(0x76);
  if(!status){
    Serial.print("Couldn't find BME280 sensor");
    
  }
  lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE,0x23,&Wire);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
    Serial.println(F("SSD1306 Allocation Failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.startscrollright(0x00, 0x0F);
}

void loop() {
  
  
  float TempC = bme.readTemperature();
  float PressureHpa = bme.readPressure();
  float Altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

  delay(2000);
  //add pin connected to ground BH1750
  float lux = lightMeter.readLightLevel();
  if (t>27){
    digitalWrite(led1, HIGH);
    delay(2000);
    digitalWrite(led1, LOW);
    delay(2000);
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(40,10);
    display.print("Turn on Fan!");

    
  } else{
    digitalWrite(LED2, HIGH);
    delay(500);
    digitalWrite(LED2, LOW);
    delay(500);
    Serial.println("Condition Normal");
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(0,20);
    display.print("Temperature Ok!");
    display.display();
    

  }

  
  
  temSense(t);
  
  humSense(h);
  
  lightSense(lux);
  delay(2000);
  
  display.display(); 

}

void temSense(float t){
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print("*C\t");
  
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.setTextSize(1);
  display.setCursor(70,0);
  display.print(t);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(1);
  display.print("C");

}

void humSense(float h){
  // display humidity
  Serial.println("Humidity: ");
  Serial.print(h);
  Serial.print("%\t");
  display.setTextSize(1);
  display.setCursor(0, 10);
  display.print("Humidity: ");
  display.setTextSize(1);
  display.setCursor(70, 10);
  display.print(h);
  display.print("%"); 

}
 
void lightSense(float lux){
    //display Lightlevel
  Serial.print("Lux: ");
  Serial.print(lux);
  
  display.setTextSize(0.5);
  display.setCursor(0,20);
  display.print("Lux: ");
  display.setTextSize(1);
  display.setCursor(70,20);
  display.print(lux);
  display.print("lx");
  }

I moved your topic to a more appropriate forum category @mahadi1234.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

I assume this means that when you test individual I2C devices they work OK but when you try the complete system it fails??
how are you powering the I2C devices ? what voltage? you may be overloading the ESP32 module power supply
I have had SSD1306 working with a ESP32-DevKit-1 and a BMP280 working to a NodeMCU V3

1 Like

Yes, you're correct. Esp32 detects the sensor modules in I2C scan. For powering the sensor I used the 3v3 pin of ESP32, Voltage 3.3v . Also attached a led to check whether it getting power like the picture. Even though I coded for 2 sensor and OLED but only using one sensor to check till now. But no response till now.

test each device in turn using a separate program, e.g. to test the BMP280 using erriezbmx280 library

#include <Wire.h>
#include <ErriezBMX280.h>

#define SEA_LEVEL_PRESSURE_HPA      1026.25
ErriezBMX280 bmx280 = ErriezBMX280(0x76);

void setup()
{
    delay(500);
    Serial.begin(115200);
    while (!Serial) {
        ;
    }
    Serial.println(F("\nErriez BMP280/BMX280 example"));
    Wire.begin();
    Wire.setClock(400000);

    // Initialize sensor
    while (!bmx280.begin()) {
        Serial.println(F("Error: Could not detect sensor"));
        delay(3000);
    }
    
    Serial.print(F("\nSensor type: "));
    switch (bmx280.getChipID()) {
        case CHIP_ID_BMP280:
            Serial.println(F("BMP280\n"));
            break;
        case CHIP_ID_BME280:
            Serial.println(F("BME280\n"));
            break;
        default:
            Serial.println(F("Unknown\n"));
            break;
    }
    bmx280.setSampling(BMX280_MODE_NORMAL,    // SLEEP, FORCED, NORMAL
                       BMX280_SAMPLING_X16,   // Temp:  NONE, X1, X2, X4, X8, X16
                       BMX280_SAMPLING_X16,   // Press: NONE, X1, X2, X4, X8, X16
                       BMX280_SAMPLING_X16,   // Hum:   NONE, X1, X2, X4, X8, X16 (BME280)
                       BMX280_FILTER_X16,     // OFF, X2, X4, X8, X16
                       BMX280_STANDBY_MS_500);// 0_5, 10, 20, 62_5, 125, 250, 500, 1000
}

void loop()
{
    Serial.print(F("Temperature: "));
    Serial.print(bmx280.readTemperature());
    Serial.println(" C");

    if (bmx280.getChipID() == CHIP_ID_BME280) {
        Serial.print(F("Humidity:    "));
        Serial.print(bmx280.readHumidity());
        Serial.println(" %");
    }

    Serial.print(F("Pressure:    "));
    Serial.print(bmx280.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print(F("Altitude:    "));
    Serial.print(bmx280.readAltitude(SEA_LEVEL_PRESSURE_HPA));
    Serial.println(" m");

    Serial.println();

    delay(1000);
}

a run on an ESP32 gives

18:41:47.630 -> Erriez BMP280/BMX280 example
18:41:47.731 -> 
18:41:47.731 -> Sensor type: BMP280
18:41:47.731 -> 
18:41:47.731 -> Temperature: 22.31 C
18:41:47.731 -> Pressure:    1021.17 hPa
18:41:47.764 -> Altitude:    41.81 m
18:41:47.764 -> 
18:41:48.739 -> Temperature: 22.30 C
18:41:48.739 -> Pressure:    1021.07 hPa
18:41:48.739 -> Altitude:    42.68 m

also works for a BME280

18:47:06.421 -> Erriez BMP280/BMX280 example
18:47:06.559 -> 
18:47:06.559 -> Sensor type: BME280
18:47:06.559 -> 
18:47:06.559 -> Temperature: 22.18 C
18:47:06.559 -> Humidity:    46.69 %
18:47:06.559 -> Pressure:    1021.06 hPa
18:47:06.559 -> Altitude:    42.76 m
18:47:06.559 -> 
18:47:07.550 -> Temperature: 22.23 C
18:47:07.550 -> Humidity:    46.65 %
18:47:07.550 -> Pressure:    1021.05 hPa
18:47:07.550 -> Altitude:    42.84 m

remove the power to any other devices

1 Like

You have to solder the pins of the BH1750 module. As far as I know, there is no one with a working project with pins that are not soldered.

The led on the breadboard needs a resistor. A led may not be connected directly to 3.3V or 5V.

Are you powering the BH1750 module with 3.3V or 5V. If you power it with 3.3V and it goes through the voltage regulator, then the BH1750 could be running at 2.3V.

2 Likes

Yes those pins will need to be soldered.
Also what value of pull up resistors are you using and to what voltage?

There seems to be a lot of 100Ω resistors about?

1 Like

Resistors are 1k ohm each, 3k ohm for SDA and SCL each, for Voltage 3.3v. ...

I will try with external 5V then. I didn't solder yet cause it worked really ok with my Arduino uno r3 so far. But yes soldering is important no doubt, I will do.

I will try this library for my BMP280 sensor.
Thanks for this information.

Very important.
If you're lucky, those pins may make contact - but it's unreliable. Solder them and you have one potential point of failure less.

That BH1750 board has a component that looks like a linear regulator; it may also have separate pins for 3.3V and 5V power input. Make sure you use the correct connections.

1 Like

After soldering the sensors BMP280 i²c bus


initializing properly, didn't use any resistors! and sending data to the cloud!

Thanks everyone for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.