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 In MicroPython

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 ***NEW!
  • 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 WiFi Using The ESP8266
  • Chapter 18 Direct To The Hardware
             Extract: Direct To The Hardware






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.


Lights, Camera, Sound - AI Improvements to Google Meet

Updates and new features in Google Meet which will bring welcome improvements to virtual meetings were announced this week at Google I/O.

Deep Blue Became World Chess Champion 25 Years Ago

Today is the 25th Anniversary of IBM's chess playing supercomputer Deep Blue beating the reigning world chess champion Gary Kasparov at his own game, marking a milestone in the progress of artificial  [ ... ]

More News





or email your comment to:


Last Updated ( Monday, 17 May 2021 )