DHT20 sensor not found

Hello. I have a Raspberry Pi 5 and I’m trying to connect a DHT20 sensor to it. As far as I can tell, the wiring is accurate but I keep getting the error “DHT sensor not found, check wiring”. I’m attaching one picture of my actual wiring and one that I was using as a guide. The coding has been done using Adafruit’s DHT library and LGPIO. Any idea on how to fix this? This is a for a kids’ school project. All hardware is newly purchased from Sparkfun.

The code is:

The final code is:

import time

import adafruit_dht

import board

dht_device = adafruit_dht.DHT22(board.D4)

while True:

try:

    temperature_c = dht_device.temperature

    temperature_f = temperature_c \* (9 / 5) + 32



    humidity = dht_device.humidity



    print("Temp:{:.1f} C / {:.1f} F    Humidity: {}%".format(temperature_c, temperature_f, humidity))

except RuntimeError as err:

    print(err.args\[0\])



time.sleep(2.0)

You’re missing the short red wire in the Fritzing diagram.
Add that and see if it starts working.

1 Like

I didn’t realize that was an actual wire since the text instructions didn’t mention it. I don’t have any jumper cables that have pins on both ends, could I use another resistor there? Thanks for replying!

It needs to be a wire, you could clip the lead off a resistor and bend that into a U shape and use that.

1 Like

I just did this but I’m still getting the same error that it can’t find the sensor.

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.

  1. 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.
  1. 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
  1. 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.

3 Likes

I do have the DH20 instead of the DH22 and I also just realized they’re very different but I was having trouble finding wiring diagrams. The one you sent is more understandable so I will try that. Thank you for the help. This will take me a bit to get to with updating code as well but I will work on this and let you know how it goes.

1 Like

Good news. With the updated wiring, I’m able to use i2cdetect to actually detect the sensor. I’ll work on the rest of the coding next.

This was it! I have both temperature and humidity outputs. Thank you!

2 Likes

Glad I could help!

1 Like