ds18x20 — Digital temperature sensor

The DS18B20 sensor is a digital thermometer that communicates over a 1-wire serial interface. These sensors have a range of -55 to +125 degrees Celsius with an accuracy of 0.5 degrees C for between -10 to +85 degrees C.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-wire bus. Using the ds18x20 module, it is simple to use the Kookaberry to use multiple DS18B20s to sense the temperatures at different points.

The DS18B20 comes in diverse packaging, including waterproof probes for sensing in liquids.

The sensor data-sheet is available here: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

Class DS18x20

Example Usage:

from onewire import OneWire
from machine import Pin
import time
from ds18x20 import DS18X20

ow = OneWire(Pin("P4")) # Create the 1-wire interface object on Pin P4
# Create further 1-wire interfaces as needed on different Pins

sensor = DS18X20(ow) # define the first sensor object
# Create further DS18X20 objects as needed on the same or other 1-wire interfaces

roms = sensor.scan() # discover all sensors on the 1-wire bus

while True: # run in this loop forever
    sensor.convert_temp() # initiate sensing for all sensors on the same 1-wire bus
    time.sleep(1) # give the sensors time to get ready
    for probe in roms: # read the sensors one at a time in turn
        temperature = probe.read_temp(probe)
        print('Probe: %s Temperature: %0.1f C' % (probe,temperature))

DS18X20 Constructors

class ds18x20.DS18X20(ow)

Creates a sensor object.

ow is 1-wire bus created using the onewire object connected to a particular Pin.

DS18X20 Methods

DS18X20.scan()

Returns an array of DS18X20 sensors on the 1-wire bus.

DS18X20.convert_temp(rom=None)

Starts the temperature conversion on all sensors on the 1-wire bus, or on the identified sensor.

The parameter rom is one of the members of the array returned by DS18X20.scan(). If rom is supplied, only this device will start conversion. Otherwise, all devices on the bus will be started.

DS18X20.read_temp(rom)

Returns the temperature in degrees Celsius from the specified sensor.

The parameter rom is one of the members of the array returned by DS18X20.scan().

Important

After power up and before a conversion cycle has been performed, the DS18x20 sensors will return the value 85°C. Since this is also a valid return value, the calling app must decide, whether it is a reasonable value in the given context.

ds18x20.read_scratch(rom)

Reads the scratchpad memory of the addressed device. 9 bytes of data will be returned. See the DS18B20 data-sheet to interpret the results. This method is useful for diagnosis of sensors but is not usually used otherwise.

The parameter rom is one of the members of the array returned by DS18X20.scan().

ds18x20.write_scratch(rom, data)

Writes to the scratchpad of the addressed device.

The parameter rom is one of the members of the array returned by DS18X20.scan().

Data shall be three bytes. The first two bytes are the high and low alarm temperatures. The third by is the configuration. See the DS18B20 data-sheet for details.