- 树莓派python温湿度检测代码
import RPi.GPIO as GPIO #添加头文件包
import time #添加time文件包
def read(channel): #定义读取函数
GPIO.setmode(GPIO.BOARD) #设计引脚
GPIO.setup(channel,GPIO.OUT) #设计channel引脚为输出
GPIO.output(channel,GPIO.LOW) #设计channel引脚为低电平
time.sleep(0.028) #延时0.028秒
GPIO.output(channel,GPIO.HIGH) #设计channel为高电平
GPIO.setup(channel,GPIO.IN) #设计channel为输入
while GPIO.input(channel) == GPIO.LOW: #当channel为低电平结束
continue
while GPIO.input(channel) == GPIO.HIGH: #当channel为高电平结束
continue
data = [] #列表
for j in range(40):
t_flag = 0 #赋值
while GPIO.input(channel) == GPIO.LOW: #判断channel为低电平
pass #弹出
t1 = time.time() #记录当前时间为t1
while GPIO.input(channel) == GPIO.HIGH: #判断channel为高电平
pass
t2 = time.time() #记录当前时间t2
if (t2-t1)*1000000 < 30: #如果值小于30
data.append(0) #输出为0
else: #否则
data.append(1)