Project 6 : Serial Communication

Gresya Leman
3 min readMar 21, 2022

Hello everyone!
Welcome to the sixth chapter of me trying to learn how to use ESP32. In this chapter, I’ll show you how I experimented with two I2C devices (BMP280 sensor and OLED display). This chapter is basically a combination of 2 of my latest projects, I already attached the links, so feel free to check out those projects.

Necessary components

Here are the components I used when doing this project :

ESP32+Breadboard (1 each) — Here I used a longer breadboard, feel free adjust it to your needs
Micro USB Cable (1 pcs)
Laptop/PC (1 pcs)
Male-to-Male Jumper cable (8 pcs)
OLED display (1pcs)
BMP280 sensor module (1 pcs)

Arrangements on the breadboard

Make sure the ESP32 is secured on the breadboard. Then check carefully the names of the leg on the BMP280 sensor and the OLED display. Connect it to the compatible ports on the ESP32. Here, I connected it as the table and picture below.

Where to connect from ESP32 to BMP280 sensor and OLED display

The compatible ports are connected by jumper cables by connecting them in parallel to each other using the breadboard and jumper cables.

Schematic (https://randomnerdtutorials.com/)
My final arrangements on my breadboard

Code the program

After the arrangements in the circuit is completed, open the Arduino IDE.

Since I’ve experimented with both components, I didn’t need to install any more libraries, but if you haven’t, please kindly check on my other projects to make sure you’ve already installed the necessary libraries.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);Adafruit_BMP280 bmp; // I2Cvoid setup() {
// put your setup code here, to run once:
Serial.begin(115200);
bool status = bmp.begin(0x76);
if(!status){
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while(1);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
}
void loop() {
delay(2000);

// put your main code here, to run repeatedly:
float temp = bmp.readTemperature();
float pres = bmp.readPressure();
float alti = bmp.readAltitude(1013.25);
if(isnan(temp) || isnan(pres) || isnan(alti)){
Serial.println("Failed to read from bmp sensor!");
}
display.clearDisplay();display.setCursor(0,0);
display.print("Temperature: ");
display.print(temp);
display.print(" ");
display.cp437(true);
display.write(248);
display.println("C");
display.setCursor(0, 25);
display.print("Pressure: ");
display.print(pres/1000);
display.print(" kPa");
display.display();
}

Compile the code and wait until it says ‘Done compiling’.

Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.

Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.

Now the result should show as the video attached below.

The video shows that when I put my index finger on the sensor, the temperature jumped a little bit, I suppose the sensor did work didn’t it?

I didn’t face any signi0ficant struggles when doing this project, but I did panicked the first time I tried it and the OLED wouldn’t turn on, but the I redid the wiring and then the OLED turned on. This was such a simple experiment but it made me realize that the possibility to use ESP32 is endless.

This is the end of the tutorial and I hope this helps!

See you in my next projects!

--

--