====== lamaPLC: RP2040_ETH_Modul: Read BME 680/688 sensor data ======
The program reads the following data from the [[sensor:bmp_bme|BME680 sensor]] and displays it in the Modbus input registers:
* Temperature: Provides ambient temperature readings.
* Humidity: Measures relative humidity.
* Barometric Pressure: Can be used to calculate altitude or track weather changes.
* Gas (VOC): Features a heated metal-oxide sensor that detects Volatile Organic Compounds (VOCs) to estimate indoor air quality.
The program is also suitable for reading the **BME688 sensor**.
Important: The Ethernet module is accessible by **RP2040_ETH** via **UART1** with the following configuration:
//uart1 = UART(1, baudrate=115200, tx=Pin(20), rx=Pin(21), timeout=50)//
Required library: https://github.com/robert-hh/BME680-Micropython/blob/master/bme680.py
import machine
from machine import Pin, I2C
import time
from bme680 import BME680_I2C # Import the driver you saved earlier
# I2C configuration for RP2040-ETH
# Using I2C port 0, SDA on Pin 4, SCL on Pin 5, at 100kHz
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=100000)
# Initialize the sensor
# The default I2C address is usually 0x77 or 0x76
try:
bme = BME680_I2C(i2c, address=0x77)
print("BME688 initialized successfully.")
except Exception as e:
print("Could not find BME688. Check wiring or address (0x76/0x77).")
# Try 0x76 if 0x77 fails
bme = BME680_I2C(i2c, address=0x76)
def display_data():
print("\n--- BME688 Environment Data ---")
# Read and print data to Serial Monitor
print("Temperature: {:.2f} °C".format(bme.temperature))
print("Humidity: {:.2f} %".format(bme.humidity))
print("Pressure: {:.2f} hPa".format(bme.pressure))
print("Gas Res.: {:.2f} Ohms".format(bme.gas))
print("-------------------------------")
# Main loop
while True:
try:
display_data()
except Exception as e:
print(f"Error reading sensor: {e}")
time.sleep(2) # Wait 2 seconds between readings
**Output example**
--- BME688 Environment Data ---
Temperature: 22.03 °C
Humidity: 33.84 %
Pressure: 978.13 hPa
Gas Res.: 12946861.00 Ohms
{{tag>code! micropython 2026 RP2040_ETH BME680 i2c sensor communication}}
This page has been accessed for: Today: {{counter|today}}, Until now: {{counter|total}}