Using sensors with a Raspberry Pi

One powerful feature of the Raspberry Pi is the row of GPIO (general purpose input/output) pins along the top edge of the board. These pins are a physical interface between the Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output).

Push Button

A push button is an input component that you can add to the Raspberry Pi GPIO pins. It will complete a circuit when the button is pressed. What that means is that a current will not flow across the button until it is pressed. When it is released, the circuit will be ‘broken’. Sensors are another type of input that can be connected to the Raspberry Pi. They may gather data about, for example, light or temperature conditions, but fundamentally they work in a similar way to the push button: they provide different input to the Pi depending on something that is happening externally.

Other sensors can be added to the pins, either as individual components connected to GPIO pins, or through an add-on board called a HAT — the name stands for Hardware Added on Top. Here are a few other popular sensors:

Individual Component Sensors

PIR Sensor

  • A passive infrared sensor or PIR detects movement. You have probably seen these before. You will most often find them in the corners of rooms for burglar alarm systems. Every object whose temperature is above absolute zero emits infrared (IR) radiation. The sensor measures the IR signature of the room it’s in, and then watches for any changes. Any object moving through the room will disturb the IR signature, and will cause a change to be noticed by the PIR module.

LDR Sensor

  • A Light Dependent Resistor or photocell is a component whose resistance will change depending on the intensity of light shining on it. It can therefore be used to detect changes in light. They are commonly used with street lighting, to make streetlamps turn on when it gets dark at night and off when it gets light in the morning.

Air Quality Sensor

  • An air quality sensor is used to determine air quality by detecting polluting gases. When air enters the sensor, it is energised by a small heater which allows its electrical resistance to be measured. This is done by passing a low level of electricity across a small gap of energised air. The more contaminated the air is, the less resistance it has and the better it will conduct electricity. The output of the sensor is therefore an analogue voltage that goes up and down according to how contaminated the air is. The more contaminants, the lower the potential difference (voltage) across the sensor.

Sense HAT

Sense Hat

The Sense HAT board for the Raspberry Pi has the ability to sense a wide variety of conditions and provide output via the built-in LED matrix. It was designed especially for the Raspberry Pi as part of the Astro Pi education programme, and there are two on board the International Space Station that can be programmed by competition winners from across ESA member states. The Sense HAT has the following sensors:

  • A gyroscope measures the orientation of an object. You may have seen a large one as a desktop toy. It has three separate axes pointing in different directions, so it can detect turning in all three directions. On a boat these directions are called pitch (the front of the boat rocks up and down), yaw (the front of the boat swings left and right), and roll (the boat rolls so that the right side is higher or lower than the left).

  • An accelerometer measures an object’s increase in speed (acceleration). At rest, it will measure the direction and force of gravity, but in motion it measures the direction and force of the acceleration acting on it — as if you were swinging it around your head on a rope. Because accelerometers can detect the direction of gravity, they are often found in devices that need to know when they are pointing downwards, such as a mobile phone or tablet. When you turn the screen sideways the accelerometer inside detects that the direction of gravity has changed, and therefore changes the orientation of the screen.

  • A magnetometer is used to measure the strength and direction of a magnetic field. Most often they’re used to measure the Earth’s magnetic field in order to find the direction of North. If your phone or tablet has a compass, it will probably be using a magnetometer to find North. They are also used to detect disturbances in the Earth’s magnetic field caused by anything magnetic or metallic; airport scanners use them to detect the metal in concealed weapons, for instance.

  • A temperature sensor is used to measure hot and cold. It’s exactly like the thermometer that you would put in your mouth to take your own temperature, except it’s an electronic one built into the Sense HAT and reports the temperature as a number in Celsius.

  • A humidity sensor measures the amount of water vapour in the air. There are several ways to measure it, but the most common is relative humidity. One of the properties of air is that the hotter it is, the more water vapour can be suspended within it. So relative humidity is a ratio, usually a percentage, between the actual amount of suspended water vapour and the maximum amount that could be suspended for the current temperature. If there is 100% relative humidity, it means that the air is totally saturated with water vapour and cannot hold any more.

  • A pressure sensor (sometimes called a barometer) measures the force exerted by tiny molecules of the air we breathe. There’s a lot of empty space between air molecules, and so they can be compressed to fit into a smaller space; the more air is squeezed into a space, and the smaller the space, the higher the pressure. This is what happens when you blow up a balloon. The air inside the balloon is slightly compressed and so the air molecules are pushing outwards on the elastic skin; this is why it stays inflated and feels firm when you squeeze it. Likewise, if you suck all the air out of a plastic bottle, you decrease the pressure inside it, and so the higher pressure on the outside crushes the bottle.

Explorer HAT

Explorer Hat

The Explorer HAT add-on board for the Raspberry Pi has useful input and output components built in as well as some useful sensors. In particular, it has eight capacitive touch pads.

  • A capacitive touch sensor detects when the metal pads connect with a person or object carrying a small electrical charge. Touching one of the eight capacitive touch pads on the Explorer HAT triggers an event, just like pressing a switch or button. Anything conductive, such as plants, fruits, and graphite pencils, can be attached to these sensors and become buttons.

After reading through the range of sensors mentioned in this article, what ideas do you have for the kind of thing you could make with them in your classroom? Please post your ideas in the comments section below.

Share this article:

This article is from the free online course:

Teaching Physical Computing with Raspberry Pi and Python

Raspberry Pi Foundation