| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
|---|
This lab explores the use of the I2C (Inter-Integrated Circuit) serial communication bus. First introduced in 1982 by Philips Semiconductors, it is widely used for connecting peripherals to microcontrollers when data rates are relatively low. I2C uses only two bidirectional data lines: one for data (SDA) and one for a clock (SCL). The SDA and SCL lines are active low, so they should be pulled up with resistors (these are typically included on sensor modules). The standard I2C bus speed is 100 kbit/s and the fast mode is 400 kbit/s. The reference design utilizes a 7-bit address space. Multiple devices can share a single bus, with one master devices connecting with one or more slaves. Slaves on the same bus must use different addresses. The ESP32-S3 has two I2C controllers built in, allowing two separate buses to operate. Other background information on the standard can be found here.
The provided example code is derived from several other examples, allowing two I2C sensor modules and an I2C OLED display to be used. These are:
GYBMEP This sensor module includes a BME280 environmental sensor that can measure temperature, humidity, and temperature. The module supports two different I2C slave addresses, allowing up to 2 sensors to be used on the same bus. As shipped, the default address is 0x76. A trace between pads on the board next to the BME280 chip grounds the SDO pin. To change the I2C address to 0x77, cut the trace connecting the two pads and solder the other two pads together to pull the SDO pin high. Further details (focused on an Arduino implementation) may be found here. The example code is based on this code.
GY-2561 This sensor module includes a TSL2561 luminosity sensor to measure light intensity in the visible and near-infrared region of the electromagnetic spectrum. The TSL2561 supports three possible slave I2C addresses: 0x29, 0x39, or 0x49. This means you can have up to three TSL2561s attached to a single I2C bus. The address used by the part is determined by the solder jumper labeled "ADDR". When there is no solder on the jumper, the TSL2561 will use the default address of 0x39. Additional information about the TSL2561 is contained in this page for a Sparkfun module. The example code is based on this code.
SSD1306 This module incorporates a 128 x 64 pixel OLED display and an I2C display controller. The default slave address is 0x3C, as the OLED address select pins marked 0x78 have been connected. The address can be changed to 0x3D by moving the 4.7 kΩ resistor to the pins marked 0x7A. Further details (for an Arduino implementation) may be found here and here. The example code is based on this code.
Note that in the example code, the sensors operate on one I2C bus (0) and the OLED display on another (1). While it is possible to run all devices on the same bus, the I2C master initialization code for the sensors and display are incompatible as written.
Before project configuration and build, be sure to set the correct chip target using idf.py set-target esp32-s3.
- An ESP32-S3-DevKitC-1 development board
- A USB cable for microcontroller power supply and programming
- SSD1306 128x64 pixel I2C OLED display
- GYBMEP BME280 I2C digital temperature/humidity/pressure sensor
- GY-2561 TSL2561 I2C digital luminosity sensor
- A solderless breadboard
- Jumper or Dupont wires (M/M, M/F) to connect components
Additional hardware used for lab exercise:
- NO pushbutton
Run idf.py -p PORT flash monitor to build, flash and monitor the project.
(To exit the serial monitor, type Ctrl-].)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
Alternatively, this project can be developed using VSCode with the PlatformIO IDE extension and the Espressif 32 platform installed.
I (287) app_start: Starting scheduler on CPU0
I (292) app_start: Starting scheduler on CPU1
I (292) main_task: Started on CPU0
I (302) main_task: Calling app_main()
I (312) lab3: Found TSL2561 in package T/FN/CL
I (312) lab3: Initialize I2C 1
I (312) lab3: CONFIG_SDA_GPIO=11
I (322) lab3: BMP280: found BME280
I (322) lab3: CONFIG_SCL_GPIO=12
I (322) main_task: Returned from app_main()
I (322) lab3: Initialize 128x64 SSD1306 display
I (342) SSD1306: OLED configured successfully
I (1312) lab3: Illuminance: 123 Lux, Pressure: 98697.06 Pa, Temperature: 20.47 C, Humidity: 35.45
I (2312) lab3: Illuminance: 123 Lux, Pressure: 98693.03 Pa, Temperature: 20.47 C, Humidity: 35.29
I (3312) lab3: Illuminance: 122 Lux, Pressure: 98693.70 Pa, Temperature: 20.47 C, Humidity: 35.14
I (4312) lab3: Illuminance: 123 Lux, Pressure: 98694.14 Pa, Temperature: 20.48 C, Humidity: 34.97
Load the example code and confirm that the sensors and OLED display are working properly. Use the IDF Monitor or Serial Studio to observe the values from the sensors and confirm that they change as expected. Also confirm that the test display on the OLED display is working. Once the example is working properly, modify the example code to add the following functionality:
-
Display the MME 9654 Lab 3 startup screen on the OLED display for 3 seconds upon power up or reset. Then use the OLED to display the sensor readings. The display should alternate between displaying the current temperature (ºC), humidity (%), and pressure (kPa) from the BME280 sensor, and the current illuminosity reading (lux) from the TSL2561 sensor. The data from each sensor should display for 2 seconds before switching. Note that since the sensor data is being displayed on the screen, you may choose to stop sending sensor data to the serial monitor—it is your choice.

-
Add a pushbutton to the circuit, nominally connected to GPIO10 (you may choose to use a different pin). After startup, the pushbutton should change the operating mode with each press, cycling back from 3 to 1.
| Operating Mode | Description |
|---|---|
| 1 | BME280 data only |
| 2 | TSL2561 data only |
| 3 | Toggle BME280 and TSL2561 data |
-
Add code that, after startup, can detect when one or both sensors has become disconnected. The display should change to indicate that an error has occurred and provide information about which sensor(s) are disconnected.

-
Use the addressable Smart LED that is integrated into the ESP32-S3-DevKitC-1 board (connected to GPIO48) to indicate the current display mode. The LED should remain on unless the system is operating in Mode 3 (toggle data display), in which case it should blink on and off while still showing the colour associated with the display mode. You may use code that is similar to what you used for Lab 2 to drive the addressable LED strip, although it may be simpler to use the minimal RMT-based code in the ESP-IDF Blink example.
| Display Mode | Colour |
|---|---|
| Startup | White |
| BME280 | Green |
| TSL2561 | Cyan |
| Alert | Red |
- Correct the relative air pressure reading to account for current altitude. See information here.
- Add a potentiometer or second pushbutton to control the toggle rate of the display.
- Explore the use of other display patterns and/or graphic elements to improve the way that sensor data is presented on the OLED display.
- Update the SSD1306 I2C initialization code to allow all devices to operate on a single I2C bus. As a starting point, see the information and examples here.
