Multiple I2C Devices using ESP32
Hello, Dear Reader!
In today's article, I will be showing how to setup multiple devices with I2C communication protocol using ESP32 development board. It requires knowledge about I2C so make sure to find some information about the communication protocol.
I2C Communication Protocol
I2C (Inter-Integrated Circuit) is one of the serial communication protocol beside SPI and UART. It is a two line communication between modules which using SDA (Serial Data Line) and SCL (Serial Clock Line). It uses 2 pin which are GPIO 21 (SDA) and GPIO 22 (SCL) in ESP32. Even with only 2 pins, it can control 2 or more devices as long as the devices doesn't have same address.
STEP 1: Required Hardware and Software
1. ESP32 Development Board,
2. PC (with Arduino IDE),
3. OLED display (I2C version),
4. BMP280 sensor,
5. Jumper wires,
6. Breadboard
I will be using BMP280 and OLED display for both I2C devices. They have different address (0x3C for OLED and 0x76 or 0x77 for BMP280) so both devices can be used together.
STEP 2: Assembling the Circuit
BMP280 have 6 pins but only 4 pins will be used. Those are SDA (GPIO 21), SCL (GPIO 22), VCC (3v3), and GND (GND). The OLED also have 4 pins respectively. The draw of the circuit is shown below.
STEP 3: Source Code
Before making the source code, there are few libraries that need to be installed. Go to Sketch > Include Libraries > Manage Libraries and search these libraries and install them. The libraries are "Adafruit BMP280 Library", "Adafruit GFX Library", and "Adafruit SSD1306".
We initiate the source code by including all the libraries we need, make sure to include Wire.h for I2C devices. Then define the screen width and height for the OLED. This OLED have 0x3C address and OLED reset for -1 (using multiple devices).
For the loop, clear the display of the OLED (for each loop so it can display new one). Then initiate the value of each parameter by using BMP reading function. Then set the display settings as shown above. Make sure to display all of it by using "display.display()" function. Delay the loop for 2000 ms.
STEP 3: Testing
After the code is done, verify and upload the code into ESP32. The code will successfully uploaded when there are the reading on the OLED display as shown in video below.
Comments
Post a Comment