“实验十:DS18B20温度传感器实验”的版本间的差异
来自丢石头百科
Yousimaier17(讨论 | 贡献) |
Yousimaier17(讨论 | 贡献) |
||
第1行: | 第1行: | ||
− | [[Basic Experiment Kits For Arduino | + | *[[Basic Experiment Kits For Arduino]] |
+ | *[[Basic Experiment Kits For Raspberry Pi]] | ||
== Arduino == | == Arduino == | ||
=== 实验现象 === | === 实验现象 === | ||
第5行: | 第6行: | ||
=== 电路连接 === | === 电路连接 === | ||
− | * | + | * |
+ | DS18B20传感器模块 Arduino UNO R3 | ||
+ | VCC 5V | ||
+ | OUT D2 | ||
+ | GND GND | ||
=== 安装库 === | === 安装库 === | ||
第17行: | 第22行: | ||
=== 主要程序 === | === 主要程序 === | ||
<pre> | <pre> | ||
− | + | #include <OneWire.h> | |
− | + | #include <DallasTemperature.h> | |
− | + | ||
+ | #define ONE_WIRE_BUS 2 | ||
+ | OneWire oneWire(ONE_WIRE_BUS); | ||
+ | DallasTemperature sensors(&oneWire); | ||
+ | |||
+ | void setup(void) | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | Serial.println("Dallas Temperature IC Control Library Demo"); | ||
− | + | sensors.begin(); | |
− | + | } | |
− | + | ||
− | + | void loop(void) | |
− | </pre> | + | { |
+ | Serial.print(" Requesting temperatures..."); | ||
+ | sensors.requestTemperatures(); | ||
+ | Serial.println("DONE"); | ||
+ | |||
+ | Serial.print("Temperature for Device 1 is: "); | ||
+ | Serial.print(sensors.getTempCByIndex(0)); | ||
+ | |||
+ | delay(500); | ||
+ | }</pre> | ||
== 树莓派 == | == 树莓派 == | ||
− | |||
− | |||
=== 电路连接 === | === 电路连接 === | ||
* | * | ||
+ | DS18B20模块 Raspberrypi | ||
+ | BCM编号 物理引脚序号 | ||
+ | VCC 3.3V 3.3V | ||
+ | OUT GPIO4 7 | ||
+ | GND GND GND | ||
+ | |||
=== 程序运行 === | === 程序运行 === | ||
− | |||
==== Python ==== | ==== Python ==== | ||
+ | * 安装gpiozero库 | ||
+ | **可以使下面命令来安装该库 | ||
+ | <pre> | ||
+ | sudo apt update | ||
+ | sudo apt install python3-gpiozero | ||
+ | </pre> | ||
+ | :*其它树莓派上的系统可以使下面命令来安装该库: | ||
+ | <pre> | ||
+ | sudo pip3 install gpiozero | ||
+ | </pre> | ||
+ | :*运行以下语句可以查看树莓派GPIO口定义 | ||
+ | <pre> | ||
+ | pinout | ||
+ | </pre> | ||
+ | *相关配置 | ||
+ | **打开 1-Wire总线 | ||
+ | <pre> | ||
+ | sudo raspi-config | ||
+ | 选择Interfaces oOptions -> 1-Wire -> Would you like the one-wire interface to be enabled?选择Yes -> “The one-wire interface is enabled” 选择Yes -> 选择finish | ||
+ | </pre> | ||
+ | :*重启树莓派系统 | ||
+ | <pre> | ||
+ | sudo reboot | ||
+ | </pre> | ||
+ | :*安装设备驱动程序并确认设备是否有效 | ||
+ | <pre> | ||
+ | sudo modprobe w1-gpio | ||
+ | sudo modprobe w1-therm | ||
+ | cd /sys/bus/w1/devices/ | ||
+ | ls | ||
+ | </pre> | ||
+ | ::* | ||
+ | ::*28-0000004145bb 是一个外部温度传感器设备的序列号,但它可能会随每个设备而变化。因为 DS18B20 温度传感器的序列号是唯一的。 | ||
+ | ::*注意:需要记一下设备序列号,需要在程序中做对应修改 | ||
+ | **检查当前温度 | ||
+ | <pre> | ||
+ | cd 28-0000004145bb | ||
+ | ls | ||
+ | </pre> | ||
+ | ::* | ||
+ | **查看设备数据 | ||
+ | <pre> | ||
+ | cat w1_slave | ||
+ | </pre> | ||
+ | ::* | ||
+ | ::*第二行t=24437为当前温度值,如果要将其转换为摄氏度,可以除以1000,则当前温度=24437/1000=24.44℃ | ||
+ | *修改工程文件 | ||
+ | **下载树莓派参考例程,将文件解压后拷贝放在用户名目录下,打开对应文件 | ||
+ | ***将第五行的DS18B20 = '28-0000004145bb' 更改为上面查询到的设备地址,然后保存文件 | ||
+ | |||
+ | **运行 | ||
+ | <pre> | ||
+ | cd raspberrypi/10/python_gpiozero | ||
+ | python sensor.py | ||
+ | </pre> | ||
+ | *此时可看见树莓派在正确运行DS18B20程序不断打印检测到的温度值,若想退出,按ctrl+C即可 | ||
+ | *更多指令请查看[https://gpiozero.readthedocs.io/en/latest/installing.html gpiozero文档] | ||
− | + | == 例程下载 == | |
+ | * [[:File:Basic Experiment Kits For Arduino.zip|Arduino例程]] | ||
+ | * [[:File:Basic Experiment Kits For Raspberry Pi.zip|树莓派5例程 python]] | ||
== 相关例程 == | == 相关例程 == | ||
{{Arduino and Raspberry Pi Case}} | {{Arduino and Raspberry Pi Case}} |
2024年12月3日 (二) 11:34的版本
Arduino
实验现象
- 串口打印出检测的温度值。
电路连接
DS18B20传感器模块 Arduino UNO R3 VCC 5V OUT D2 GND GND
安装库
主要程序
#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup(void) { Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library Demo"); sensors.begin(); } void loop(void) { Serial.print(" Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); Serial.print("Temperature for Device 1 is: "); Serial.print(sensors.getTempCByIndex(0)); delay(500); }
树莓派
电路连接
DS18B20模块 Raspberrypi BCM编号 物理引脚序号 VCC 3.3V 3.3V OUT GPIO4 7 GND GND GND
程序运行
Python
- 安装gpiozero库
- 可以使下面命令来安装该库
sudo apt update sudo apt install python3-gpiozero
- 其它树莓派上的系统可以使下面命令来安装该库:
sudo pip3 install gpiozero
- 运行以下语句可以查看树莓派GPIO口定义
pinout
- 相关配置
- 打开 1-Wire总线
sudo raspi-config 选择Interfaces oOptions -> 1-Wire -> Would you like the one-wire interface to be enabled?选择Yes -> “The one-wire interface is enabled” 选择Yes -> 选择finish
- 重启树莓派系统
sudo reboot
- 安装设备驱动程序并确认设备是否有效
sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ ls
- 28-0000004145bb 是一个外部温度传感器设备的序列号,但它可能会随每个设备而变化。因为 DS18B20 温度传感器的序列号是唯一的。
- 注意:需要记一下设备序列号,需要在程序中做对应修改
- 检查当前温度
cd 28-0000004145bb ls
- 查看设备数据
cat w1_slave
- 第二行t=24437为当前温度值,如果要将其转换为摄氏度,可以除以1000,则当前温度=24437/1000=24.44℃
- 修改工程文件
- 下载树莓派参考例程,将文件解压后拷贝放在用户名目录下,打开对应文件
- 将第五行的DS18B20 = '28-0000004145bb' 更改为上面查询到的设备地址,然后保存文件
- 下载树莓派参考例程,将文件解压后拷贝放在用户名目录下,打开对应文件
- 运行
cd raspberrypi/10/python_gpiozero python sensor.py
- 此时可看见树莓派在正确运行DS18B20程序不断打印检测到的温度值,若想退出,按ctrl+C即可
- 更多指令请查看gpiozero文档