最近在學習python,最近也買了塊樹莓派(raspberry 3 b),講不清是為了學習python而買的pi,還是為了鼓搗pi而去學習python。
昨天晚上按照教程自己搭建了乙個溫濕度測試平台,**主要也是在網上找的,我沒有仔細去看dh11的原理,大概和ds18b20是乙個套路吧。先能把**跑起來,再返回頭看**吧。這能操作gpio的基礎,將來做個跑馬燈,控制數碼管,繼電器,步進電機都離不開它。
~~~啥時候能支援markdown呢?
引腳號名稱型別說明1
vcc電源
+級,輸入3v-5.5v
2data
資料輸出
輸出引腳
3gnd
接地接地引腳
****
```#!/usr/bin/python
import rpi.gpio as gpio
import time
channel =4
data =
j = 0
gpio.setmode(gpio.bcm)
time.sleep(1)
gpio.setup(channel, gpio.out)
gpio.output(channel, gpio.low)
time.sleep(0.02)
gpio.output(channel, gpio.high)
gpio.setup(channel, gpio.in)
while gpio.input(channel) == gpio.low:
continue
while gpio.input(channel) == gpio.high:
continue
while j < 40:
k = 0
while gpio.input(channel) == gpio.low:
continue
while gpio.input(channel) == gpio.high:
k += 1
if k > 100:
break
if k < 8:
else:
j += 1
print "sensor is working."
print data
humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]
humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0
for i in range(8):
humidity += humidity_bit[i] * 2 ** (7-i)
humidity_point += humidity_point_bit[i] * 2 ** (7-i)
temperature += temperature_bit[i] * 2 ** (7-i)
temperature_point += temperature_point_bit[i] * 2 ** (7-i)
check += check_bit[i] * 2 ** (7-i)
tmp = humidity + humidity_point + temperature + temperature_point
if check == tmp:
print "temperature :", temperature, "*c, humidity :", humidity, "%"
else:
print "wrong"
print "temperature :", temperature, "*c, humidity :", humidity, "% check :", check, ", tmp :", tmp
gpio.cleanup()
```
樹莓派連線DHT11溫濕度感測器(python)
vcc接5v或者3.3v data接gpio 我接的是bcm的16號針腳,物理針腳是36 gnd接gnd codesyntax lang python usr bin python import rpi.gpio as gpio import time channel 16 引腳號16 data 溫...
樹莓派連線DHT11溫濕度感測器(python)
vcc接5v或者3.3v data接gpio 我接的是bcm的16號針腳,物理針腳是36 gnd接gnd codesyntax lang python python view plain copy usr bin python import rpi.gpio as gpio import time ...
樹莓派python學習篇(一)溫濕度感測器
二 dht11 python 16 感測器輸出口接bcm模式 gpio16 data j 0 gpio.setmode gpio.bcm time.sleep 1 mcu向dct發射啟動訊號 gpio.setup channel,gpio.out gpio.output channel,gpio.l...