Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more


Explore automatic input devices that measure physical properties, otherwise known as sensors.

In this step, you will be introduced to another class of input device: sensors. You will look at the types of data they return, and some of the things you need to consider when you are choosing sensors to use with Raspberry Pi.

What are sensors?

Sensors are input devices that are defined by two key traits:

  • They are used to measure or detect a physical property
  • They provide input constantly, rather than relying on human interaction

A physical property refers to things such as temperature or light levels; they are properties of an environment that we may want to measure and monitor with our programs. The input devices you have heard about so far are manual devices, meaning that they require a human to function. Sensors are built to measure properties constantly, and we can query the device to pull that data into our programs at any time.

Analogue versus digital

Using the GPIO pins on a Raspberry Pi, it is easy to send a signal to an output component and turn it on or off. You can also detect whether an input component is on or off quite easily. Components that operate in this way are called digital components because they have only two states.

A button is an example of a digital input component; it also has two states. When the button is pressed, it sends a 1 to the Raspberry Pi GPIO pin it is connected to. When the button is released, it sends a 0 to the GPIO pin. There is no other value that can be sent, as you can’t half-press a button.

An illustration of a button

Look at the graph below. This shows a button being pushed and released over time. When it is pushed, the Raspberry Pi reads a 1, and when it is released it reads a 0.

A graph showing a signal that changes vertically between 0 and 1, but does not take a value in-between.

Digital input components are easy to use with a Raspberry Pi as the GPIO pins are all digital. They can only send or receive 1s and 0s. Not all components are digital, though. Some are analogue components. Instead of having just two states, an analogue component provides a range of values that must be interpreted.

An example of an analogue input component is a light dependent resistor (LDR). When there is no light shining on the component it sends a 0; as light increases the value sent by the LDR gradually increases until it hits a maximum value of 1.

An illustration of an LDR

The graph below shows how the signal sent from an LDR will increase and decrease over the course of a 24-hour period as it goes from dark to light and back again.

A graph showing an analogue signal which changes continuously between values.

Using analogue components with Raspberry Pi is a little trickier than using digital ones. In this course you will use only digital components, but it would be good for you to understand how to use analogue components, too.

To use an analogue output component with the GPIO pins, you need to use a technique called pulse width modulation (PWM). This sends very rapid pulses of 1s and 0s to the component, which when taken as an average can be received as values between 1 and 0.

Another way you can use these devices is to connect them through an analogue-to-digital converter (ADC) that will turn analogue signals into digital signals. You can buy small ADCs for use in your circuits, and there are also add-on boards you can buy for Raspberry Pi with ADCs included, such as the Explorer HAT. Another option is to use a capacitor in your circuits along with the analogue component. An example of this can be found on the Laser Tripwire resource on the Raspberry Pi website.

The sensors you will be looking at in this course will all provide digital data that Raspberry Pi can easily use. This means they can be connected directly to the GPIO pins.

Your first sensor: The PIR

An illustration of a PIR sensor

A passive infrared sensor, or PIR sensor, detects movement. You may well have seen these before, as they are very common. You most often find them in the corners of rooms as part of burglar alarm systems. All objects whose temperatures are above absolute zero emit infrared radiation. The PIR sensor continuously measures the infrared signature of the room, and the sensor is triggered by any changes to this signature. As a person is hotter than their surroundings, the infrared signature changes when they move through a room, and the PIR sensor is triggered.

In the next step, you will complete a project using a PIR sensor and a buzzer to create an intruder alarm.

If you were to set up a motion-triggered alarm somewhere in your life, where would you place it? What would you want to catch? Leave your answers in the comments below.

This article is from the free online

Teaching Physical Computing with Raspberry Pi and Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now