Using a temperature and humidity sensor to monitor a terrarium

Lead Image © lineartestpilot,

Sunny and Warm

An Arduino sensor, a Raspberry Pi, and a Python program help a budding naturalist keep tabs on the conditions in his snake's terrarium.

At the end of the last school semester, one of my young friends was allowed to purchase a pet snake (Figure 1). Because snakes are very sensitive to temperature and humidity variations, he wanted a cool way to keep an eye on what was happening inside his terrarium. "You have to push buttons on the old one to switch between temperature and humidity," he explained, which meant he had to reach into the terrarium periodically to check the conditions. To improve his setup, I hooked an Arduino sensor to a Raspberry Pi and wrote a graphing temperature/humidity logger that displays the conditions of the snake's habitat on a monitor.

Figure 1: Gabe and Jax waiting patiently for Jax's new, monitored environment.

The Sensor

The temperature/humidity (T/H) sensor is a DHT11 [1] that I purchased from my local electronics outlet. This particular module is installed on a carrier board (Figure 2), so the required pull-up resistors and electrical interface are already provided.

Figure 2: The T/H sensor (blue) mounted on a board with the necessary electronics.

The DHT11 uses a single data line both to receive commands and to transmit data; this line was connected to one of the GPIO pins. To talk with the sensor, the Raspberry Pi sets the GPIO line as an output and then has to pull the data line to ground (low logic state). After waiting 50 microseconds, the Rasp Pi releases the data line and then switches the GPIO pin to input (high logic state). The sensor then transmits its data. Short pulses are a binary 0 and long pulses are a binary 1. Once 40 bits are received, the data packet is complete.

Conveniently, Adafruit offers a free library that lets a Rasp Pi to talk to a DHT11 sensor and takes care of all of the high-speed bit fiddling and returns temperature and humidity as float values to Python. (See the "Getting the Library" box for instructions on installing the library.)

Getting the Library

To download, compile, and install the library, you need to run the following commands on the Pi terminal:

git clone
cd Adafruit_Python_DHT
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
sudo python install

Instructions are courtesy of the Adafruit temperature/humidity sensor tutorial [2].

Wiring the Sensor

Wiring the sensor is very simple. I power it directly from the Pi. Unfortunately, the pins on the Pi GPIO don't appear in the same order as the pins on the sensor. To solve this problem, I connected the sensor to the Rasp Pi with a couple of header jumper cables; then, I used breadboard jumpers to connect from the headers into the ribbon cable. The sensor plugs directly into the ribbon cable on the other end (Figures 3 and 4).

Figure 3: Wiring the T/H carrier board to the Pi.
Figure 4: The ribbon cable is about 6 feet long, so the board can reach well into the terrarium. You can see the previous thermometer in the background.

Buy this article as PDF

Express-Checkout as PDF

Pages: 7

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content