Measuring Temperature, Humidity with the DHT22 and Pi Pico

In this tutorial we will be using the Raspberry Pi Pico, a DHT22 sensor and a simple button switch. We will program the Raspberry Pi Pico to read the values of humidity, temperature from the DHT22 every time the push button switch is pressed. We will also program the Pi Pico so that the LED’s light up in a sequence after they display the temperature, humidity readings.

About the DHT22: The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (analog input pins are not required). It’s fairly simple to use but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old.

Simply connect the first pin on the left to 3-5V power, the second pin to your data input pin, and the rightmost pin to ground. Although it uses a single wire to send data it is not Dallas One Wire compatible! If you want multiple sensors, each one must have its own data pin.

DHT22 Temperature, Humidity Sensor

Here are some of the specifications of the DHT22 sensor –

  1. DHT22is a low cost, affordable sensor
  2. Can be powered from 3V to 5V DC
  3. Consumes 2.5mA max current use during conversion (while requesting data)
  4. Good for 0-100% humidity readings with 2-5% accuracy
  5. Good for -40 to 80°C temperature readings ±0.5°C accuracy
  6. No more than 0.5 Hz sampling rate (once every 2 seconds)
  7. Body size 15.1mm x 25mm x 7.7mm
  8. 4 pins with 0.1″ spacing

Raspberry Pi Pico – Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040, the flagship microcontroller chip designed by Raspberry Pi in the UK. From light displays and IoT devices to signage and manufacturing processes, Raspberry Pi Pico gives the maker the power to control countless home, hobby, and industrial projects. Programmable in C and MicroPython, the Pi Pico is adaptable to a vast range of applications and skill levels, and getting started is as easy as dragging and dropping a file. More experienced users can take advantage of Raspberry Pi Pico’s rich peripheral set, including SPI, I2C, and eight Programmable I/O (PIO) state machines for custom peripheral support.

Here’s the specifications (from Raspberry Pi Foundation website) of the Raspberry Pi Pico in case you are interested –

  1. 21 mm × 51 mm form factor
  2. RP2040 microcontroller chip designed by Raspberry Pi in the UK
  3. Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
  4. 264kB on-chip SRAM
  5. 2MB on-board QSPI flash
  6. 2.4GHz 802.11n wireless LAN (Raspberry Pi Pico W and WH only)
  7. 26 multifunction GPIO pins, including 3 analogue inputs
  8. 2 × UART, 2 × SPI controllers, 2 × I2C controllers, 16 × PWM channels
  9. 1 × USB 1.1 controller and PHY, with host and device support
  10. 8 × Programmable I/O (PIO) state machines for custom peripheral support
  11. Supported input power 1.8–5.5V DC
  12. Operating temperature -20°C to +85°C (Raspberry Pi Pico and Pico H); -20°C to +70°C (Raspberry Pi Pico W and Pico WH)
  13. Castellated module allows soldering direct to carrier boards (Raspberry Pi Pico and Pico W only)
  14. Drag-and-drop programming using mass storage over USB
  15. Low-power sleep and dormant modes
  16. Accurate on-chip clock
  17. Temperature sensor
  18. Accelerated integer and floating-point libraries on-chip

Pinout diagram for the Raspberry Pi Pico – To help you work out which pins are located where and what they are called you can refer to the pinout diagram for the Raspberry Pi Pico provided below.

Raspberry Pi Pico Pinout Diagram

Bill Of Material – Here’s the Bill Of Material for this tutorial.

  1. Raspberry Pi Pico
  2. DHT22
  3. Male to Male Dupont cables
  4. 800 point breadboard
  5. 10K resistor
  6. Push button switch

Circuit Diagram – Here’s the circuit you will need to put together. .

Raspberry Pi Pico with PiStop (3 LED’s, SMD resistors)

The above diagram was put together in Fritzing. The connections are to be made as follows –

  1. DHT22 Vcc connected to Vcc or 3V3 OUT
  2. DHT22 GND (Ground) connected to GND
  3. DHT22 Data pin connected to a 10K resistor, same end connected to GPIO Pin 2
  4. Other end of the 10K resistor connected to 3V3 OUT of 3V3 rail
  5. One pin of the push button switch going to 3V3 OUT or Vcc rail
  6. The second pin of the push button switch going to GPIO 14

The switch has been setup to work with GPIO 14. Every time you hit the switch the code will obtain a reading from the DHT22 and provide that information on the screen.

Code

Here’s the code for the tutorial that you’ll need to put together. You are encourage to not just copy the code, but rather ask yourself the meaning of each of the lines of code as you type them out and use Google where you need additional support.

import dht
import machine
from time import sleep
from machine import Pin     #importing relevant modules & classes
# Setting up the DHT22 sensor
d = dht.DHT22(machine.Pin(2))
# Setting up the push button for use
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
    if button.value() == 1:  
    	try:
		# Obtaining temperature, humidity measurement
        	d.measure()
		# Assigning temperature, humidity to variables
        	temp = d.temperature()
        	hum = d.humidity()
		# Printing variables to screen
        	print("                              ")
        	print("The temperature is - %3.1f DegC" %temp)
        	print("The current humidity is - %3.1f %%" %hum)
        	print("                              ")
        	sleep(3)
    	except OSError as e:
        	print('Failed to read sensor.')
  
        # Sleeping for 5s
        time.sleep(5)  
    else:
        print("Sleeping....")
        time.sleep(5)

Summary – We hope you’ve enjoyed this tutorial on the DHT22 module and the Raspberry Pi Pico. In this module we learned how to setup the DHT22 sensor, connect each of the pins on the DHT22 sensor to the Raspberry Pi Pico, connect up a push button switch and program it in Micropython to get it all working. Please drop us a note at trevor at hack2 dot live if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *