Embedded System Project 6: ESP32 I2C Communication

Fernaldi Fauzie
4 min readMar 4, 2021

Yay! This time, we will go further in experimenting with ESP32! More precisely, this time we will combine project 4 which is about ESP32 external sensor with project 5 which is about OLED display with ESP32! What does that mean? It means we will try to get sensor readings from BMP280 sensor (via I2C) and displays the results on an I2C OLED display!

My Simple Circuit

Let’s Begin!

Oh wait, before we go further, I would like to tell you guys that for this project I am using this randomnerdtutorials link as reference! Although, I will use BMP280 instead of BME280!

In this project, we will use:

  • ESP32 Board
  • BMP280 sensor
  • 0.96 inch OLED display
  • 8 male to male jumper wires
  • Micro USB cable
  • Breadboard

Keep in mind that, every I2C device has its own address, so it’s possible to have multiple I2C devices on the same bus. This time, because we will experimenting with BMP280 sensor with OLED display, although it’s in same bus, it’s different address.

To set it up, we have to:

  • Connect both peripherals to the ESP32 GPIO 22 (SCL) and GPIO 21 (SDA)
  • In the code, we should refer to each peripheral by its address

Unfortunately, I can’t find BMP280 with 6 pins with fritzing, so in my schematic diagram its pin are 4 instead of 6, but actually there is no major problem since all of 4 pins are available in BMP280 with 6 pins!

My schematic diagram is pretty similar with schematic diagram in this randomnerdtutorials, but here it’s BMP280 instead of BME280 and also some differences in wiring.

For the code, I’m using this randomnerdtutorials as reference!

/*********
Another Version of Rui Santos
*********/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_BMP280 bmp; // I2C
void setup() {
Serial.begin(115200);
bmp.begin(0x76); // Initialize I2C address of BMP280
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
delay(3000);

//read temperature and pressure
float t = bmp.readTemperature();
float p = bmp.readPressure();
if (isnan(p) || isnan(t)){
Serial.println("Failed to read from bmp sensor!");
}

// clear display
display.clearDisplay();

// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");

// display pressure
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Pressure: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(p/1000);
display.print(" kPa");

display.display();
}

The Result?

My Experimentation

Fortunately, it seems that my BMP280 and OLED can work well! Since the temperature that can be seen on OLED changed from 30.76 °C to 32.50 °C if I touch the BMP280 with my bare hand.

OLED is Displaying 0 °C and 0 kPa?

Probably some of you guys will experience this problem that happened before. First of all, you should try to troubleshooting 1 by 1, which means try to troubleshooting the BMP280 alone, then the OLED alone, the jumper wires alone, then the ESP32 alone!

In my case, apparently the problem is because BMP280 I2C address can’t be found! Thankfully, it can be solved by fixing the wiring, because apparently I followed wrong reference in wiring because that reference is for BME280, not BMP280. Actually, I realized before that it’s BME280 instead of BMP280 but I thought that there is no difference in wiring (which is actually different).

Yay! We did it!

In conclusion, because the BMP280 and the OLED have different addresses, we can use the same GPIO 22 (SCL) and GPIO 21 (SDA) without any problem (same bus, different addresses). With this, we can get sensor readings from BMP280 sensor (via I2C) and displays the results on an I2C OLED display just like we did before!

That’s all about this ESP32 I2C Communication story. At the end, I hope this blog can help you guys that reading this to give more inspiration and insight. Have fun and see you in the next story!

--

--