Intro to Embedded System with ESP32 [Serial Communication]
Hi, I’m back again, in this blog I’ll share my experience with serial communication of ESP32 as I told you in the sensors project.
Data communications are used for exchanging data with other devices. These communications in ESP32 are available through the GPIO pins. The communication used in this project is serial communication which is used primarily to transfer information between peripherals. To ensure the communication runs well needs a protocol between the host and sender.
Some terminologies of serial communication that rule by the protocol include transmission modes, clock synchronization, baud rate, framing, bit synchronization, and error control.
There are synchronous serial protocols like SPI, I2C, CAN, and LIN and almost every microcontroller has one Universal Asynchronous Receiver Transmitter (UART) for serial communication. In this project, I’ll utilize multiple I2C protocols to build the project.
— Component Required
- LCD 16 x 2
- ESP32
- BMP280
- Jumper wires
- Breadboard
Here are the pins references for LCD and BMP280.
Here’s the schematic I’ll use to build this project and make sure the pins are properly connected.
— Code
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup() {
Serial.begin(9600);
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin(0x76);
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
lcd.init();
lcd.backlight();
}
void loop() {
float temp = bmp.readTemperature();
float press = bmp.readPressure();
Serial.print(F("Temperature = "));
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(temp);
lcd.print("C");
Serial.print(temp);
Serial.println(" *C");
Serial.print(F("Pressure = "));
lcd.setCursor(0,1);
lcd.print("Pres:");
lcd.print(press);
lcd.print("Pa");
Serial.print(press);
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
delay(2000);
}
The code I use in this project has already been explained in the last 2 projects. You can look at the last 2 projects here:
After running the code, the LCD will be displaying the realtime temperature and the pressure.
By following the step above you’ve already finished this simple project about ESP32 Serial Communication. Congrats 🎉. This is simply a simple experiment with ESP32 Serial Communication; you may try more complex circuits and programs.
This blog can be helpful for those who are learning and getting started using ESP32. That’s all for this project, stay tuned for the next experiment with ESP32 !!