ds18x20 — DS18B20 temperature sensor driver

The one wire bus is a serial bus that uses only a single wire for communication (in addition to the wires for ground and power). The DS18B20 temperature sensor is a very popular single-wire device, featuring a small size, low hardware overhead, strong anti-interference ability, and high accuracy.




../../../../../../_images/DS18B20-500x500-800x800.jpg

Note

The image must be viewed online: ds18b20-module.png

../../../../../../_images/cebe2df9-a7d2-4f28-8477-9fd5ffc0ab46.jpg
  • Unique onewire (single-bus) interface: when connecting to a microprocessor, the DS18B20 needs only one port line to achieve two-way communication between the microprocessor and the DS18B20.

  • Temperature measurement range: -55℃~+125℃,

  • Supports multi-point networking: multiple DS18B20 devices can be connected in parallel on the only three wires, up to 8 in parallel, to achieve multi-point temperature measurement. If there are too many, the supply voltage will be too low, causing unstable signal transmission.

  • Operating power: 3.0~5.5V/DC



../../../../../../_images/ds18x20_res.jpeg

If you are using a single ds18b20 device, to ensure stable data transmission, the signal OUT pin needs a 4.7K pull-up resistor.


DS18X20 Class

class ds18x20.DS18X20(onewire)

Construct the DS18X20 class.

  • onewire - The one wire instance object

DS18X20.scan()

Scan the ds18b20 devices on the single bus and return a list of device addresses.

DS18X20.convert_temp()

Read the sampled temperature and perform the temperature unit conversion.

DS18X20.read_temp(rom)

Return the temperature value of the device. Note that after converting the temperature with convert_temp(), you must wait at least 750 milliseconds before reading the temperature value.

  • rom - The device address

Example - ds18b20
 1from machine import Pin
 2import onewire 
 3import time, ds18x20
 4# Create a one wire bus, the pin is P0
 5ow = onewire.OneWire(Pin(Pin.P0)) 
 6# Instantiate the DS18X20 class
 7ds = ds18x20.DS18X20(ow)
 8# Scan the DS18B20 on the bus to get the device list
 9roms = ds.scan()  
10
11while True:
12    # Convert the temperature value, convert_temp must be called before each reading, otherwise the temperature data will not be updated
13    ds.convert_temp()
14    # Wait at least 750ms after convert_temp
15    time.sleep_ms(750)
16    # Return the temperature value of the ds18b20 device on the bus
17    for rom in roms:
18        print('Device %s temperature is %d'%(bytes(rom),ds.read_temp(rom)))