Page 3 of 3
Complete Listing
import rp2
from machine import Pin
@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW,rp2.PIO.OUT_LOW),
autopush=True, in_shiftdir=rp2.PIO.SHIFT_LEFT)
def dht22():
wrap_target()
label("again")
pull(block)
set(pins, 0)
mov(x, osr)
label("loop1")
jmp(x_dec, "loop1")
set(pindirs, 0)
wait(1, pin, 0)
wait(0, pin, 0)
wait(1, pin, 0)
wait(0, pin, 0)
set(y, 31)
label("bits")
wait(1, pin, 0) [25]
in_(pins, 1)
wait(0, pin, 0)
jmp(y_dec, "bits")
set(y, 7)
label("check")
wait(1, pin, 0)[25]
set(pins,2)
set(pins,0)
in_(pins, 1)
wait(0, pin, 0)
jmp(y_dec, "check")
push(block)
wrap()
class DHT22():
def __init__(self, gpio):
self.sm = rp2.StateMachine(0, dht22, freq=490196, in_base=Pin(gpio), set_base=Pin(gpio), jmp_pin=Pin(gpio))
self.sm.active(1)
def getReading(self):
self.sm.put(500)
data=0
data = self.sm.get()
byte1 = (data >> 24 & 0xFF)
byte2 = (data >> 16 & 0xFF)
byte3 = (data >> 8 & 0xFF)
byte4 = (data & 0xFF)
checksum = self.sm.get() & 0xFF
self.checksum =
(checksum == (byte1+byte2+byte3+byte4) & 0xFF)
self.humidity = ((byte1 << 8) | byte2) / 10.0
neg = byte3 & 0x80
byte3 = byte3 & 0x7F
self.temperature = (byte3 << 8 | byte4) / 10.0
if neg > 0:
self.temperature = -self.temperature
dht = DHT22(2)
dht.getReading()
print("Checksum", dht.checksum)
print("Humidity= ", dht.humidity)
print("Temperature=", dht.temperature)
Summary
The DHT22 is a low-cost temperature and humidity sensor.
It uses a custom single wire bus which is not compatible with the 1-Wire bus.
Its asynchronous protocol is easy to implement directly in user space.
A very simple checksum is used to detect errors.
It is possible to implement the protocol as defined in the datasheet using a PIO by using counting loops to time each pulse.
A better use of the PIO is to notice that the protocol can be decoded by testing the state of the line a fixed time after the rising edge.
Programming the Raspberry Pi Pico/W In MicroPython Third Edition
By Harry Fairhead & Mike James
Buy from Amazon .
Contents
Preface
Chapter 1 The Raspberry Pi Pico – Before We Begin
Chapter 2 Getting Started
Chapter 3 Getting Started With The GPIO
Chapter 4 Simple Output
Chapter 5 Some Electronics
Chapter 6 Simple Input Extract: Simple Input ****
Chapter 7 Advanced Input – Events and Interrupts
Chapter 8 Pulse Width Modulation Extract: PWM ****
Chapter 9 Controlling Motors And Servos Extract: DC Motors ****
Chapter 10 Getting Started With The SPI Bus
Chapter 11 A-To-D and The SPI Bus ****
Chapter 12 Using The I2C Bus
Chapter 13 Using The PIO
Chapter 14 The DHT22 Sensor Implementing A Custom Protocol Extract: A PIO Driver For The DHT22 ****
Chapter 15 The 1‑Wire Bus And The DS1820
Chapter 16 The Serial Port
Chapter 17 Using The Pico W - WiFi Extract: HTTP Client ****
Chapter 18 Sockets Extract: Sockets ****
Chapter 19 Asyncio And Servers Extract: Asyncio Client ***NEW!!!
Chapter 20 Advanced Hardware
Chapter 21 Direct To The Hardware Extract: Direct To The Hardware ****
Also of interest:
Raspberry Pico File System
**** articles from the second edition not yet updated.
<ASIN:B0BR8LWYMZ>
<ASIN:B0BL1HS3QD>
To be informed about new articles on I Programmer, sign up for our weekly newsletter , subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin .
Corretto 25 Adds Ahead-Of-Time-Caching Support 25/09/2025
Amazon Corretto 25, a Long Term Support (LTS) version, is now generally available. This release brings Corretto into line with JDK 25 with support for compact object headers, ahead-of-time-cachin [ ... ]
Qodana Revisited 20/10/2025
It's been some years since we first looked at Qodana, the solid SAST tool from JetBrains. Let's find out what's new in its latest release, Qodana 2025.2.
More News
Comments
Make a Comment or View Existing Comments Using Disqus
or email your comment to: comments@i-programmer.info