import machine import dht import bme280 import time Initialize DHT22 sensor on GP15…
- User
import machine import dht import bme280 import time
Initialize DHT22 sensor on GP15
dht_pin = machine.Pin(2) dht_sensor = dht.DHT22(dht_pin)
Initialize I2C bus for BME280
i2c_2 = machine.I2C(0, scl=machine.Pin(5), sda=machine.Pin(4), freq=100000)
Initialize BME280
try: # Specify the I2C address if known (commonly 0x76 or 0x77) bme = bme280.BME280(i2c=i2c_2, address=0x76) print("BME280 initialized successfully") except OSError as e: print("BME280 initialization failed:", e) bme = None
def read_dht22(): try: dht_sensor.measure() temp = dht_sensor.temperature() hum = dht_sensor.humidity() return temp, hum except OSError as e: print("Failed to read DHT22:", e) return None, None
def read_bme280(): if bme is None: return None, None, None try: temperature, pressure, humidity = bme.values # Adjust based on your bme280 library return temperature, pressure, humidity except OSError as e: print("Failed to read BME280:", e) return None, None, None
while True: # Read from DHT22 temp_dht, hum_dht = read_dht22()
# Read from BME280 temp_bme, pressure_bme, hum_bme = read_bme280() # Display the results print("---- Sensor Readings ----") if temp_dht is not None and hum_dht is not None: print("DHT22 Temperature: {:.2f} °C".format(temp_dht)) print("DHT22 Humidity: {:.2f} %".format(hum_dht)) else: print("DHT22: Read Error") if temp_bme is not None and pressure_bme is not None and hum_bme is not None: print("BME280 Temperature: {:.2f} °C".format(temp_bme)) print("BME280 Pressure: {:.2f} hPa".format(pressure_bme)) print("BME280 Humidity: {:.2f} %".format(hum_bme)) else: print("BME280: Read Error") print("-------------------------\n") time.sleep(2) # Wait for 2 seconds before next readMPY: soft reboot BME280 initialization failed: [Errno 5] EIO ---- Sensor Readings ---- DHT22 Temperature: 28.20 °C DHT22 Humidity: 63.50 % BME280: Read Error
- Assistant
The error message
BME280 initialization failed: [Errno 5] EIOsuggests there was an issue with the I2C communication between the microcontroller and the BME280 sensor. Here are some steps you can take to troubleshoot and resolve the issue:-
Check Wiring:
- Ensure that the I2C connections between the microcontroller and the BME280 sensor are correct.
- Double-check the connections for SDA (data line) and SCL (clock line). The pins should be connected as follows:
-
- User
MPY: soft reboot Traceback (most recent call last): File "", line 5, in File "/lib/bme280.py", line 153, in init File "/lib/bme280.py", line 159, in _load_calibration File "/lib/bme280.py", line 119, in readU16LE File "/lib/bme280.py", line 102, in readU16 OSError: [Errno 5] EIO
- Assistant
The
OSError: [Errno 5] EIOerror during the initialization of the BME280 sensor indicates an Input/Output error. This typically relates to issues with I2C communication. Here are steps to help diagnose and potentially resolve the problem:-
Verify I2C Communication:
-
- User
MPY: soft reboot Scanning I2C bus... I2C device found at address: ['0x76']
- Assistant
The I2C scanner successfully detected a device at address
0x76, which indicates that the BME280 sensor is physically connected and recognized on the bus. However, you are still encountering an during initialization. Here are further steps to address this: