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

By Harry Fairhead & Mike James

picoPython360

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

<ASIN:1871962692>

<ASIN:B09522FQRY>

<ASIN:1871962684>

<ASIN:B093LT5W54>

 

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


Lights, Camera, Sound - AI Improvements to Google Meet
13/05/2022

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
15/05/2022

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

pythondata

 



 

Comments




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

 



Last Updated ( Monday, 17 May 2021 )