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:

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:

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

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:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
            "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
    except Exception as error:
        raise error


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)


dhtDevice = adafruit_dht.DHT22(board.D4)

If you're using a BeagleBone Black with a DHT22 (or an AM2302) sensor connected to Pin P8_11, change the following line from

dhtDevice = adafruit_dht.DHT22(board.D18)


dhtDevice = adafruit_dht.DHT22(board.P8_11)

If you're using a DHT11 sensor, you can change the sensor type by renaming the DHT22 class to DHT11:

dhtDevice = adafruit_dht.DHT11(board.D18)

Then, save the example. Next, run the example by typing the following command into the terminal:

python3 dht_simpletest.py

Press enter to run the example. You should see the temperature (in Fahrenheit and Celsius) and humidity values displayed in the terminal:

Temp: 73.4 F / 23.0 C    Humidity: 40.3%
Temp: 73.4 F / 23.0 C    Humidity: 40.3%

If your output looks like the output above - your linux board is set up for use with CircuitPython libraries and can read values from a connected DHT sensor.

If you do not see the correct values printed or if the script is unable to create a dhtDevice object

Check that the wiring is correct (Are ground and power connected? is the correct DHT pin pulled up to 3.3v?). Then, make sure you provided the dhtDevice with the correct physical pin number your board is connected to.

If you're receiving ImportError: No module named 'adafruit_dht' 

Make sure you installed the CircuitPython DHT library with pip3 and are running the command with python3

If running the script returns ImportError: No module named 'board' 

Make sure you're running the script with python3. If you're still receiving this error, follow the setup guide for installing CircuitPython libraries again.

This guide was first published on Aug 24, 2012. It was last updated on Jul 13, 2024.

This page (Python Setup) was last updated on Jul 13, 2024.

Text editor powered by tinymce.