These Two I2C Components

Andreana Hartadi Suliman
5 min readMar 20, 2022

HI! Back again with ESP-32 series tutorial after two weeks of exams X_x

This time I will combine the uses of the BMP280 sensor and OLED. The purpose of these steps is to display the pressure and temperature read by BMP280 into the OLED display. So, what are you waiting for?

Prepare these things first.

  1. ESP32

2. Breadboard

3. Micro USB Cable

4. OLED display

5. BMP280

6. Male to Male Cable

Assemble the component that you need on the breadboard.

put the ESP-32, BMP280, and OLED on the breadboard.

Set up the male-to-male jumper on the ESP-32: Attach the male-to-male jumper cable to the 3V3 pin of the ESP-32 and connect it to the positive pole of the breadboard. Attach the male-to-male jumper cable to the GND pin of the ESP-32 and connect it to the negative pole of the breadboard. Attach the male-to-male jumper cable to pin D21 of the ESP-32 and connect it to one of the breadboard pins on the left of the BMP280 and the OLED. Install the male-to-male jumper cable on pin D22 ESP-32 and connect it to the right of the previous jumper cable parallel to the horizontal.

Set up the male-to-male jumper on the BMP280: Install the male-to-male jumper cable on the BMP280 VCC pin and connect it to the positive pole of the breadboard. Attach the male-to-male jumper cable to the GND pin and connect it to the negative pole of the breadboard. Install the male-to-male jumper cable on the SCL BMP280 pin and connect it vertically parallel to the jumper cable connected to the D22 pin of the ESP-32. Install the male-to-male jumper cable on the SDA BMP280 pin and connect it vertically parallel to the jumper cable connected to the D21 pin of the ESP-32.

Set up the male-to-male jumper on the OLED: Attach the male-to-male jumper cable to the OLED GND pin and connect it to the negative pole of the breadboard. Attach the male-to-male jumper cable to the VCC OLED pin and connect it to the positive pole of the breadboard. Install the male-to-male jumper cable on the SCK OLED pin and connect it vertically parallel to the jumper cable connected to the D22 ESP-32 pin and the jumper cable connected to the BMP280 SCL pin. Install the male-to-male jumper cable on the SDA OLED pin and connect it vertically parallel to the jumper cable connected to the D21 pin of the ESP-32 and the jumper cable connected to the BMP280 SDA pin.

It will look like this.

Open the Arduino application. Of course, we need some additional libraries, but because in previous tutorials we have used BMP280 and OLED, we don't need to install additional libraries. If you haven't seen the previous tutorial, we need the Adafruit BMP280 Library and the Adafruit SSD1306 library. Create a new file by pressing CTRL+N on the keyboard. After that enter the code as follows.

/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-i2c-communication-arduino-ide/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#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
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

bool status = bmp.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}

delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
delay(5000);
//read temperature and humidity
float t = bmp.readTemperature();
float h = bmp.readPressure();
if (isnan(h) || 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(30,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(30, 45);
display.print(h/1000);
display.print("kPa");

display.display();
}

Make sure the board and port are correct. Verify the sketch to check whether there are still parts that have errors or not. Upload the sketch to the ESP-32 so that the program can run.

Check the display on the OLED, the OLED display should present temperature and pressure information that is read on the BMP280 sensor. If in your opinion, the display is a little sloppy, you want to change the color, and the text is cut off, you can change the section:

display.setTextColor(WHITE); (change text color)
display.setTextSize(1); (change text size)
display.setCursor(0,8); (changes the position of the text on the OLED)

The result should be like this :

Thanks for following this tutorial! Hope you enjoy it, also don’t be shy to tell me if something’s wrong because I appreciate any feedback.

--

--

Andreana Hartadi Suliman

If we are afraid of failure, it means we have limited our ability. — Hi, nice to see you here. Have a good day!