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 or switch 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 work in the same way as a button or switch.

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 which stands for Hardware Added on Top. Here are a few other popular sensors:

Individual Component Sensors

PIR Sensor

  • A Passive Infra Red sensor or PIR detects movement. You might have seen these before as they are very common. You would most often find them in the corners of rooms for burglar alarm systems. All objects whose temperatures are above absolute zero emit infra red radiation. The sensor measures the infra red signature of the room it’s in and then watches for any changes. Any object moving through the room will disturb the infra red signature, and will cause a change to be noticed by the PIR module.

LDR Sensor

  • An Light Dependent Resistor or photocell is a component whose resistance will change depending on the intensity of light shining upon it. It can therefore be used to detect changes in light. They are commonly used in street lighting to turn on when it gets dark at night and turn 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 (like a variable resistor). 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 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. It can be quite difficult to visualise if you’ve never used one before, but you may have seen one being used for a fairground ride. The ride, an Aerotrim, takes only one person who is strapped in by their legs and arms. They sit inside three big metal rings, which can independently rotate in relation to each other. The ride operator usually says “Scream if you want to go faster!” and sets you going; you then go upside down, back to front and side to side all at once, while screaming. This ride is a gyroscope as it allows three degrees of movement. These are normally called: Pitch (up and down like a plane taking off and landing), Yaw (left and right like steering a car), and Roll (imagine a corkscrew movement, like barrel rolling a fighter jet).
  • 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 main 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 to the maximum amount that could be suspended for the current temperature. If there was 100% relative humidity, it would mean 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; 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’re decreasing 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 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, acting as a switch or button. Besides human beings, lots of objects like plants, fruits and graphite pencils can become buttons.

After reading through the small 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