Skip main navigation

Analogue vs digital

How do you read analogue signals from sensors and components connected to the Raspberry Pi digital GPIO pins?
© CC BY-SA 4.0

Using the GPIO pins on the 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. Components that operate in this way are called digital components.

An LED is an example of a digital output component. It can either be on or off, and there is no value in-between. We can think of the on and off states as being either 1 or 0. You can send a 1 to the LED to illuminate it on and a 0 to the LED to turn it off again.

led

A button is an example of a digital input component. It can either be on or off as well. 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.

button

Look at the graph below. It shows the result of a button being pushed and released over time. When it is being pushed it sends a 1, and when it is released it sends a 0.

digital graph

Digital input and output components are easy to use with the Raspberry Pi, as the GPIO pins are all digital. They can only send or receive 1s and 0s.

However, not all components are digital. Some are called analogue components. Analogue components can send and receive values in-between 1 and 0.

A motor is an example of an analogue output component. You can send it values between 1 and 0, which will control the speed of the motor. If you send the motor a 1 it will drive at full speed. If you send it 0.5 it will drive at half speed. Sending a 0 will stop the motor.

motor

An example of an analogue input component is a Light Dependent Resistor (LDR). When there is no light shining on the component it will send a 0, and as light increases, the value sent by the LDR will gradually increase until it hits a maximum value of 1.

LDR

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

analogue graph

Using analogue components with the Raspberry Pi is a little trickier than using digital components.

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 in between 1 and 0.

Look at the graph below. The blue line shows the digital signal, over a period of time, moving from 0 to 1 and back again. The signal is 1 for a third of the total time and 0 for the remaining two thirds. This then averages out at around 0.33, which would be the value that is received by the analogue component. You can see this as the red line on the graph.

pwm

To use an analogue input component with the GPIO pins, you need to use an Analogue to Digital Convertor (ADC), that will turn analogue signals into digital signals. Although you can buy small ADCs for use in your circuits, there are several add-on boards you can buy for the 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.

How would you explain the differences between analogue and digital to your students?

Use the comments space below to answer.

© CC BY-SA 4.0
This article is from the free online

Teaching Physical Computing with Raspberry Pi and Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education