一、实验目的
使用DS18B20(ok) 测试当前环境的温湿度。显示正常。
测试3色led灯。
二、实验材料
2.1、DS18B20的手册
https://wenku.baidu.com/view/df6db6a327d3240c8447efda.html
2.2、硬件
1)树莓派3b+ 主板 1块
2)扩展板 1块
3)数据线 若干
4)DS18B20及3色LED板一个
三、硬件连接
3.1、连接方法
DS18B20及3色LED灯板
3.2、树莓派扩展板图
树莓派扩展板
注释含义:
1)序号,1,2,3,4.。。。。。,是对应J1的引脚序号,用这个序号可以在树莓派4上使用。
2)注释:每一个引脚对应的功能含义。
3)插针是每5个一组,横着联通,含义相同。
3.3、线路连接方法
1) DS18B20及3色LED板的VCC(电源+)连接到扩展的5V(1脚)上。
2) DS18B20及3色LED板的GND(电源-)连接到扩展的GND(6脚)上。
3) DS18B20及3色LED板的Temp(信号线)连接到扩展的GPIO7(7脚)上。
四、软件代码分析
4.1、软件配置
打开1线制总线开关
1线制总线开关
4.2、DS18B20代码
#!/usr/bin/env python
#----------------------------------------------------------------
# Note:
# ds18b20's data pin must be connected to pin7.
# replace the 28-XXXXXXXXX as yours.
#----------------------------------------------------------------
import os
ds18b20 = ''
def setup():
global ds18b20
for i in os.listdir('/sys/bus/w1/devices'):
if i != 'w1_bus_master1':
ds18b20 = i
def read():
# global ds18b20
location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
tfile = open(location)
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
return temperature
def loop():
while True:
if read() != None:
print "Current temperature : %0.3f C" % read()
def destroy():
pass
if __name__ == '__main__':
try:
setup()
loop()
except KeyboardInterrupt:
destroy()
备注:
注意格式,粘贴的时候,格式弄丢了。
4.3、运行效果
五、联系方式:
5.1、淘宝链接
https://shop33987716.taobao.com/shop/view_shop.htm?tracelog=twddp&user_number_id=38189558
5.2、 QQ群:
671854013(艺达的小车)