ds18x20
— ds18b20 Temperature sensor driver¶
One wire bus is a serial bus that uses only one wire for communication (except the wires used for grounding and power supply). The DS18B20 temperature sensor is a very popular single-wire device with the characteristics of conpact, low hardware overhead, strong anti-interference ability, and high accuracy.
- Unique onewire (single bus) interface mode, DS18B20 only needs one port line to realize bidirectional communication between microprocessor and DS18B20 when connected with microprocessor.
- Temperature range -55℃~+125℃,
- Support multi-point networking function, multiple DS18B20s can be connected in parallel on the only three wires, and only a maximum of 8 can be connected in parallel to achieve multi-point temperature measurement. If the number is too large, the power supply voltage will be too low, resulting in stable signal transmission.
- Power Supply: 3.0~5.5V/DC
DS18X20 class¶
-
class
ds18x20.
DS18X20
(onewire)¶
Create DS18X20 class。
onewire
- one wire object sample
-
DS18X20.
scan
()¶
Scan the ds18b20 device on the single bus and return a list of device addresses.
-
DS18X20.
convert_temp
()¶
Obtain temperature sampling for convert temperature unit conversion.
-
DS18X20.
read_temp
(rom)¶
Returns the temperature value of the device. NOte that after convert_temp()
converts the temperature, you need to wait at least 750 ms before reading the temperature value.
rom
- device address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from machine import Pin
import onewire
import time, ds18x20
# 创建one wire总线,引脚为P0
ow = onewire.OneWire(Pin(Pin.P0))
# 实例DS18X20类
ds = ds18x20.DS18X20(ow)
# 扫描总线上的DS18B20,获取设备列表
roms = ds.scan()
while True:
# 转换温度值,每次获取温度前必须调用convert_temp,否则温度数据不会更新
ds.convert_temp()
# convert_temp后至少等待750ms
time.sleep_ms(750)
# 返回总线的上ds18b20设备的温度值
for rom in roms:
print('Device %s temperature is %d'%(bytes(rom),ds.read_temp(rom)))
|