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
Electronics
Complete Listing

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

By Harry Fairhead & Mike James

picopython2e360

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

Also of interest:

Raspberry Pico File System

<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.

Banner


C23 ISO Standard Is Here But You Probably Won't Read It
06/11/2024

At last ISO C23 has been published, but at $250 you probably aren't going to read it. Can we really tolerate this sort of profiteering on the work of others? This is worse than academic publishing!



Looking Forward To NAO 7
03/11/2024

Introduced to the world in 2004 by its creator Bruno Maisonnier the kid-sized, autonomous humanoid robot NAO, turns 20 this year. At less than 2 ft tall, it is small in stature, but plays a big r [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Monday, 17 May 2021 )