en:pfw:dht22
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:pfw:dht22 [2025-04-25 01:08] – [Source Code] mka | en:pfw:dht22 [2025-06-27 12:05] (current) – [Implementation for RP2040 using a PIO block] mka | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| {{pfw: | {{pfw: | ||
| - | ====== DHT22 Protocol | + | ====== DHT22 ====== |
| - | ===== Introduction ===== | + | |
| The DHT22 module is a low-cost digital temperature and humidity sensor. It utilizes a capacitive humidity sensor and a thermistor to measure the surrounding air and outputs a digital signal via a single data pin. It's known for its wider measurement range and higher accuracy compared to its predecessor, | The DHT22 module is a low-cost digital temperature and humidity sensor. It utilizes a capacitive humidity sensor and a thermistor to measure the surrounding air and outputs a digital signal via a single data pin. It's known for its wider measurement range and higher accuracy compared to its predecessor, | ||
| Line 16: | Line 14: | ||
| * Suitable for indoor environmental monitoring, weather stations, and HVAC systems. | * Suitable for indoor environmental monitoring, weather stations, and HVAC systems. | ||
| - | ===== SDA ===== | + | ===== SDA Protocol |
| The DHT22 communicates using a **proprietary single-wire serial protocol**. Here's a breakdown of the process: | The DHT22 communicates using a **proprietary single-wire serial protocol**. Here's a breakdown of the process: | ||
| Line 37: | Line 35: | ||
| The microcontroller needs to carefully time these pulses to correctly interpret the data sent by the DHT22. Libraries for popular platforms like Arduino and Raspberry Pi handle these timing-critical operations. | The microcontroller needs to carefully time these pulses to correctly interpret the data sent by the DHT22. Libraries for popular platforms like Arduino and Raspberry Pi handle these timing-critical operations. | ||
| - | [[https:// | + | [[https:// |
| + | |||
| + | On page 2, under 4, "The definition of single-bus interface," | ||
| {{ : | {{ : | ||
| - | ===== Implementation ===== | + | |
| - | ==== Testing with a Logic Analyser (PulsView) ==== | + | ===== Testing with a Logic Analyser (PulsView) |
| Pin P1.0 is bidirectional. It sends the start pulse and then receives the 40 data bits from the sensor module. | Pin P1.0 is bidirectional. It sends the start pulse and then receives the 40 data bits from the sensor module. | ||
| Line 56: | Line 56: | ||
| {{ : | {{ : | ||
| - | ==== Pseudocode | + | ====== Implementation for TI's Launchpad ====== |
| - | < | + | This program was used to test DHT22 modules before installation. The DHT22 modules were tested for functionality at room temperature, |
| - | Function: Pio ( -- ) \ set port I/O function. SDA --> P1.0 | + | |
| - | Function: wakeup ( -- ) \ notify sensor to prepare the data | + | |
| - | Function: @data ( -- sun moon hum tmp chk chksum ) \ get sensor data | + | |
| - | get response time of sensor, store in register ' | + | |
| - | get startpulse duration, store in register ' | + | |
| - | 40 loop | + | |
| - | read bit using ' | + | |
| - | | + | |
| - | 08 loop | + | |
| - | | + | |
| - | calculate chksum | + | |
| - | Function: (dht22) | + | |
| - | Function: | + | |
| - | Function: | + | |
| - | n loop dht22 | + | |
| - | </ | + | |
| - | ==== Source Code ==== | + | [[en: |
| - | [[en: | ||
| + | ====== Implementation for RP2040 ====== | ||
| - | < | + | [[en: |
| - | \ Read DHT22. (bitbang) | + | |
| - | \ TI MSP430G2553 Launchpad with noForth | + | |
| - | (* | + | PIO stands for // |
| - | To do: timeout | + | ====== |
| - | + | ||
| - | History | + | |
| - | V0065 - Temperature in tenths of a degree. mk 20250424 | + | |
| - | V0064 - Loops named with ( A ) ... ( D ). Variants of loops B and D studied. This loop variation of B and D work well. Comments revised. mk 20250423 05:25 | + | |
| - | V0063 - Cleaned up the code. Data output reduced to the essentials. mk 20250421 02:04 | + | |
| - | V0062 - Works well, output rounded to whole digits. mk 20250419 21:51 | + | |
| - | Previous versions: Successive approximation to the solution. | + | |
| - | + | ||
| - | Patch millisecond to 100 microseconds. | + | |
| - | See: config2553.f | + | |
| - | 00c7 10B2 rom! | + | |
| - | hex 10B2 DMP | + | |
| - | After COLD this value is active. | + | |
| - | + | ||
| - | addr acronym registername | + | |
| - | 020 P1IN | + | |
| - | 021 P1OUT Output | + | |
| - | 022 P1DIR | + | |
| - | 023 P1IFG | + | |
| - | 024 P1IES | + | |
| - | 025 P1IE Interrupt Enable | + | |
| - | 026 P1SEL Port Select | + | |
| - | 041 P1SEL2 | + | |
| - | 027 P1REN | + | |
| - | *) | + | |
| - | + | ||
| - | + | ||
| - | asm\ | + | |
| - | + | ||
| - | hex | + | |
| - | + | ||
| - | \ helper | + | |
| - | code cls ( -- ) s0 # sp mov next end-code | + | |
| - | + | ||
| - | code p1H #1 021 & .b bis next end-code \ set lines | + | |
| - | code p1L #1 021 & .b bic next end-code | + | |
| - | code p6H 40 # 021 & .b bis next end-code | + | |
| - | code p6L 40 # 021 & .b bic next end-code | + | |
| - | + | ||
| - | + | ||
| - | \ Assign DHT22 data line to pin p1.0, controll pin to p1.6 | + | |
| - | : Pio ( -- ) \ set I/O function | + | |
| - | 41 027 *bic \ p1ren off | + | |
| - | 41 025 *bic \ p1ie disable interrupt | + | |
| - | 41 026 *bic \ P1SEL I/0 | + | |
| - | 41 041 *bic \ P1SEL2 I/0 | + | |
| - | 41 022 *bis \ set out for P1.0, P1.6 | + | |
| - | ; | + | |
| - | + | ||
| - | : wakeup ( -- ) \ notify sensor to prepare | + | |
| - | pio p1H p6H 1 ms p1L 10 ms ( p6L ) p1H ; \ 1 ms == 100┬Ás ! | + | |
| - | + | ||
| - | Code @data ( -- sun moon hum tmp chk chksum ) \ read sensor data | + | |
| - | \ get response time | + | |
| - | #1 022 & .b bic \ p1,0 IN | + | |
| - | #0 sun mov | + | |
| - | #1 024 & .b bis \ FALLING EDGE\__ | + | |
| - | #1 023 & .b bic \ clear P1IFG | + | |
| - | 40 # 021 & .b bic \ p6L\___ | + | |
| - | begin, ( A ) \ wait for edge | + | |
| - | #1 023 & .b bit \ test bit | + | |
| - | \ Z: Set if result is zero, reset otherwise | + | |
| - | \ C: Set if result is not zero, reset otherwise (.NOT. Zero) | + | |
| - | cc? while, | + | |
| - | #1 sun add | + | |
| - | repeat, | + | |
| - | 40 # 021 & .b bis \ ___/ | + | |
| - | tos sp -) mov | + | |
| - | sun tos | + | |
| - | \ get startpulse time | + | |
| - | #0 moon | + | |
| - | #1 024 & .b bic \ ___/ RISING EDGE | + | |
| - | #1 023 & .b bic \ clear P1IFG | + | |
| - | 40 # 021 & .b bic \ p6L\___ | + | |
| - | begin, ( B ) \ wait for edge | + | |
| - | #1 023 & .b bit \ test bit | + | |
| - | \ Z: Set if result is zero, reset otherwise | + | |
| - | \ C: Set if result is not zero, reset otherwise (.NOT. Zero) | + | |
| - | cc? while, | + | |
| - | #1 moon add | + | |
| - | repeat, | + | |
| - | 40 # 021 & .b bis \ ___/ | + | |
| - | tos sp -) mov | + | |
| - | moon tos | + | |
| - | \ read 40 bits to array | + | |
| - | #0 xx mov \ init array | + | |
| - | #0 yy mov | + | |
| - | #0 zz mov | + | |
| - | dm 40 # day mov | + | |
| - | begin, ( C ) | + | |
| - | #1 023 & .b bic \ clear P1IFG | + | |
| - | begin, | + | |
| - | #1 023 & .b bit \ test IFG | + | |
| - | \ Z: Set if result is zero, reset otherwise | + | |
| - | \ C: Set if result is not zero, reset otherwise (.NOT. Zero) | + | |
| - | cs? until, | + | |
| - | \ wait to read bit | + | |
| - | moon w mov | + | |
| - | w w add | + | |
| - | 40 # 021 & .b bic \ p6L\___ | + | |
| - | begin, ( D ) | + | |
| - | #1 w sub | + | |
| - | 0=? until, | + | |
| - | \ read bit | + | |
| - | 40 # 021 & .b bis \ __/ | + | |
| - | 020 & w .b mov \ read data line: bit H or L | + | |
| - | #1 w | + | |
| - | \ tos sp -) mov | + | |
| - | \ w tos | + | |
| - | \ #1 sr bic \ clear carry bit | + | |
| - | \ shift left all bits | + | |
| - | zz zz add | + | |
| - | yy yy addc | + | |
| - | xx xx addc | + | |
| - | w zz bix | + | |
| - | #1 day sub | + | |
| - | 0=? until, \ 40 bits are read | + | |
| - | \ adjust bits | + | |
| - | 8 # day mov \ lshift 40 bits up 8x -> xx=hum yy=tmp | + | |
| - | begin, | + | |
| - | zz zz add | + | |
| - | yy yy addc | + | |
| - | xx xx addc | + | |
| - | #1 day sub | + | |
| - | 0=? until, | + | |
| - | \ push data to stack | + | |
| - | tos sp -) mov xx tos mov ( -- xx ) | + | |
| - | tos sp -) mov yy tos mov ( -- yy ) | + | |
| - | zz swpb \ get upper byte | + | |
| - | tos sp -) mov zz tos mov ( -- zz.b ) | + | |
| - | \ calculate checksumme | + | |
| - | #0 w mov | + | |
| - | xx w .b add xx swpb xx w .b add | + | |
| - | yy w .b add yy swpb yy w .b add | + | |
| - | tos sp -) mov | + | |
| - | w tos mov | + | |
| - | next end-code | + | |
| - | + | ||
| - | \ display | + | |
| - | decimal | + | |
| - | : (dht22) ( -- sun moon hum tmp chk chksum ) | + | |
| - | cls wakeup @data | + | |
| - | ; | + | |
| - | + | ||
| - | : decemit ( c -- ) \ print c from DEC special character set | + | |
| - | hx 1B emit [char] ( emit [char] 0 emit emit | + | |
| - | hx 1B emit [char] ( emit [char] B emit \ back to normal output | + | |
| - | ; | + | |
| - | + | ||
| - | : .tmp ( tmp -- ) | + | |
| - | 10 /mod 3 .r [char] , emit . | + | |
| - | [char] f decemit | + | |
| - | [char] C emit space | + | |
| - | ; \ ja, geht. | + | |
| - | + | ||
| - | + | ||
| - | : .hum ( hum -- ) \ rounded to whole percent | + | |
| - | 10 / . ." %rel" space | + | |
| - | ; | + | |
| - | + | ||
| - | : dht22 ( -- ) \ print temperatur and humidity | + | |
| - | cr (dht22) | + | |
| - | | + | |
| - | .tmp space space .hum space space | + | |
| - | \ ( sun moon -- ) drop drop | + | |
| - | ( sun moon -- ) . . \ testing | + | |
| - | ; | + | |
| - | + | ||
| - | (* | + | |
| - | The DHT22 starts its next measurement AFTER the query and saves the values. | + | |
| - | These are output with the next query, so they are old. | + | |
| - | For up-to-date values, query twice. | + | |
| - | There must be more than 2 seconds between each query, otherwise the DHT22 will not respond again. | + | |
| - | *) | + | |
| - | + | ||
| - | : test ( n -- ) | + | |
| - | >r | + | |
| - | cr ." discard old values..." | + | |
| - | cr ." reading current data:" | + | |
| - | r> 0 do | + | |
| - | dm 40000 ms \ wait until the sensor is ready again. | + | |
| - | dht22 | + | |
| - | loop | + | |
| - | ; | + | |
| - | + | ||
| - | shield nn\ | + | |
| - | freeze | + | |
| - | ( finis) | + | |
| - | </ | + | |
| - | + | ||
| - | ==== More information on the MSP430G2553 | + | |
| + | * How this was discovered: [[en: | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[en: | ||
| - | --- | + | ====== Aids ====== |
| - | + | ||
| - | === Source code editor | + | |
| + | MK:\\ | ||
| I like //Notepad Next// for Linux, a cross-platform, | I like //Notepad Next// for Linux, a cross-platform, | ||
| - | https:// | + | https:// |
| - | + | ||
| - | === Terminal === | + | |
| To work with the embedded noForth system I use //e4thcom// by Manfred Mahlow. | To work with the embedded noForth system I use //e4thcom// by Manfred Mahlow. | ||
| - | https:// | + | https:// |
| - | + | Sometimes I also use Windows and Teraterm, for example, to log longer MCU outputs to a file. I also prefer it for testing parts of the source code using copy and paste.\\ | |
| - | === Logic Analyser === | + | In this project I used a Logic Analyzer |
| - | + | ||
| - | AZDelivery | + | |
| // | // | ||
| - | |||
| - | --- | ||
en/pfw/dht22.1745536128.txt.gz · Last modified: 2025-04-25 01:08 by mka