MicroPython ESP32 读取DS18B20温度数据
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MicroPython ESP32 读取DS18B20温度数据相关的知识,希望对你有一定的参考价值。
MicroPython ESP32 读取DS18B20温度数据
-
DS18B20
-
接线图
-
本示例基于
Thonny
平台开发
引入的模块
from ds18x20 import DS18X20
import time, onewire
from machine import Pin
示例代码
DS18B20接在ESP32 的22号引脚上
from ds18x20 import DS18X20
import time, onewire
from machine import Pin
OneWirePin = 22
def readDS18x20():
# the device is on GPIO22
dat = Pin(OneWirePin)
# create the onewire object
ds = DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()
ds.convert_temp() # 数据转换
time.sleep_ms(750)
values = []
for rom in roms:
values.append(ds.read_temp(rom))
# values.append(u"℃")
print(values,r"℃")
return values
while True:
readDS18x20()
time.sleep(5)
示例二
from ds18x20 import DS18X20
import machine,onewire,time
def read_temp():
# the device is on GPIO22
dat = machine.Pin(22)
# create the onewire object
ds = DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()
# print('found devices:', roms)
print('temperature:', end=' ')
ds.convert_temp()
time.sleep_ms(750)
temp = ds.read_temp(roms[0])
print(temp,end='℃\\n ')
return temp
while True:
read_temp()
time.sleep(3)
- Shell调试窗口输出
示例三
读取10次温度数据,去平均值
from ds18x20 import DS18X20
from onewire import OneWire
from machine import Pin
import time
def status():
dat = OneWire(Pin(22))
ds = DS18X20(dat)
roms = ds.scan()
if not len(roms):
print("Nao encontrei um dispositivo onewire.")
return None
ds.convert_temp()
time.sleep_ms(750)
temp = 0
for i in range(10):
for rom in roms:
temp += ds.read_temp(rom)
return temp / 10
while True:
print(status(),end='℃\\n ')
time.sleep(3)
- Shell调试窗口输出
以上是关于MicroPython ESP32 读取DS18B20温度数据的主要内容,如果未能解决你的问题,请参考以下文章
Arduino ESP8266/ESP32 多路DS18B20温度采样读取
MicroPython RP2040读取DS18B20温度传感器数据+ 0.96“I2C oled显示
ESP32+时钟、闹钟、温度+microPython程序 (2020-10-09)
MicroPython ESP32读取esp32内部霍尔传感器数据