ESP32 with Multiple I2C Devices


Hello, future engineers!!

Welcome again to my blog!

My name is Muhamad Fariz Ramadhan from STI 2020 ITB. I made this blog to help you guys out about embedded system especially setting up ESP32 to have some conditions. At this time, I am going to show you how connecting ESP32 to BMP280 sensor and OLED for serial communication.

*DON'T YOU KNOW??*

Besides using serial monitor that served by Arduino UNO, we can see the information between data processing peripherals by Serial Communication Protocols, here are several types of it.

  1. Inter- Intergrated Circuit (I2C)
    Half duplex, serial data transmission used for short-distance between board, modles and peripherals. Uses 2 pins
  2. Serial Peripheral Interface (SPI)
    Full duplex, serial data transmission used for short-distance between devices. Uses 4 pins.
  3. Universal Asynchronous Receiver- Transmitter (UART)
    Asynchronous, serial data transmission between devices. Uses 2 pins.
In this tutorial, I'm going to show you how to connect I2C communication protocol. Here is the tutorial.

STEP 1 : Required Hardware

BMP280
source : elekrtolead.

OLED
source : Hallroad.org



1. ESP32 Development Board
2. Laptop / PC
3. Micro USB cable
4. BMP280 (soldered)
5. Motherboard
6. Jumper wires 
7. OLED (I'm using SSD1306)

STEP 2 : Required Software

1. Download and install Arduino IDE


You can download Arduino IDE from this link: https://www.arduino.cc/en/software and choose suitable version for your laptop / PC. After that, please install the app by following the instructions. You can check my other blog here for more information.

2. Open Arduino IDE and install BMP280 library
After Arduino get installed then we have to set up the app to suitable for BMP280
Go to Tools > Manage Libraries. Then, search "BMP280" and install the 2 of top; Adafruit and BMP280_DEV.


3.  Install BMP280 library
You can go to Tools > Manage Libraries, then search Adafruit SSD1306. Click Install > Install all. 

STEP 3 : Circuit time & Demonstration

1. Learn the concept
    Basically, we're going to connect only 4 of 6 BMP280's pin to ESP32, Here is the schematic illustration.


2. Set up the circuit
You just need to pair the pin name of BMP280 and on the LED for the circuit.
Here is mine.


3. Upload the code

Here is the code, you can copy it and upload to your Arduino IDE. 

#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
// 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 bme280
  bmp.begin(0x76);
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(); 
}

4. Results
Here is the results. If your circuit and code uploaded properly then the OLED will show the temperature and pressure of the environment.


Here is the video demonstration.


That's it. Thank you guys.
Semoga bermanfaat!!
- Fariz STI'20 

Komentar

Postingan populer dari blog ini

ESP32 Web Server : Turn on LED using WiFi from Your Mobile or Desktop

Displayed cute picture of cat with OLED Display on ESP32 + PWM