温度传感器(DS18B20)
概述
基于DS18B20数字温度传感器,用于检测环境温度。
技术参数
工作电压: VCC 3.3V -5V
模块尺寸:24*48mm
固定安装:2个兼容塑料积木通孔
温度:-55--125℃
引脚定义
引脚 |
定义 |
|---|---|
GND |
地 |
VCC |
电源 |
DI |
数字输出 |
NC |
空 |
示例程序
间隔一秒打印温度信息
from mpython import *
from machine import Timer
import onewire
import ds18x20
_ds18x20_list = []
tim13 = Timer(13)
def timer13_tick(_):
for p in _ds18x20_list:
try:
eval("ds_" + p + ".convert_temp()")
for rom in eval("roms_" + p):
t = eval("ds_" + p).read_temp(rom)
eval("set_ds_value_" + p + "(" + str(t) + ")")
break
except: pass
tim13.init(period=1000, mode=Timer.PERIODIC, callback=timer13_tick)
import time
def set_ds_value_0(_t):
global _ds18x20_value_0
_ds18x20_value_0 = _t
ow_0 = onewire.OneWire(Pin(Pin.P0))
ds_0 = ds18x20.DS18X20(ow_0)
roms_0 = ds_0.scan()
_ds18x20_value_0 = 0
_ds18x20_list.append(str(0))
while True:
print(_ds18x20_value_0)
time.sleep(1)