Measuring Temperature, Humidity with the DHT11 and Pi Pico

In this tutorial we will be using the Raspberry Pi Pico, a DHT11 sensor and a simple button switch. We will program the Raspberry Pi Pico to read the values of humidity, temperature from the DHT11 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 DHT11: The DHT11 is a basic, ultra 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 (this does not require any analog input pins). 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. Compared to the DHT22, the DHT11 is less precise, less accurate, and works in a smaller range of temperature/humidity, but its smaller and less expensive

DHT11 Temperature, Humidity Sensor

Here are some of the specifications of the DHT11 sensor –

  1. DHT11 is 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 20-80% humidity readings with 5% accuracy
  5. Good for 0-50°C temperature readings ±2°C accuracy
  6. No more than 1 Hz sampling rate (once every second)
  7. Body size 15.5mm x 12mm x 5.5mm
  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. DHT11
  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. DHT11 Vcc connected to Vcc or 3V3 OUT
  2. DHT11 GND (Ground) connected to GND
  3. DHT11 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 DHT11 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 DHT11 sensor
d = dht.DHT11(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 DHT11 module and the Raspberry Pi Pico. In this module we learned how to setup the DHT11 sensor, connect each of the pins on the DHT11 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.