Search code examples
pythonpython-3.xraspberry-pisensorsgpio

Raspberry Pi DHT 11 sensor not working. I get None None


Have integrated Raspberry pi4 with a DHT sensor. The data pin is connected to pin GPIO 26

Have tried connecting the VCC to both 3.3V and 5V

Have tried with both Adafruit_DHT.DHT11 and Adafruit_DHT.DHT22 in the code for the same sensor but I get None None

import Adafruit_DHT

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22
pin = 26

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    print(temperature, humidity)

Output:

None None

Is the sensor broken??Should I replace it or is there any other solution??


Solution

  • from pigpio_dht import DHT11, DHT22
    
    gpio = 4 # BCM Numbering
    
    sensor = DHT11(gpio)
    #sensor = DHT22(gpio)
    
    result = sensor.read()
    print(result)
    

    This worked for me. Before running the code enter the below commands on the terminal

    sudo pigpiod #Start daemon
    
    pigs pud 4 u # Set internal pull up
    

    If pigpio-dht is not installed enter pip3 install pigpio-dht and run the above program