树莓派PICO读取温度传感器

需要的材料

1.温度传感器模块:

我购买的是某宝上3块钱的“DS18B20温度传感器模块”

 

2.杜邦线:某宝几块钱一组40P,这里只需要三根,用于连接 树莓派与温度传感器

树莓派PICO GPIO 说明

GPIO是(General Purpose Input Output)的缩写,也就是通用输入输出,是一种常见的硬件接口,用以表示开关量。

 

物理连接

1.连线图:

 

Python 控制脚本

 去树莓派PICO中文站 现在载入载入MicroPython到树莓派PICO中

 去 Thonny, Python IDE for beginners 下载 Thonny Python IDE

代码如下:

import machine, onewire, ds18x20, time 
ds_pin=machine.Pin(4) #我将传感器连接到GO4 这里为:4
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) #创建onewire总线 引脚4(GO4)
roms = ds_sensor.scan() #扫描总线上的设备
print('Found DS devices: ', roms)
while True:
    ds_sensor.convert_temp() #获取采样温度
    time.sleep_ms(750)
    for rom in roms:
        print(rom)
        print(ds_sensor.read_temp(rom)) #得到温度
    time.sleep(2)

运行结果如下

原文地址:https://www.cnblogs.com/ejiyuan/p/15389806.html