How can I interface a DHT11 module with a PYNQ-Z1 board to read temperature and humidity data?
I tried this. `from pynq import GPIO
from pynq.overlays.base import BaseOverlay
import time
Main execution
base = BaseOverlay(“base.bit”)
Create GPIO object for the DHT11 data pin, initially as output
dht11_pin = GPIO(GPIO.get_gpio_pin(3), ‘out’) # Use pin 1
while True:
try:
print(“Starting data transmission…”)
# Start data transmission
dht11_pin.write(0) # Pull down pin for at least 18ms
time.sleep(0.018) # 18ms
# Pull up and wait for response
dht11_pin.write(1) # Pull up
time.sleep(0.000040) # 40us
# Switch to input mode
dht11_pin.release() # Release the GPIO
dht11_pin = GPIO(GPIO.get_gpio_pin(3), 'in') # Reinitialize as input
print("Waiting for sensor response...")
while dht11_pin.read() == 1:
continue
print("Sensor response detected.")
# Sensor's low signal response
print("Waiting for sensor's low signal response...")
while dht11_pin.read() == 0:
continue
print("Sensor's low signal response detected.")
# Sensor's high signal response
print("Waiting for sensor's high signal response...")
while dht11_pin.read() == 1:
continue
print("Sensor's high signal response detected.")
# Read 40 bits of data
data = []
print("Reading data bits...")
for _ in range(40):
while dht11_pin.read() == 0:
continue
# Measure high pulse duration
start = time.time()
while dht11_pin.read() == 1:
continue
duration = time.time() - start
# Decode bit (>50us is 1, <50us is 0)
data.append(1 if duration > 0.000050 else 0)
print("Data bits read successfully.")
# Convert bits to bytes
humidity_int = int(''.join(map(str, data[0:8])), 2)
humidity_dec = int(''.join(map(str, data[8:16])), 2)
temp_int = int(''.join(map(str, data[16:24])), 2)
temp_dec = int(''.join(map(str, data[24:32])), 2)
checksum = int(''.join(map(str, data[32:40])), 2)
# Verify checksum
if (humidity_int + humidity_dec + temp_int + temp_dec) & 0xFF == checksum:
print(f"Temperature: {temp_int + (temp_dec / 10.0)}°C, Humidity: {humidity_int + (humidity_dec / 10.0)}%")
else:
print("Failed to read sensor data: Checksum mismatch")
except Exception as e:
print(f"Error occurred: {e}")
time.sleep(2) # Wait before the next read` there is no output
Hi,
If I understood correctly, you tried this python code. Were you able to get the datas from the DHT11 when executing it on a jupyter notebook on the PYNQ-Z1?
I am trying to interface the DHT11 module on the Jupyter Notebook with the PYNQ-Z1, but it does not work! When I tried the same sensor in Arduino, it worked fine. is there anything that can help or am I missing something?
When you say “it doesn’t work” are there any errors displayed in the terminal when you execute your code in a terminal? How do you know it is not working?
No errors it stuck here Start signal sent.
Waiting for sensor response…
Sensor response detected. Reading data…
from pynq import GPIO
import time
# Define the GPIO pin number
pin_number = GPIO.get_gpio_pin(8) # Using a user index
# Function to send the start signal
def send_start_signal():
# Initialize the GPIO pin as output to send the start signal
dht11_pin = GPIO(pin_number, 'out')
dht11_pin.write(0) # Pull down for 18 ms
time.sleep(0.018)
dht11_pin.write(1) # Release for 20 µs
time.sleep(0.00002)
dht11_pin.release() # Release the pin after use
print("Start signal sent.")
# Function to read data from the sensor
def read_data():
# Reinitialize the pin as input to read data
dht11_pin = GPIO(pin_number, 'in')
data = []
print("Waiting for sensor response...")
# Wait for the sensor's response
while dht11_pin.read() == 1:
pass # Busy-wait until response
print("Sensor response detected. Reading data...")
# Read 40 bits from the sensor
for i in range(40):
while dht11_pin.read() == 0: # Wait for the bit to start
pass
start = time.time()
# Measure the pulse width to determine bit value
while dht11_pin.read() == 1:
pass
pulse_length = time.time() - start
data.append(1 if pulse_length > 0.00005 else 0)
print(f"Bit {i}: {'1' if pulse_length > 0.00005 else '0'} (pulse: {pulse_length:.6f}s)")
print("Data reading completed.")
dht11_pin.release() # Release pin after reading
return data
# Start Communication
send_start_signal()
response = read_data()
print("Received Data:", response)
Thank you for your reply. I tried to read the DHT11 sensor on the PYNQ Z1, but I failed. I am new to PYNQ.
Is it possible to read the DHT11 using the PS GPIO from the base overlay or arduino header? Are there any specific methods to do so? I’ve seen many examples for Grove sensors, but I’m particularly interested in the DHT11.
Hi @rparada, unfortunately I couldn’t get it working. If I remember correctly, I ended up using a different Grove I2C temperature and humidity sensor that was supported by PYNQ, and I gave up on the DHT11 at that time.
Do you have any ideas or an implementation for it?
I was using HLS, and I believe I was close to solving it, but I ran out of time and moved to another sensor instead. If you decide to implement it and you have some code but get stuck, I might be able to help.
Hi @Moh_Ali, thank you for the response. We’re trying to connect the DHT22 sensor, at the moment, we could load the libraries however, we have a problem with the clock sync. So, with the Grove I2C temperature and humidity sensor was just “plag & play”, if you can give me some tips to make it work would be great.