DHT11 Humidity Sensor and Raspberry Pi


The humidity sensor, DHT 11, is the last sensor to be added to my Weather Balloon project. The board I am using is shown above, a resisistor is already connected between Vcc and the Output and so only three connections, Vcc, Output and Gnd are required. The sensor will operate at 3 volts as long as the connections are kept short.

I followed the Adafruit instructions found here. Once everything was installed I needed to use Python code to add to my existing code for the weather balloon. One of the examples in the Adafruit library was almost exactly what I required, but was designed to run from a command line, for example

AdafruitDHT.py 11 4

Where 11 is the sensors number (use 22 for the DHT22) and 4 is the GPIO pin used. The  DHT11 is connected to pin 7 (Output), pin 17 (3.3 volts) and pin 20 (Gnd). 

I modified the AdafruitDHT.py code by removing the part that collects the device number and GPIO pin from the command line

import sys

import Adafruit_DHT

# I am using a DHT11 on GPIO pin 4
humidity, temperature = Adafruit_DHT.read_retry(11, 4)

if humidity is not None and temperature is not None:
print 'Temp = {0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
print 'Failed to get reading. Try again!'

The code can now be run from within a python programme.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: