Read Data from I2C Sensor in MicroPython for ESP32 and ESP8266

In this project, you’ll learn how to Read Data from I2C Sensor in MicroPython for ESP32 and ESP8266, using a sensor like the BMP180, which measures temperature and pressure. I2C (Inter-Integrated Circuit) is a communication protocol used to connect low-speed peripherals to microcontrollers. This project will focus on I2C initialization and communication to interact with the sensor and retrieve data.

Purpose of the Project:

The purpose of this project is to help you:

  • Initialize I2C communication in MicroPython.
  • Read data from a sensor using the I2C protocol.
  • Understand how to work with I2C devices like the BMP180 to gather sensor readings.

Data Types and Variable Table for Reading Data from I2C Sensor in MicroPython

Data Type Variable Name Description
I2C i2c Represents the I2C object used to communicate with the sensor.
int temperature Stores the temperature reading retrieved from the sensor.
int pressure Stores the pressure reading retrieved from the BMP180 sensor.

Syntax Table for Reading Data from I2C Sensor in MicroPython

Operation Syntax Example
Initialize I2C i2c = I2C(scl=Pin(scl_pin), sda=Pin(sda_pin)) i2c = I2C(scl=Pin(22), sda=Pin(21))
Scan I2C Devices i2c.scan() i2c.scan()
Read Data from Sensor i2c.readfrom_mem(address, register, num_bytes) i2c.readfrom_mem(0x77, 0xF4, 2)

Required Components for Reading Data from I2C Sensor

  • ESP32 or ESP8266 board
  • BMP180 temperature and pressure sensor (or similar I2C sensor)
  • Breadboard
  • Jumper Wires

Circuit Diagram for Reading Data from I2C Sensor

         BMP180

         ——–

         |      |

         | VCC  |—--> 3.3V

         | GND  |—--> GND

         | SCL  |—--> GPIO22 (SCL)

         | SDA  |—--> GPIO21 (SDA)

         ——–

 

Circuit Connection Table

Component Pin ESP32/ESP8266 Pin Explanation
BMP180 VCC Connected to 3.3V 3.3V Powers the sensor.
BMP180 GND Connected to GND GND Ground connection for the sensor.
BMP180 SCL Connected to GPIO22 GPIO22 I2C clock line (SCL) for communication.
BMP180 SDA Connected to GPIO21 GPIO21 I2C data line (SDA) for communication.

I2C Communication Analysis:

I2C (Inter-Integrated Circuit) allows multiple devices (slaves) to communicate with a master device (like the ESP32/ESP8266) over just two lines: SCL (clock) and SDA (data). Each I2C device has a unique address, which the master uses to communicate with it. In this project, we’ll use the I2C protocol to communicate with the BMP180 sensor, read its temperature and pressure data, and display the results.

Writing the MicroPython Code for Reading Data from I2C Sensor:

Here’s a sample code to initialize I2C, read data from a BMP180 sensor, and display the temperature and pressure readings:

import machine

import time

import bmp180

 

# Initialize I2C on GPIO22 (SCL) and GPIO21 (SDA)

i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))

 

# Initialize BMP180 sensor

sensor = bmp180.BMP180(i2c)

sensor.oversample_sett = 2

sensor.sealevel = 101325  # Adjust to your local sea level pressure

 

# Function to read temperature and pressure

def read_sensor_data():

    temperature = sensor.temperature  # Read temperature

    pressure = sensor.pressure         # Read pressure

    altitude = sensor.altitude         # Calculate altitude (based on pressure)

    

    # Print sensor readings

    print(“Temperature: {:.2f} °C”.format(temperature))

    print(“Pressure: {:.2f} Pa”.format(pressure))

    print(“Altitude: {:.2f} meters”.format(altitude))

 

# Continuously read sensor data every 5 seconds

while True:

    read_sensor_data()

    time.sleep(5)

 

Running the Code and Checking the Output:

  1. Upload the code to your ESP32 or ESP8266 using Thonny or another MicroPython IDE.
  2. Ensure that the BMP180 sensor is correctly wired to the ESP32 or ESP8266.
  3. When the code runs, the temperature, pressure, and calculated altitude will be displayed in the console every 5 seconds.

Explanation of the Code:

  • I2C Initialization: The I2C object is initialized using GPIO22 as the clock (SCL) and GPIO21 as the data line (SDA).
  • BMP180 Sensor Initialization: The BMP180 sensor is initialized, and oversampling is set to improve accuracy.
  • Reading Sensor Data: The sensor.temperature and sensor.pressure methods are used to retrieve the temperature and pressure data, while sensor.altitude calculates the altitude based on the pressure.
  • Loop Function: The code continuously reads the sensor data every 5 seconds and prints it to the console.

Expanding the Project:

  1. Multiple I2C Devices: Add more I2C devices, such as a humidity sensor or an OLED display, to the same I2C bus and retrieve data from multiple sensors.
  2. Data Logging: Store the temperature and pressure data in a file for long-term monitoring and analysis.
  3. Alerts: Implement an alert system that triggers a buzzer or LED when the temperature or pressure crosses certain thresholds.

Common Problems and Solutions for Reading Data from I2C Sensor:

Problem Solution
Sensor not detected by I2C scan Ensure the correct SCL and SDA pins are used and verify the wiring.
Incorrect or no sensor readings Check the I2C address of the sensor and make sure the sensor is powered correctly.
Data reading inconsistent Increase the oversampling setting on the BMP180 for more stable readings.

FAQ for Reading Data from I2C Sensor:

Q: What is the I2C address of the BMP180 sensor?
A: The default I2C address of the BMP180 sensor is 0x77. You can use i2c.scan() to detect the sensor’s address on the I2C bus.

Q: Can I use other sensors with this code?
A: Yes, you can modify the code to work with any I2C sensor by adjusting the initialization and data reading methods specific to that sensor.

Q: How do I handle multiple I2C devices?
A: I2C supports multiple devices on the same bus. You can connect additional sensors to the same SCL and SDA lines and communicate with each device using its unique address.

Conclusion:

In this project, you learned how to read data from an I2C sensor in MicroPython for ESP32 and ESP8266. By understanding how to initialize I2C and communicate with sensors like the BMP180, you can gather temperature and pressure data for various applications, such as weather stations or environmental monitoring systems.