Sense HAT science: humidity

By Russell Barnes. Posted

Create a humidity sensor and duplicate the experiment Tim Peake has been running on the International Space Station

In the past, we used the Sense HAT to investigate some of the properties of light and measure atmospheric pressure. This time, we’re going to experiment with another of the environmental sensors, and take a look at humidity and the things that can influence it.

The full article can be found in The MagPi 45 and was written by Richard Hayler

You'll need

A Sense HAT

twoDigits Python library

Some resealable sandwich bags, drinking straws, and sticky tape

Crew Detector code

Raspberry Pi Camera Module

Attach the Sense HAT to your Pi and power it up. Open IDLE3 or just type python3 in a terminal window, and then import the SenseHat library and connect to the board:

from sense_hat import SenseHat
sh = SenseHat()

As with pressure measurements, we can take a humidity reading with a single line:


This should produce a number like 32.87158203125 (if you just get 0 back, run the command again). What does this number mean?

A simple way to think of humidity is that it’s the amount of water vapour in the air. However, there are different ways of measuring humidity. Absolute humidity is the water content of air at a certain temperature and is normally measured in grams/m3 , whereas relative humidity is the amount of moisture in the air compared to what the air can ‘hold’ at that temperature, given as a percentage. The Sense HAT reports relative humidity; this is useful for weather forecasting as the higher the value, the greater chance of precipitation (rain, dew, or fog).

To make it easier to move around and measure differences in humidity, let’s make our Pi portable.

Making a sensor

From last issue, you’ll probably remember that the standard units for pressure are millibars, and normal values tend to be at least three or four digits long. Therefore, when displaying our measurements, we had to scroll them across the LED matrix. This time, we can easily round our humidity values to the nearest integer percent, because we should normally expect the readings to be 0-99%. Two digits is an ideal size for displaying a number on the LED matrix, and we’ve written a handy Python module that makes it simple. You can install it by typing this at the command line:

sudo pip-3.2 install twoDigit

Once that’s done, download or type up the code from the listing, then start the program running by typing:


Now disconnect the mouse, keyboard, and monitor. The humidity reading should be displayed on the Sense HAT LED matrix every half a second. Just as with the pressure measurements, red text is used if the value is higher than the previous one, and green text if lower.

Take your portable sensor for a walk and explore how humidity changes. You might notice a big change if you take the Pi outside. What differences do you see moving into a steamy kitchen or a bathroom?

Controlled environment

Obviously, a shower or a kettle can add lots of moisture to the air, but our bodies are also a great source of humidity. Let’s see how much effect we can have on our environment just through breathing. Take two sealable sandwich bags and snip the tip off one of the bottom corners on each. Cut off a short length of straw and poke an end through each of the corner holes, then use sticky tape to seal up the bags around the straw. Now pick one of the bags, snip off the other bottom corner and seal it round the remainder of the straw. Put the Pi and its battery into the first bag (the one with a single straw). Finally, seal both of the bags up, making sure that they’re not too inflated. Blow into the long straw and watch the readings increase. Can you cause a rise to 100% relative humidity?

You should quickly see why we used two bags rather than just one. The internal surfaces of the first bag will rapidly become covered in condensation, while the second – with the Pi inside – should stay drier. Always remember: electronics and water don’t go well together!

Our Code Club’s winning entry for the Astro Pi competition used the humidity sensor to try to detect astronauts on the International Space Station. You can download this code to see how well it works on Earth. Attach the Pi Camera Module and connect it back to mains power. Then type the following three commands:

git clone

The first thing you’ll see is a slightly self-indulgent display of animations on the LED matrix. While this is in progress, set up the Pi where you’re going to run the experiment. Once the animations have finished, you’ll see a message saying that the program is starting. Keep away from the Pi for five minutes while it takes its baseline measurements. Once it has finished, you’ll see a message reporting that the experiment has started. See how near you need to get to the Pi to trigger a reaction; once the code thinks there has been a significant rise in humidity, it will ask if anyone is there and take a picture with the camera. You may need to tweak some of the settings in the code depending on your environment; see the file for more information.

Code listing

import logging
from twoDigit.twoDigit import numToMatrix
from sense_hat import SenseHat
from datetime import datetime
from time import sleep

# Set up the logfile name based on date/time
logfile = "humidity-"+str(
# Logging settings and format for CSV
logging.basicConfig(filename=logfile, level=logging.DEBUG,
    format='%(asctime)s %(message)s',
    datefmt='%Y-%m-%d, %H:%M:%S,')

sh = SenseHat() # Connect to Sense HAT
h_old = 0
col = [0,0,255]

while True: # Main loop

   h = sh.get_humidity() # Take humidity reading # Log value to file
   if h > h_old: # If humidity has increased...
       col = [255,0,0] # Set to red
   else: # If humidity has decreased...
       col = [0,255,0] # Set to green

   h = int(round(h,0)) # Round to a whole percent
   i = numToMatrix(h,col) # Create 2-digit image for matrix
   sh.set_pixels(i) # Display reading
   h_old = h # Set previous value

From The MagPi store


Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.