Serial Communication ESP32

Pavita
3 min readMar 19, 2022

Hello everyone, today I will show you my sixth experience with ESP32. In this project, I will use OLED and BMP280 as atmospheric pressure, temperature and relative humidity sensor. Before we get into the tutorial, I will show you necessary items to build the system.

  1. ESP32

2. Micro USB Cable

3. Laptop/PC with Arduino IDE app installed

4. Male to male jumper cable

5. OLED

6. BMP280

After you have all of this, you can start the project.

This project is pretty simple, you just need to connect the BMP280 to ESP32 like this :

After that, you will need to connect the BMP280 with OLED, you just need to connect VCC in BMP280 with VCC in OLED, and the others follow the same rule. Your breadboard will look like this :

After you build your breadboard, you will need this code for running the system.

#include <Wire.h>
#include <SPI.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);
// inisialisasi alamat bmp280
bmp.begin(0x76);
// inisialisasi alamat OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed or couldn't find a valid bme280"));
for(;;);
}
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();
}

After you upload the code you can see the temperature and pressure in the OLED. This is the result:

So, this is the end of my experience with ESP32 serial communication. Thank you for your attention. In approx. one week, I will update my next experience with ESP32 :)

Pavita Andrea (18220014)

--

--