Article Directory
1. Introduction
In order to be familiar with the GPIO operation of the Raspberry Pi, I found a small case. Some problems occurred, which were only solved recently, so I recorded it.
2. Hardware preparation
1. DHT11 temperature and humidity sensor
The DHT11 pins sold on the market now have three pins and four pins. Because one pin is empty, they are all used the same. I bought three pins.
2. Raspberry Pi 4B
extension port:
Pin wiring: (The current wiring is arbitrary, just remember that the DATA position and the positive and negative poles are not plugged in reverse)
- VCC - Pin1
- DATE - Pin3
- GND - Pin6
Three, software preparation
Thonny
Use the Python IDE that comes with the system
Four, program design
1. Use Adafruit to read DHT11 temperature and humidity sensor
①, software installation
Before you start, you need to update the software package:
sudo apt-get update
sudo apt-get install build-essential python-dev
Get the Adafruit library from GitHub:
sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
Install the library for Python 2 and Python 3:
sudo python setup.py install
sudo python3 setup.py install
②, use the sample program
Sample program
Adafruit provides a sample program, run the following command to test.
cd ~
cd Adafruit_Python_DHT
cd examples
python AdafruitDHT.py 11 2
③, a custom Python program using the Adafruit library
Of course, you can also use this library in your own Python programs to implement calls
import Adafruit_DHT
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT11
# Set GPIO sensor is connected to
gpio=2
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
④ A custom python program for real-time cycling display of temperature and humidity
#!/usr/bin/python
# *-*- coding:utf-8 -*-*
import Adafruit_DHT
class DHT11:
def DHT11_Read(self):
sensor = Adafruit_DHT.DHT11
gpio = 2
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
return humidity,temperature
if __name__ == '__main__':
float humid,temp
dht11 = DHT11()
while True:
humid,temp=dht11.DHT11_Read()
if humid is not None and temp is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temp, humid))
else:
print('Failed to get reading. Try again!')
2. Use GPIO to read DHT11 temperature and humidity sensor
import RPi.GPIO as GPIO
import time
channel =2 # 上文提到的GPIO编号
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:
data.append(0)
else:
data.append(1)
j += 1
print ("sensor is working.")
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, "%")
res='{value:%f}'% temperature
import json
with open('/home/pi/Desktop/data.txt', 'a') as outfile:
json.dump(res, outfile)
outest=open('/home/pi/Desktop/data.txt','a')
outest.write(res)
outest.close
print(res)
else:
print ("wrong")
GPIO.cleanup()
It stands to reason that there should be no problem, but the results can not be displayed, the temperature and humidity are both displayed 255, the data bit is not in handy, welcome friends who are knowledgeable to answer.
3. Problems encountered
①, Raspberry Pi 4B using Adafruit_DHT can not display the temperature correctly
This is because the downloaded Adafruit_DHT driver does not support the 4b processor BCM2711, we need to open it manually /home/pi/Adafruit_Python_DHT/Adafruit_DHT/platform_detect.py
and add the following code in the red circle.
elif match.group(1) == 'BCM2711':
# Pi 4b
return 3
Then return to the Adafruit_Python_DHT
directory, reinstall the Adafruit Python DHT Sensor function library, and run the sample program again to display the correct temperature and humidity.
cd Adafruit_Python_DHT/
sudo python setup.py install