Are you sure you have the correct sensor for the guide you’re following?
The code and diagram you show looks like it’s for a DHT22, which uses the onewire protocol for communication but you say you’re using the DHT20 which uses the I2C protocol.
If you actually have a DHT20, the connections and code are not the same as what you using.
I’m not finding a easy to use guide anywhere but here’s the pinout for the DHT20.
Pin 1 on the sensor needs to go to the Pi’s 3.3 volt pin. (Red wire below)
Pin 2 on the sensor needs to go to the Pi’s SDA pin. (Blue wire below)
Pin 3 on the sensor needs to go to the Pi’s GND pin. (Black wire below)
Pin 4 on the sensor needs to go to the Pi’s SCL pin. (Yellow wire below)
Additionally you need two resistors. The 10K ones you appear to have should work. One leg of each resistor connects to 3.3 volts, (red wire) and the other end of one resistor goes to SCL (yellow wire) and the other end of the other resistor goes to SDA (blue wire)
Google’s AI had the following, which should work:
AI Overview
The DHT20 sensor connects to the Raspberry Pi using the
I²C interface, which simplifies wiring compared to older DHT sensors. The required code utilizes the I²C bus for communication.
Hardware Hookup (Wiring)
The DHT20 sensor has four pins: VCC, GND, SDA, and SCL. Connect them to the Raspberry Pi’s GPIO pins as follows:
- VCC: Connect to a 3.3V or 5V pin on the Raspberry Pi.
- GND: Connect to any Ground (GND) pin on the Raspberry Pi.
- SDA (Serial Data): Connect to the Raspberry Pi’s SDA (GPIO 2, physical Pin 3).
- SCL (Serial Clock): Connect to the Raspberry Pi’s SCL (GPIO 3, physical Pin 5).
Software Setup and Code
To use the DHT20, you need to enable the I²C interface on your Raspberry Pi and
install the necessary Python libraries.
- Enable I²C Interface
- Open the Raspberry Pi terminal.
- Run
sudo raspi-config.
- Navigate to Interfacing Options > I2C and enable it.
- Reboot your Raspberry Pi if prompted.
- Install Necessary Libraries
You’ll need the smbus2 Python module for accessing the I²C bus.
bash
sudo apt update
sudo apt install python3-smbus2 python3-dev
- Python Code Example
Here is a basic Python script to read data from the DHT20 sensor:
import time
import smbus
# The default I2C address for the DHT20 is 0x38
address = 0x38
# Open I2C bus 1 (standard on Raspberry Pi)
i2cbus = smbus.SMBus(1)
# Wait for the sensor to power up and initialize
time.sleep(0.5)
# Function to read data from the sensor
def read_dht20():
# Send a command to trigger measurement
i2cbus.write_i2c_block_data(address, 0xAC, [0x33, 0x00])
# Wait for the measurement to complete (typically 80ms)
time.sleep(0.08)
# Read 7 bytes of data
data = i2cbus.read_i2c_block_data(address, 0x71, 7)
# Check if data is valid (Bit[7] of status word should be 0 when busy flag is off)
# The first byte is the status word
status = data[0]
if not (status & 0x80):
# Calculation for humidity
hraw = ((data[1] << 12) | (data[2] << 4) | (data[3] >> 4))
humidity = 100 * float(hraw) / 2**20
# Calculation for temperature
traw = (((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5])
temperature = 200 * float(traw) / 2**20 - 50
return temperature, humidity
else:
return None, None
# Main loop to print readings every few seconds
try:
while True:
temp, hum = read_dht20()
if temp is not None:
print(f"Temperature: {temp:.2f} C, Humidity: {hum:.2f} %")
else:
print("Failed to read from DHT20 sensor!")
time.sleep(3)
except KeyboardInterrupt:
pass
You can save this as a Python file (e.g., dht20_reader.py) and run it from the terminal using python3 dht20_readaer.py
Unfortunately I don’t have a sensor or Pi handy to test but hopefully this gets you going.