Standard library · Hardware

hw.dht

Read a DHT11 or DHT22 temperature and humidity sensor on one pin: start signal, pulse width decoding, checksum.

import hw.dht

hw.dht reads the DHT11 and the DHT22 (also sold as AM2302), two sensors that report temperature and relative humidity over one data pin. The driver sends the start signal, measures the pulse widths with board.PulseIn and checks the checksum the sensor sends. On a computer board.SimDht puts a simulated sensor on a pin that answers with the real pulse train, so the same code runs without hardware.

The two sensors differ in range and in how they store the numbers: the DHT11 gives whole degrees and percents with 1 degree and 5 percent accuracy, the DHT22 gives tenths with 0.5 degree and 2 percent. Pass the right kind to Open, because the start signal and the decoding depend on it. A sensor needs a pull-up resistor of about 4.7 kilohms on the data pin (the module boards have one), and should be read at most once a second (DHT11) or every two seconds (DHT22).

Reading a sensor

Read returns a Reading. Check Valid before using the numbers, and use Temperature and Humidity to take them out.

hw-dht-guide-read.salam
Try it
import hw.board
import hw.dht

func main:
    board.SimDht(4, dht.DHT22, 612, 238)
    room := dht.Open(4, dht.DHT22)
    got := dht.Read(room)
    if dht.Valid(got):
        println "temperature", dht.Temperature(got), "C"
        println "humidity", dht.Humidity(got), "%"
    else:
        println "no reading"
    end
end
Output
temperature 23.8 C
humidity 61.2 %

When a reading fails

A reading fails with READ_TIMEOUT when the sensor does not answer, and with READ_CHECKSUM when the bits arrive damaged. The usual remedy is to wait a moment and read again.

hw-dht-guide-errors.salam
Try it
import hw.board
import hw.dht

func main:
    room := dht.Open(4, dht.DHT22)
    println dht.Status(dht.Read(room))
    board.SimDht(4, dht.DHT22, 500, 200)
    board.SimDhtBadChecksum(true)
    println dht.Status(dht.Read(room))
    board.SimDhtBadChecksum(false)
    println dht.Status(dht.Read(room))
end
Output
1
2
0

The DHT11

The DHT11 needs a longer start signal (20 milliseconds against 2) and stores whole numbers. Opening a DHT11 as a DHT22, or the other way round, usually ends in a timeout.

hw-dht-guide-dht11.salam
Try it
import hw.board
import hw.dht

func main:
    board.SimDht(4, dht.DHT11, 550, 230)
    old := dht.Open(4, dht.DHT11)
    println dht.ReadTemperature(old), dht.ReadHumidity(old)
    wrong := dht.Open(4, dht.DHT22)
    println dht.Status(dht.Read(wrong))
end
Output
23 55
1

Types

Sensor

#
struct Sensor

A DHT sensor on a pin, with its kind (DHT11 or DHT22).

NameTypeDescription
pinint = 0The pin the data wire is connected to.
kindint = 22DHT11 or DHT22. It decides the length of the start signal and how the five data bytes are read.
hw-dht-Sensor.salam
Try it
import hw.board
import hw.dht

func main:
    println dht.DHT11, dht.DHT22
    board.SimDht(4, dht.DHT22, 480, 215)
    room := dht.Open(4, dht.DHT22)
    println dht.ReadTemperature(room), dht.ReadHumidity(room)
end
Output
11 22
21.5 48

Reading

#
struct Reading

The result of one reading: a status, and the humidity and the temperature when the status is READ_OK.

NameTypeDescription
statusint = 1READ_OK, READ_TIMEOUT or READ_CHECKSUM.
humidityf64 = -1000The relative humidity in percent. Meaningful only when the status is READ_OK.
temperaturef64 = -1000The temperature in degrees Celsius, negative below zero. Meaningful only when the status is READ_OK.
hw-dht-Reading.salam
Try it
import hw.board
import hw.dht

func main:
    room := dht.Open(4, dht.DHT22)
    board.SimDht(4, dht.DHT22, 553, 221)
    got := dht.Read(room)
    println dht.Valid(got), dht.Status(got) == dht.READ_OK
    println dht.Humidity(got), dht.Temperature(got)
    board.SimDhtBadChecksum(true)
    bad := dht.Read(room)
    println dht.Valid(bad), dht.Status(bad) == dht.READ_CHECKSUM
    board.SimDht(-1, dht.DHT22, 0, 0)
    silent := dht.Read(room)
    println dht.Status(silent) == dht.READ_TIMEOUT, dht.Temperature(silent) == dht.ERROR_VALUE
end
Output
true true
55.3 22.1
false true
true true

Constants and variables

DHT11

#
const DHT11: i32 = 11

The kind of the DHT11 sensor, 11.

The example for this item is shown with the ones above: hw-dht-Sensor.salam

DHT22

#
const DHT22: i32 = 22

The kind of the DHT22 (AM2302) sensor, 22.

The example for this item is shown with the ones above: hw-dht-Sensor.salam

READ_OK

#
const READ_OK: i32 = 0

The status of a good reading, 0.

The example for this item is shown with the ones above: hw-dht-Reading.salam

READ_TIMEOUT

#
const READ_TIMEOUT: i32 = 1

The status of a reading where the sensor did not answer or stopped answering, 1.

The example for this item is shown with the ones above: hw-dht-Reading.salam

READ_CHECKSUM

#
const READ_CHECKSUM: i32 = 2

The status of a reading whose checksum did not match the data, 2.

The example for this item is shown with the ones above: hw-dht-Reading.salam

ERROR_VALUE

#
const ERROR_VALUE: f64 = -1000

The value Temperature, Humidity and the read shortcuts give for a failed reading, -1000.0.

hw-dht-Quick.salam
Try it
import hw.board
import hw.dht

func main:
    board.SimDht(4, dht.DHT11, 620, 245)
    room := dht.Open(4, dht.DHT11)
    println dht.ReadTemperature(room)
    println dht.ReadHumidity(room)
end
Output
24.5
62

Functions

Open

#
func Open(pin: int, kind: int): Sensor

Opens a sensor of the given kind on a pin: the pin becomes an input with the pull-up on.

The example for this item is shown with the ones above: hw-dht-Sensor.salam

Read

#
func Read(s: Sensor): Reading

Sends the start signal, measures the 40 data bits and checks the checksum. Returns a Reading whose status tells whether it worked.

The example for this item is shown with the ones above: hw-dht-Reading.salam

Valid

#
func Valid(r: Reading): bool

True when the reading is good.

The example for this item is shown with the ones above: hw-dht-Reading.salam

Status

#
func Status(r: Reading): int

The status of the reading: READ_OK, READ_TIMEOUT or READ_CHECKSUM.

The example for this item is shown with the ones above: hw-dht-Reading.salam

Humidity

#
func Humidity(r: Reading): f64

The relative humidity of a reading in percent, or ERROR_VALUE when the reading failed.

The example for this item is shown with the ones above: hw-dht-Reading.salam

Temperature

#
func Temperature(r: Reading): f64

The temperature of a reading in degrees Celsius, or ERROR_VALUE when the reading failed.

The example for this item is shown with the ones above: hw-dht-Reading.salam

ReadHumidity

#
func ReadHumidity(s: Sensor): f64

Reads the sensor and returns the humidity in percent, or ERROR_VALUE when the reading failed.

The example for this item is shown with the ones above: hw-dht-Quick.salam

ReadTemperature

#
func ReadTemperature(s: Sensor): f64

Reads the sensor and returns the temperature in degrees Celsius, or ERROR_VALUE when the reading failed.

The example for this item is shown with the ones above: hw-dht-Quick.salam