Welcome back to my blog! This week's project is about serial communication on ESP32, especially I2C. We'll try connecting ESP32 with the BMP280 sensor and OLED Display by this serial communication protocol.
I2C communication protocol uses two wires to share information, one for clock signal (SCL) and one for sending and receiving data (SDA). To connect devices with I2C, you have to know the addresses of its slaves (the devices controlled). It can be usually found on the component's datasheet.
To connect multiple I2C devices, you just have to connect both peripherals to the ESP32 SCL and SDA lines and refer to each peripheral by its address in the code.
For this project, we'll try to connect BMP280 sensor and OLED Display.
These are the components you need to prepare before starting:
1. ESP32
2. OLED Display
3. BMP280
4. Breadboard
5. Male-to-male jumpers
6. Micro USB
7. Laptop/PC
Before starting, make sure you've installed the required software like Arduino IDE and the libraries for OLED Display and BMP280.
The libraries you should prepare are:
1. Adafruit SSD1306
2. Adafruit GFX Library
3. Adafruit BMP280
I also installed some other libraries the pop-ups showed when installing the required libraries. If you look at my library manager, it looks like this.
After you're done installing the libraries, let's arrange the circuit! My circuit looks like this.
Now moving on to the laptop:
1. Open Arduino IDE.
2. Copy this code
/*
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() {
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(1.9);
display.setCursor(0,10);
display.print(String(bmp.readTemperature()));
display.print(" ");
display.setTextSize(0.9);
display.cp437(true);
display.write(167);
display.setTextSize(1.9);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Pressure: ");
display.setTextSize(1.9);
display.setCursor(0, 45);
display.print(String(bmp.readPressure()));
display.print(" hPa");
display.display();
delay(1000);
}
As you can see, the I2C address for BMP280 is 0x76, and for OLED Display is 0x3C.
3. Connect the laptop/PC with the ESP32 board.
4. Upload the code
5. Yay! It's done
That's all! Thank you for reading and hope this could help :D
Comments
Post a Comment