The Pico In MicroPython: A PIO Driver For The DHT22
Written by Harry Fairhead & Mike James   
Monday, 17 May 2021
Article Index
The Pico In MicroPython: A PIO Driver For The DHT22
Complete Listing

Complete Listing

import rp2
from machine import Pin
         autopush=True, in_shiftdir=rp2.PIO.SHIFT_LEFT)
def dht22():
    set(pins, 0)
    mov(x, osr)
    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)
    wait(1, pin, 0) [25]
    in_(pins, 1)
    wait(0, pin, 0)
    jmp(y_dec, "bits")
    set(y, 7)
    wait(1, pin, 0)[25]
    in_(pins, 1)
    wait(0, pin, 0)
    jmp(y_dec, "check")
class DHT22():
    def __init__(self, gpio): = rp2.StateMachine(0, dht22, freq=490196,
in_base=Pin(gpio), set_base=Pin(gpio),
jmp_pin=Pin(gpio)) def getReading(self): data=0 data = byte1 = (data >> 24 & 0xFF) byte2 = (data >> 16 & 0xFF) byte3 = (data >> 8 & 0xFF) byte4 = (data & 0xFF) checksum = & 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)


  • 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 Second Edition

By Harry Fairhead & Mike James


Buy from Amazon.


  • 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 ***NEW!
  • 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 Asyncio And Servers
  • Chapter 19 Direct To The Hardware
             Extract: Direct To The Hardware

Also of interest:

Raspberry Pico File System






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.


Google Rebrands Bard With Subscription

Google Bard will from now on be known as Gemini, and Google is launching a paid subscription tier that will be based on yet another new AI model from Google. The move to a paid subscription follows th [ ... ]

OpenSilver 2.1 Adds F# Support

OpenSilver 2.1 has been released with improvements including support for F#. The open-source alternative to Silverlight is capable of running large, complex legacy applications, as well as newly writt [ ... ]

More News

raspberry pi books



or email your comment to:


Last Updated ( Monday, 17 May 2021 )