Skip to content
Advertisement

unable to set line to input

I am trying to read humidity and temperature values from the AM2301(DHT21). for that I am using the adafruit_dht library which is written for DHT11 & DHT22. because I can’t find any adafruit library for AM2301 so I use this.
when I first run the code using some gpio say D17. it works fine and gives the temperature and humidity values but when I run that program again I get this error unable to set line 24 to input but when I change the pin it solves the problem temporarily for the first time and then it causes the same error.
a lot many people ask this question, and I have tried their solutions but nothing works.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_dht
from pulseio import PulseIn
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D17)

# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single-board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print(
            "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
            )
        )

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        dhtDevice.exit()
        raise error

    time.sleep(2.0)

Output:
enter image description here

Advertisement

Answer

I found a solution, by writing use_pulseio=False solves the problem:

dhtDevice = adafruit_dht.DHT22(board.D17, use_pulseio=False)

pulseio module contains classes to provide access to basic pulse IO. Individual pulses are commonly used in infrared remotes and in DHT temperature sensors.

6 People found this is helpful
Advertisement