Raspberry Pi Zero DHT11
Connect DHT11 module to pin 1,6,7
To set up a DHT11 sensor on a Raspberry Pi according to the article, you’ll need to use the following commands: (use DHT11 module and pin 1,6,7)
1. Update and Upgrade:
sudo apt-get update
sudo apt-get upgrade
2. Install Python Packages:
sudo apt-get install python3-pip python3-dev
3. Install DHT11 Python Library:
pip3 install Adafruit_DHT
pip3 install RPi.GPIO
4. Run a test script:
To test the sensor, you can create a Python script and run it. The article provides the following code to save as dht11-test.py
:
import Adafruit_DHT
import time
# Set the sensor type and GPIO pin number
sensor = Adafruit_DHT.DHT11
pin = 4
# Attempt to get a sensor reading
try:
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Print the data to the console
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
# Wait for 3 seconds before the next reading
time.sleep(3)
except RuntimeError as error:
# Errors happen, but sometimes a good retry is all you need.
print(error.args[0])
time.sleep(2.0)
except KeyboardInterrupt:
print('Exiting the script.')
To run the script, save the file and use this command:
python3 dht11-test.py