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.
Here are some of the specifications of the DHT22 sensor –
- DHT22is a low cost, affordable sensor
- Can be powered from 3V to 5V DC
- Consumes 2.5mA max current use during conversion (while requesting data)
- Good for 0-100% humidity readings with 2-5% accuracy
- Good for -40 to 80°C temperature readings ±0.5°C accuracy
- No more than 0.5 Hz sampling rate (once every 2 seconds)
- Body size 15.1mm x 25mm x 7.7mm
- 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 –
- 21 mm × 51 mm form factor
- RP2040 microcontroller chip designed by Raspberry Pi in the UK
- Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
- 264kB on-chip SRAM
- 2MB on-board QSPI flash
- 2.4GHz 802.11n wireless LAN (Raspberry Pi Pico W and WH only)
- 26 multifunction GPIO pins, including 3 analogue inputs
- 2 × UART, 2 × SPI controllers, 2 × I2C controllers, 16 × PWM channels
- 1 × USB 1.1 controller and PHY, with host and device support
- 8 × Programmable I/O (PIO) state machines for custom peripheral support
- Supported input power 1.8–5.5V DC
- 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)
- Castellated module allows soldering direct to carrier boards (Raspberry Pi Pico and Pico W only)
- Drag-and-drop programming using mass storage over USB
- Low-power sleep and dormant modes
- Accurate on-chip clock
- Temperature sensor
- 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.
Bill Of Material – Here’s the Bill Of Material for this tutorial.
- Raspberry Pi Pico
- Male to Male Dupont cables
- 800 point breadboard
- 10K resistor
- Push button switch
Circuit Diagram – Here’s the circuit you will need to put together. .
The above diagram was put together in Fritzing. The connections are to be made as follows –
- DHT22 Vcc connected to Vcc or 3V3 OUT
- DHT22 GND (Ground) connected to GND
- DHT22 Data pin connected to a 10K resistor, same end connected to GPIO Pin 2
- Other end of the 10K resistor connected to 3V3 OUT of 3V3 rail
- One pin of the push button switch going to 3V3 OUT or Vcc rail
- 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.
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.