0

DHT22 sensor Unable to set line 4 to input
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

DHT22 sensor Unable to set line 4 to input

by bobs409 on Mon Sep 14, 2020 5:35 pm

I have been trying to get a DHT22 (actually an AM2302) sensor to read on Pi zero. I used the latest circuit python library from here: https://learn.adafruit.com/circuitpytho ... spberry-pi

I am running the code from here and I am running it as python3: https://learn.adafruit.com/dht-humidity ... thon-setup

I have tried both pin 4 and pin 18 but neither work.

First off, I get this error message:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "trytemptest.py", line 9, in <module>
    dhtDevice = adafruit_dht.DHT22(board.D4,use_pulseio=False)
TypeError: __init__() got an unexpected keyword argument 'use_pulseio'


If I remove "use_pulseio=False" from the code all together and run it, I get this:
Code: Select all | TOGGLE FULL SIZE
Unable to set line 4 to input


These pins are in fact set to input.

Any ideas on what is wrong?

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Tue Sep 15, 2020 8:52 pm

This sounds like an out of date blinka library. Could you run this command to make sure everything is up to date:

Code: Select all | TOGGLE FULL SIZE
pip3 install --upgrade adafruit-blinka adafruit-circuitpython-dht

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Wed Sep 16, 2020 12:22 pm

Thanks for replying Mike.

I ran that and got this:
Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~ $ pip3 install --upgrade adafruit-blinka adafruit-circuitpython-dht
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting adafruit-blinka
  Downloading https://www.piwheels.org/simple/adafruit-blinka/Adafruit_Blinka-5.4.0-py3-none-any.whl (132 kB)
     |████████████████████████████████| 132 kB 272 kB/s
Collecting adafruit-circuitpython-dht
  Downloading https://www.piwheels.org/simple/adafruit-circuitpython-dht/adafruit_circuitpython_dht-3.5.1-py3-none-any.whl (8.8 kB)
Requirement already satisfied, skipping upgrade: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from adafruit-blinka) (4.2.4)
Requirement already satisfied, skipping upgrade: pyftdi>=0.40.0 in /usr/local/lib/python3.7/dist-packages (from adafruit-blinka) (0.51.2)
Requirement already satisfied, skipping upgrade: Adafruit-PureIO>=1.1.5 in /usr/local/lib/python3.7/dist-packages (from adafruit-blinka) (1.1.5)
Collecting Adafruit-PlatformDetect>=2.15.1
  Downloading https://www.piwheels.org/simple/adafruit-platformdetect/Adafruit_PlatformDetect-2.17.0-py3-none-any.whl (18 kB)
Requirement already satisfied, skipping upgrade: RPi.GPIO in /usr/lib/python3/dist-packages (from adafruit-blinka) (0.7.0)
Requirement already satisfied, skipping upgrade: sysv-ipc in /usr/local/lib/python3.7/dist-packages (from adafruit-blinka) (1.0.1)
Requirement already satisfied, skipping upgrade: pyusb>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from pyftdi>=0.40.0->adafruit-blinka) (1.0.2)
Requirement already satisfied, skipping upgrade: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->adafruit-blinka) (3.4)
Installing collected packages: Adafruit-PlatformDetect, adafruit-blinka, adafruit-circuitpython-dht
Successfully installed Adafruit-PlatformDetect-2.17.0 adafruit-blinka-5.4.0 adafruit-circuitpython-dht-3.5.1


I then ran the dht_simpletest.py again and got this:
Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~/Adafruit_Python_DHT $ python3 dht_simpletest.py
Unable to set line 4 to input
Timed out waiting for PulseIn message. Make sure libgpiod is installed.
Timed out waiting for PulseIn message. Make sure libgpiod is installed.
^CTraceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 85, in _wait_receive_msg
    message = self._mq.receive(block=False, type=type)
sysv_ipc.BusyError: No available messages of the specified type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dht_simpletest.py", line 16, in <module>
    temperature_c = dhtDevice.temperature
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 253, in temperature
    self.measure()
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 198, in measure
    pulses = self._get_pulses_pulseio()
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 136, in _get_pulses_pulseio
    while self.pulse_in:
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 155, in __len__
    message = self._wait_receive_msg()
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 88, in _wait_receive_msg
    time.sleep(0.001)  # wait a bit then retry!
KeyboardInterrupt



Thanks for any help you can offer,


Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Wed Sep 16, 2020 2:08 pm

Hi Bob,

Let's make sure libgpiod is installed (which it is asking for).

Code: Select all | TOGGLE FULL SIZE
sudo apt install libgpiod2

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Wed Sep 16, 2020 4:46 pm

Mike,

Looks like it was there already:
Code: Select all | TOGGLE FULL SIZE
sudo apt install libgpiod2
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgpiod2 is already the newest version (1.2-3+rpi1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


I gave the script another try and still the same error messages.


Thanks,

Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Thu Sep 17, 2020 10:38 am

Hi Bob,

I think the libgpiod_pulsein has a runaway process in the background which is preventing you from access the pin. Killing that process (libgpiod_pulsein) should free up the pin.

Also there might be a subtle difference between running as root with 'sudo' and running as the pi user so please try both ways.

Also I think a force reinstall flag might need to be included to make these upgrades happen based on the output you shared with me.

Code: Select all | TOGGLE FULL SIZE
pip3 install --upgrade --force-reinstall adafruit-blinka

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Thu Sep 17, 2020 2:16 pm

Not quite sure on how to kill a process, new to all of this. I did a quick search but not really finding how to do that.

I did the install again and this is how that went:

Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~/Adafruit_Python_DHT $ pip3 install --upgrade --force-reinstall adafruit-blinka
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting adafruit-blinka
  Using cached https://www.piwheels.org/simple/adafruit-blinka/Adafruit_Blinka-5.4.0-py3-none-any.whl (132 kB)
Collecting Adafruit-PlatformDetect>=2.15.1
  Using cached https://www.piwheels.org/simple/adafruit-platformdetect/Adafruit_PlatformDetect-2.17.0-py3-none-any.whl (18 kB)
Collecting Adafruit-PureIO>=1.1.5
  Using cached https://www.piwheels.org/simple/adafruit-pureio/Adafruit_PureIO-1.1.5-py3-none-any.whl (12 kB)
Collecting rpi-ws281x>=4.0.0
  Using cached https://www.piwheels.org/simple/rpi-ws281x/rpi_ws281x-4.2.4-cp37-cp37m-linux_armv6l.whl (115 kB)
Collecting sysv-ipc
  Using cached https://www.piwheels.org/simple/sysv-ipc/sysv_ipc-1.0.1-cp37-cp37m-linux_armv6l.whl (68 kB)
Collecting pyftdi>=0.40.0
  Using cached pyftdi-0.51.2-py3-none-any.whl (150 kB)
Collecting RPi.GPIO
  Using cached https://www.piwheels.org/simple/rpi-gpio/RPi.GPIO-0.7.0-cp37-cp37m-linux_armv6l.whl (69 kB)
Collecting pyserial>=3.0
  Using cached pyserial-3.4-py2.py3-none-any.whl (193 kB)
Collecting pyusb>=1.0.0
  Using cached https://www.piwheels.org/simple/pyusb/pyusb-1.1.0-py3-none-any.whl (58 kB)
Installing collected packages: Adafruit-PlatformDetect, Adafruit-PureIO, rpi-ws281x, sysv-ipc, pyserial, pyusb, pyftdi, RPi.GPIO, adafruit-blinka
  Attempting uninstall: Adafruit-PlatformDetect
    Found existing installation: Adafruit-PlatformDetect 2.17.0
    Uninstalling Adafruit-PlatformDetect-2.17.0:
      Successfully uninstalled Adafruit-PlatformDetect-2.17.0
  Attempting uninstall: Adafruit-PureIO
    Found existing installation: Adafruit-PureIO 1.1.5
    Uninstalling Adafruit-PureIO-1.1.5:
      Successfully uninstalled Adafruit-PureIO-1.1.5
  Attempting uninstall: rpi-ws281x
    Found existing installation: rpi-ws281x 4.2.4
    Uninstalling rpi-ws281x-4.2.4:
      Successfully uninstalled rpi-ws281x-4.2.4
  Attempting uninstall: sysv-ipc
    Found existing installation: sysv-ipc 1.0.1
    Uninstalling sysv-ipc-1.0.1:
      Successfully uninstalled sysv-ipc-1.0.1
  Attempting uninstall: pyserial
    Found existing installation: pyserial 3.4
    Uninstalling pyserial-3.4:
      Successfully uninstalled pyserial-3.4
  Attempting uninstall: pyusb
    Found existing installation: pyusb 1.1.0
    Uninstalling pyusb-1.1.0:
      Successfully uninstalled pyusb-1.1.0
  Attempting uninstall: pyftdi
    Found existing installation: pyftdi 0.51.2
    Uninstalling pyftdi-0.51.2:
      Successfully uninstalled pyftdi-0.51.2
  Attempting uninstall: RPi.GPIO
    Found existing installation: RPi.GPIO 0.7.0
    Uninstalling RPi.GPIO-0.7.0:
      Successfully uninstalled RPi.GPIO-0.7.0
  Attempting uninstall: adafruit-blinka
    Found existing installation: Adafruit-Blinka 5.4.0
    Uninstalling Adafruit-Blinka-5.4.0:
      Successfully uninstalled Adafruit-Blinka-5.4.0
Successfully installed Adafruit-PlatformDetect-2.17.0 Adafruit-PureIO-1.1.5 RPi.GPIO-0.7.0 adafruit-blinka-5.4.0 pyftdi-0.51.2 pyserial-3.4 pyusb-1.1.0 rpi-ws281x-4.2.4 sysv-ipc-1.0.1


Still the same errors as before:
Code: Select all | TOGGLE FULL SIZE
Unable to set line 4 to input
Timed out waiting for PulseIn message. Make sure libgpiod is installed.
Timed out waiting for PulseIn message. Make sure libgpiod is installed.




Thank you,


Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Sat Sep 19, 2020 2:47 pm

Hi Bob,

The script should work when you first boot up your Pi.

If there is a process currently running and you try to run the script again you see the:

Code: Select all | TOGGLE FULL SIZE
 Unable to set line 4 to input


There are a few different commands that can be used for searching for a process.

Code: Select all | TOGGLE FULL SIZE
 pgrep libgpiod_pulsein

Code: Select all | TOGGLE FULL SIZE
 kill <PID found>


The second command is to kill the process ID of libgpiod_pulsein. I think you can also use:

Code: Select all | TOGGLE FULL SIZE
 killall libgpiod_pulsein


If none of the above commands are working the standard linux process search is:

Code: Select all | TOGGLE FULL SIZE
ps auxww | grep libgpiod

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Sun Sep 20, 2020 10:14 am

Hi Mike,

First, I tried running the script immediately after booting up but still got the same messages.

I must have a stubborn pi, it doesn't want to cooperate! lol

Using your process examples; This one (pgrep libgpiod_pulsein) gets no results.

This one (killall libgpiod_pulsein) says no process found.

The last one (ps auxww | grep libgpiod) does show one:

Code: Select all | TOGGLE FULL SIZE
pi@raspberrypi:~ $ ps auxww | grep libgpiod
pi        3025  0.0  0.4   7332  1876 pts/0    S+   09:57   0:00 grep --color=auto libgpiod


I am unable to kill this. I used the (kill 3025) and it says no process found? If I run the search again, that number changes. Keeps changing each time I search but maybe it's normal?

Thanks for sticking this out!


Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Mon Sep 21, 2020 11:39 am

Okay, good to know the reboot did not resolve this and that the process you are seeing is just your own search (notice the grep in there).

I think we have the software side worked out.

Can you show me a photo of your wiring for the DHT22 sensor to your Pi. I know you made the recommended code change from pin 18 to pin 4. Did you move the wire as well on the Pi?

Python Setup
Like
We're going to use a special library called adafruit_blinka (named after Blinka, the CircuitPython mascot) to provide the layer that translates the CircuitPython hardware API to whatever library the Linux board provides.

Installing CircuitPython Libraries on Raspberry Pi or BeagleBone Black
If you haven't set up your Raspberry Pi or BeagleBone Black for running CircuitPython libraries yet, follow our guide and come back to this page when you've completed the steps listed on the page and verified that your setup is working:

Setup your Linux Board for using CircuitPython Libraries
Installing the CircuitPython-DHT Library
You'll also need to install a library to communicate with the DHT sensor. Since we're using Adafruit Blinka (CircuitPython), we can install CircuitPython libraries straight to our small linux board. In this case, we're going to install the CircuitPython_DHT library. This library works with both the DHT22 and DHT11 sensors.

Run the following command to install the CircuitPython-DHT library:

pip3 install adafruit-circuitpython-dht

sudo apt-get install libgpiod2

Testing the CircuitPython DHT Library
To make sure you've installed everything correctly, we're going to test that we can read values from the DHT sensor connected to your device.

Create a new file called dht_simpletest.py with nano or your favorite text editor and put the following in:

Download: Project Zip or dht_simpletest.pyCopy Code
import time
import board
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D18)

# 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)
Next, you're going to need to modify a line of code in this file with information about the pin the DHT sensor is connected to and the type of DHT sensor you're using.

If you're using a Raspberry Pi with a DHT22 (or an AM2302) sensor connected to Pin 4, change the following line from:

dhtDevice = adafruit_dht.DHT22(board.D18)

to

dhtDevice = adafruit_dht.DHT22(board.D4)


https://learn.adafruit.com/dht-humidity ... thon-setup

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Mon Sep 21, 2020 2:50 pm

Image

Hope this picture works. I labeled the wires. The purple is the power wire going to 3.3volts. The white goes to ground and the center wire marked (out) on the sensor goes to gpio pin 4.

Just wanted to mention that I also have a Raspberry pi 4 and this script runs on it. It shows the temp and humidity in the SSH.

The one that is not working shown in picture is on a Pi zero W that I'm building a control panel for my garage heater. Not sure if there is a problem with the pi zero's?


Thanks,

Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Tue Sep 22, 2020 10:25 am

The picture did not come through.

Usually I select "Choose File" --> Add the File --> Place In line with images.

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Tue Sep 22, 2020 12:21 pm

Ok, this should work.

wiring.jpg
wiring.jpg (76.26 KiB) Viewed 1374 times

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Re: DHT22 sensor Unable to set line 4 to input

by mikeysklar on Wed Sep 23, 2020 12:25 pm

That wiring looks reasonable. I cannot confirm that pins 7 & 9 on the Pi are not swapped because they are both white coming from the sensor.

Is your sensor still using D4 in software? On the default is D18.

dhtDevice = adafruit_dht.DHT22(board.D4)

This DHT22 does not look like one of our own. Where did you buy it?

mikeysklar
 
Posts: 4576
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 sensor Unable to set line 4 to input

by bobs409 on Wed Sep 23, 2020 1:52 pm

A little hard to tell but the middle wire on the sensor is light gray, white for ground and purple for 3.3v. I believe this sensor was part of a variety type kit I have. I have tested this sensor on my Pi 4 and it does work on that one. I used the same wiring set up there too.

I have been using gpio 4 for the data wire. I have it set in the code too.

Why this all works on Pi 4 but not my Pi zero is odd.


Thanks,

Bob

bobs409
 
Posts: 13
Joined: Sat Sep 05, 2020 6:30 pm

Please be positive and constructive with your questions and comments.