Standard library · Hardware

hw.ds18b20

Read a DS18B20 1-Wire temperature sensor in degrees Celsius: reset, ROM commands, conversion, scratchpad with CRC and resolution.

import hw.ds18b20

hw.ds18b20 talks to a Maxim DS18B20 temperature sensor over its single data wire. The bus is bit-banged through the pin and timing functions of hw.board, so it needs no special hardware: wire the data pin to a pull-up resistor of about 4.7 kilohms and open the sensor on that pin. On a computer the simulated board can hold one DS18B20 (board.SimOneWire) whose temperature you set with board.SimTemperature, and the driver talks to it with the real timings.

Reading the temperature

ReadCelsius does the whole exchange: it checks the resolution, starts a conversion, waits for the sensor to finish (up to 750 milliseconds at 12 bits) and reads the scratchpad. Every scratchpad read is checked with the CRC the sensor sends, and a missing sensor or a damaged read gives ERROR_C.

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

func main:
    probe := ds18b20.Open(4)
    board.SimOneWire(4, 1 as u64)
    board.SimTemperature(19625)
    temp := ds18b20.ReadCelsius(probe)
    println "temperature", temp, "C"
    println ds18b20.Detected(probe)
end
Output
temperature 19.625 C
true

Resolution and speed

The sensor measures with 9 to 12 bits. Fewer bits convert faster (94 milliseconds at 9 bits) but round the result to 0.5, 0.25 or 0.125 degrees.

hw-ds18b20-guide-resolution.salam
Try it
import hw.board
import hw.ds18b20

func main:
    probe := ds18b20.Open(4)
    board.SimOneWire(4, 1 as u64)
    board.SimTemperature(21375)
    each bits in [12, 11, 10, 9]:
        ds18b20.SetResolution(probe, bits)
        println bits, "bits:", ds18b20.ReadCelsius(probe)
    end
end
Output
12 bits: 21.375
11 bits: 21.375
10 bits: 21.25
9 bits: 21

Addressing one sensor

With one sensor on the wire, Open skips addressing. When several share the wire, read each unique 64-bit ROM code with ReadRom while it is alone and open it later with OpenRom, which selects it by that code.

hw-ds18b20-guide-addressed.salam
Try it
import hw.board
import hw.ds18b20

func main:
    board.SimOneWire(4, 0xC0FFEE as u64)
    alone := ds18b20.Open(4)
    rom := ds18b20.ReadRom(alone)
    println "rom", rom
    chosen := ds18b20.OpenRom(4, rom)
    board.SimTemperature(30000)
    println ds18b20.ReadCelsius(chosen)
end
Output
rom 15276209939278720552
30

Types

Sensor

#
struct Sensor

A DS18B20 on a data pin, with the ROM code to select it by when several sensors share the wire.

NameTypeDescription
pinint = 0The pin the data wire is connected to.
romu64 = 0The 64-bit ROM code used to select the sensor. Only used when addressed is true.
addressedbool = falseTrue when the sensor is selected by its ROM code, false when the single sensor on the wire is addressed by skipping the ROM.
hw-ds18b20-Sensor.salam
Try it
import hw.board
import hw.ds18b20

func main:
    probe := ds18b20.Open(4)
    println ds18b20.Detected(probe)
    board.SimOneWire(4, 0x0000A1B2C3D4 as u64)
    println ds18b20.Detected(probe)
    rom := ds18b20.ReadRom(probe)
    println rom & 255 as u64 == ds18b20.FAMILY as u64
    named := ds18b20.OpenRom(4, rom)
    println ds18b20.ReadCelsius(named)
end
Output
false
true
true
25

Constants and variables

FAMILY

#
const FAMILY: i32 = 40

The family code of the DS18B20, 40 (0x28), the low byte of every ROM code.

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

ERROR_C

#
const ERROR_C: f64 = -1000

The value the temperature functions return when the sensor does not answer or a read fails its CRC check, -1000.0. No real reading comes near it.

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

Functions

Open

#
func Open(pin: int): Sensor

Opens the only sensor on a pin: the pin is released with the pull-up on, and every command skips the ROM.

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

OpenRom

#
func OpenRom(pin: int, rom: u64): Sensor

Opens one sensor out of several on a pin, selected by its 64-bit ROM code as ReadRom returns it.

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

Crc8

#
func Crc8(data: Vector<int>): int

The Dallas/Maxim CRC-8 (polynomial x^8 + x^5 + x^4 + 1) of a list of bytes. The CRC of a block followed by its own CRC byte is 0.

hw-ds18b20-Config.salam
Try it
import hw.board
import hw.ds18b20

func main:
    probe := ds18b20.Open(4)
    board.SimOneWire(4, 7 as u64)
    board.SimTemperature(21375)
    println ds18b20.Resolution(probe), ds18b20.ReadCelsius(probe)
    println ds18b20.SetResolution(probe, 9), ds18b20.ReadCelsius(probe)
    pad := ds18b20.Scratchpad(probe)
    println pad.Len(), ds18b20.Crc8(pad) == 0
    println ds18b20.SetResolution(probe, 13)
end
Output
12 21.375
true 21
9 true
false

Detected

#
func Detected(s: Sensor): bool

Sends a reset pulse and returns true when a sensor answers with a presence pulse.

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

ReadRom

#
func ReadRom(s: Sensor): u64

Reads the ROM code of the only sensor on the wire and returns it as a 64-bit number with the family code in the low byte. Returns 0 when no sensor answers or the CRC does not match.

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

Scratchpad

#
func Scratchpad(s: Sensor): Vector<int>

Reads the nine scratchpad bytes (temperature, alarm limits, configuration, CRC). Returns an empty vector when no sensor answers or the CRC does not match.

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

Resolution

#
func Resolution(s: Sensor): int

The resolution the sensor is set to, 9 to 12 bits, or 0 when it cannot be read.

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

SetResolution

#
func SetResolution(s: Sensor, bits: int): bool

Sets the resolution to 9, 10, 11 or 12 bits, keeping the alarm limits, and returns true when the sensor reports the new value. Anything else returns false.

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

Start

#
func Start(s: Sensor): bool

Starts a temperature conversion and returns true when a sensor answered. Use Ready or WaitReady to know when it finishes.

hw-ds18b20-Temperature.salam
Try it
import hw.board
import hw.ds18b20

func main:
    probe := ds18b20.Open(4)
    board.SimOneWire(4, 7 as u64)
    println ds18b20.LastCelsius(probe)
    board.SimTemperature(23500)
    println ds18b20.Start(probe), ds18b20.Ready(probe)
    println ds18b20.WaitReady(probe, 1000), ds18b20.LastCelsius(probe)
    board.SimTemperature(-5250)
    println ds18b20.ReadCelsius(probe)
    println ds18b20.RawToCelsius(0xFC90), ds18b20.ERROR_C
    board.SimOneWire(-1, 0 as u64)
    println ds18b20.ReadCelsius(probe)
end
Output
85
true false
true 23.5
-5.25
-55 -1000
-1000

Ready

#
func Ready(s: Sensor): bool

Reads one bit from the sensor: true when the running conversion has finished. Only meaningful right after Start.

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

WaitReady

#
func WaitReady(s: Sensor, timeout_ms: int): bool

Calls Ready once per millisecond for up to the given number of milliseconds and returns true as soon as the conversion is done. On a computer the waiting only advances the simulated clock.

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

RawToCelsius

#
func RawToCelsius(raw: int): f64

Turns the 16-bit raw temperature of the scratchpad (two's complement, sixteenths of a degree) into degrees Celsius.

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

LastCelsius

#
func LastCelsius(s: Sensor): f64

The temperature stored in the scratchpad, without starting a conversion. A sensor that has not converted since power-up reports 85.0. Returns ERROR_C when the read fails.

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

ReadCelsius

#
func ReadCelsius(s: Sensor): f64

Starts a conversion, waits for it, and returns the temperature in degrees Celsius, or ERROR_C when the sensor is missing or a read fails its CRC check.

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