write a command in thonny for reading humidity, pressure, and temperature. with dht22…
- User
write a command in thonny for reading humidity, pressure, and temperature. with dht22 and bme280 raspberry pi pico
- Assistant
write a command in thonny for reading humidity, pressure, and temperature. with dht22 and bme280 raspberry pi pico
Certainly! Below is a MicroPython script that you can run in Thonny on your Raspberry Pi Pico to read humidity and temperature from both the DHT22 and BME280 sensors, as well as pressure from the BME280.
DHT22 Sensor:
BME280 Sensor:
Libraries Needed:
dht: For interacting with the DHT22 sensor.bme280: For the BME280 sensor. If you don’t have this library, you can download it from MicroPython's repository and upload it to your Pico using Thonny.Install the BME280 Library:
bme280.py file from the link above.bme280.py to your Pico.import machine
import dht
import bme280
import time
# Initialize DHT22 sensor on GP15
dht_pin = machine.Pin(15)
dht_sensor = dht.DHT22(dht_pin)
# Initialize I2C bus for BME280
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=100000)
# Initialize BME280
try:
bme = bme280.BME280(i2c=i2c)
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:
temp, pressure, hum = bme.read_compensated_data()
# The read_compensated_data() might return values scaled by 100 or 256, adjust accordingly
# Assuming they are scaled integers
temp_c = temp / 100
pressure_hpa = pressure / 256 # Typically, pressure is in Pascals
hum_perc = hum / 1024
return temp_c, pressure_hpa, hum_perc
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 read
Imports:
machine: For accessing hardware like GPIO and I2C.dht: To interface with the DHT22 sensor.bme280: To interface with the BME280 sensor.time: For adding delays between sensor readings.DHT22 Initialization:
BME280 Initialization:
bme280.BME280 class is instantiated with the I2C bus. Error handling is added in case the sensor is not found or fails to initialize.Reading Functions:
read_dht22(): Measures temperature and humidity from the DHT22. Returns None if there's an error.read_bme280(): Reads temperature, pressure, and humidity from the BME280. The raw data is scaled to human-readable units. Adjust the scaling factors (/100, /256, /1024) based on your specific bme280 library implementation.Main Loop:
Library Compatibility: Ensure that the bme280.py library you’re using matches the methods used in the code. Some BME280 libraries might return data in different units or require different scaling.
Pull-up Resistor for DHT22: It's essential to have a pull-up resistor (typically 4.7kΩ) on the data line of the DHT22 to ensure reliable data transmission.
Sensor Addresses: If you have multiple I2C devices, ensure that the BME280’s I2C address doesn't conflict with others. The default address is usually 0x76 or 0x77.
Error Handling: The code includes basic error handling. Depending on your application, you might want to add more robust error management or retries.
bme280.py library to your Pico if you haven't already.This setup allows you to monitor environmental conditions using both the DHT22 and BME280 sensors, providing redundancy or additional data points for humidity and temperature, along with pressure readings from the BME280.
Not yet included in a published release.