Temperature and Humidity Measurement with MicroPython on Raspberry Pi Pico

With this code we can read temperature and humidity from DHT11 sensor and change the unit of temperature from degree centigrade to Fahrenheit and vice versa using a push button. This code displays the values of temperature and humidity using an LCD display. An RGB LED is also used to declare the range of humidity.

Requirements: Raspberry Pi Pico board, push button, RGB LED, 220 Ohms resistors (3 numbers), DHT11 sensor, LCD1602 display. LCD1602 library available here, and you can save it on Raspberry Pi Pico board and copy and paste the following code on a empty python script and save it on the board as a python file. These two files must be in the same directory. According to the library, SDA and SCL pins of lcd1602 display must be connected to the GP6 and GP7 on raspberry pi pico, otherwise we need to change their definition in library. Display requires 5 volts, so VCC must be connected to VBUS on raspberry pi pico, and ground to ground.

According to the library, SDA and SCL pins of lcd1602 display must be connected to the GP6 and GP7 on raspberry pi pico, otherwise we need to change their definition in library. Display requires 5 volts, so VCC must be connected to VBUS on raspberry pi pico, and ground to ground. VCC pin of DHT11 is connected to 3.3 volt on raspberry pi pico and its data pin to GP16, and ground to ground. We don’t need NC pin of this sensor for this project. One leg of push button is connected to GP17 and the other one to the ground. Red, green, blue legs of RGB LED are also connected to GP13, GP14, GP15 respectively through 220 Ohms resistors, and its ground leg to the ground on raspberry pi pico.

After running the program, you can get temperature and humidity on desktop and LCD display as shown in this video:

Micropython code for this project is as follows:

# DHT11 Temperature and Humidity Sensor with different units for temperature displayed on LCD display and LED indicator for humidity.
# Sensor reads temperature and humidity, and by the use of a push button we can toggle temperature display between Celsius and Fahrenheit.

from machine import Pin
import utime as time # imports time from utime
from dht import DHT11 # imports DHT11 method from dht library
from lcd1602 import LCD # imports LCD method from lcd1602 library

pushButton = 17
dataPin = 16
RedLED = 13
GreenLED = 14
BlueLED = 15
data = Pin(dataPin, Pin.OUT, Pin.PULL_DOWN) # Using pin method, defines dataPin as output with pull_down resistor enabled
Sensor = DHT11 (data) # Sensor object
time.sleep(1)

ToggleButton = Pin(pushButton,Pin.IN,Pin.PULL_UP) # defines pushButton as input object

Temp= [0,0] # A two dimensional array for storing temperature in two different units
TempUnit=['C','F'] # A two dimensional array for storing units of temperature

lcd=LCD() # LCD object

#Output LED objects:
RLED = Pin(RedLED, Pin.OUT)
GLED = Pin(GreenLED, Pin.OUT)
BLED = Pin(BlueLED, Pin.OUT)

#initializing LEDs states:
RLEDState = False
GLEDState = False
BLEDState = False
RLED.value (RLEDState)
GLED.value (GLEDState)
BLED.value (BLEDState)

CurrentTime = time.ticks_ms() # Current time in miliseconds

InterruptCount = 0
Index= 0 #Initial index of the Temp and TempUnit

# Interrupt Service Routine (ISR) or Interrupt Handler: here ISR toggles the Index while the CPU is interrupted by a push button:
def InterruptHandler(pin):
global CurrentTime, Index, InterruptCount
#Multiple callbacks are triggered just by one interrupt request. But we can compare new time with old time and shift Index based on it:
if ((time.ticks_ms()-CurrentTime) > 300):
CurrentTime = time.ticks_ms()
InterruptCount = InterruptCount + 1
Index=not Index # Swithing Index between 0 and 1
# print (Index)
print("\r" ,'Temperature=', Temp[Index],chr(176)+TempUnit[Index],' Humidity=',Humidity,'%', ' InterruptCount= ', InterruptCount, end=' ')
HumidityColor()
lcd.clear()
lcd.write(0,0,'Temp= '+str(Temp[Index])+chr(223)+str(TempUnit[Index]))
lcd.write(0,1,'Humidity='+str(Humidity)+'%')

ToggleButton.irq(trigger=Pin.IRQ_RISING, handler=InterruptHandler) # Triggers interrupt request from ToggleButton and calls handler,where interrupt is on rising edge (i.e. after releasing push button). It could be on falling edge either (when button is pressed and not released yet).

def HumidityColor(): # using RGB LED indicator for differing range of humidity
if (Humidity<= 50 ):
GLEDState = True
RLEDState = False
BLEDState = False
if (50<=Humidity<= 70 ):
GLEDState = False
RLEDState = False
BLEDState = True
if (Humidity>= 70 ):
GLEDState = False
RLEDState = True
BLEDState = False
RLED.value (RLEDState)
GLED.value (GLEDState)
BLED.value (BLEDState)

print('My Sensor Data')
while True:
Sensor.measure()
Temperature = Sensor.temperature()
Humidity = Sensor.humidity()
Temp[0] = Temperature
Temp[1] = round(Temperature*(9/5)+32)
print("\r" ,'Temperature=', Temp[Index],chr(176)+TempUnit[Index],' Humidity=',Humidity,'%', ' InterruptCount= ', InterruptCount, end=' ')
HumidityColor()
lcd.clear()
lcd.write(0,0,'Temp= '+str(Temp[Index])+chr(223)+str(TempUnit[Index]))
lcd.write(0,1,'Humidity= '+str(Humidity)+'%')
time.sleep(1)