import Adafruit_DHT # Set sensor type (DHT11) sensor = Adafruit_DHT.DHT11 # Set pin number pin = 4 # Read data from sensor humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # Print results if humidity is not None and temperature is not None: print('Temperature = {0:0.1f}C | Humidity = {1:0.1f}%'.format(temperature, humidity)) else: print('Failed to read data from sensor')
import Adafruit_DHT # Set sensor type (DHT22) sensor = Adafruit_DHT.DHT22 # Set pin number pin = 4 # Read data from sensor humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # Print results if humidity is not None and temperature is not None: print('Temperature = {0:0.1f}C | Humidity = {1:0.1f}%'.format(temperature, humidity)) else: print('Failed to read data from sensor')In both examples, we import the Adafruit_DHT library and specify which sensor we're using (DHT11 or DHT22). We then set the pin number that the sensor is connected to and read the data from the sensor. If the read was successful, we print the temperature and humidity values. Based on the import statement, the Adafruit_DHT library is likely located in a package called "Adafruit".