Project - using the DHT11 sensor with a ROCK 4C+
I bought a ROCK 4C+ and wanted to measure temperature and humidity using the DHT11 sensor module. I had trouble using the same code that was working on my Raspberry Pi (using the Adafruit CircuitPython DHT library) and couldn’t find a complete guide for getting it working on the Rock.
I’ve gotten it all working now so thought I would share in-case this helps anyone else.
Step 1. Wire it up
My DHT11 has 3 pins - VCC, Data and GND.
I connected the data pin to pin 16 (GPIO4_D4). Is this the best one? Who knows, I know nothing about electronics. But it appeared to work. It seems to work most accurately with a resistor.
Step 2. Prepare the OS
The operating system seemingly needs to be > Linux 4.8, so I downloaded the newest version of Debian (Debian 11 Desktop) for the ROCK 4 off the Radxa website. I flashed that onto a micro-SD card using Balena Etcher.
Step 3. Update the OS
I turned the ROCK on and updated it
sudo apt upgrade
sudo apt autoremove
Step 4. Install the libraries
Firstly I installed the python3-dev tools
sudo apt install python3-dev
Next I installed the following Python libraries:
sudo pip3 install click
sudo pip3 install gpiod
sudo pip3 install adafruit-python-shell
sudo pip3 install adafruit-blinka
sudo pip3 install adafruit-circuitpython-dht
Step 5. Write the code
I created a Python file with the following code:
from time import sleep
from adafruit_dht import DHT11
import board
# Set the device to receive data from pin GPIO4_D2
dht_device = DHT11(board.D16)
while True:
# The sensor fails every now and again
# So we put in error catching so that the whole program doesn't stop
try:
# Get temperature and humidity
temp = dht_device.temperature
hum = dht_device.humidity
print(f"Temp: {temp}C, Humidity: {hum}%")
# Print any errors with the sensor
except RuntimeError as error:
print(error.args[0])
# Wait for 5 seconds
sleep(5.0)
Step 6. Run
Run the code - for some reason it only works when run using sudo, otherwise we get a permissions error.
sudo python3 dht11.py
I haven’t tested it, but hopefully this works for other adafruit-circuitpython libraries!