Analogue Vs. Digital
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 quite easily. 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.
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 release, 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.
Look at the graph below. This shows a button being pushed and released over time. When it is pushed it sends a 1 and when it is released it sends a 0.
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.
Not all components are digital however. 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.
An example of an analogue input component is a Light Dependent Resitor (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.
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.
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.
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.