Use Analogue Sensors with Raspberry Pi


While the Raspberry Pi can control digital inputs and outputs, what if you wanted to read analog signals? That's when analog to digital (ADC) converters come in the picture.

In this guide, we'll connect up an Analogue to Digital Converter (the MCP3008) to the Raspberry Pi, and then connect a light dependent resistor (LDR) or light sensor to it. We will then program it to read and output values from the LDR.

After completing this guide, feel free to connect other analogue sensors to your Raspberry Pi!

Step 1  Insert the Pi Breakout Board

First, connect the T-Cobbler Plus breakout board to the breadboard. 

  • Attach it so that one side of it is between C1 to C20
  • Attach the other side so that it is between G1 to G20

Step 2  Insert the MCP3008

Connect MCP3008 to the breadboard.

  • So that one side is between B35 to B42 
  • The other side should be between F35 to F42

Note: The orientation of the MCP3008 matters here! Make sure that the divot, the little half circle indentation and dot on the MCP3008 is at B35 and F35.

Step 3  Create a 3V3 Power Rail

  • Connect a Red Jumper Wire between A1 to the Red Power Rail.

Step 4  Create a Ground Rail

  • Connect a Black Jumper Wire between A5 to the Blue Ground Rail.

Step 5  Extend your Power Rail

  • Join the gap in your Power rail.

Step 6  Extend your Ground Rail

  • Extend your Ground Rail

Step 7  Connect VDD to Power Rail

  • Run a Red Jumper Wire from G35 to the Power Rail to connect the MCP3008's VDD to power.

Step 8  Connect VREF to Power Rail

  • Run a Red Jumper Wire from G36 to the Power Rail to connect the MCP3008's VREF to power.

Step 9  Connect Analogue Ground to Ground Rail

  • Run a Black Jumper Wire from G37 to the Ground Rail to connect the MCP3008's Analogue Ground to Ground.

Step 10  Connect CLK to GPIO18

  • Run a Yellow Jumper Wire from G38 to J6 to connect the MCP3008's CLK to GPIO18.

Step 11  Connect Digital Out to GPIO23

  • Run a Green Jumper Wire from G39 to J8 to connect the MCP3008's Digital Out to GPIO23.

Step 12  Connect Digital In to GPIO24

  • Run a Blue Jumper Wire from G40 to J9 to connect the MCP3008's Digital In to GPIO24.

Step 13  Connect CS/SHDN to GPIO25

  • Run another Yellow Jumper Wire from G41 to J11 to connect the MCP3008's CS/SHDN to GPIO25.

Step 14  Connect Digital Ground to Ground Rail

  • Run a Black Jumper Wire from G42 to the Ground Rail to connect the MCP3008's Digital Ground to Ground.

Step 15  Insert 10K Resistor

  • Insert a 10K Ohm Resistor with one leg in H55 and the other in H58.

Step 16  Insert Photo Resistor

  • Insert a 10K Ohm Photo Resistor with one leg in G52 and the other in G55.

Step 17  Connect Photo Resistor to Ground

  • Run a Black Jumper between F52 and Ground to connect the Photo Resistor to Ground.

Step 18  Connect 10K Resistor to Power Rail

  • Run a Red Jumper between F58 and Power to connect the Resistor to the Power Rail.

Step 19  Connect Voltage Divider to Analogue In of MCP3008

  • Run a Green Jumper between F55 and D35 to connect the Voltage Divider to Analogue Channel 0 of the MCP3008.

Step 20  Install GPIO Zero Library

With a NOOBS microSD card, start up your Raspberry Pi and install Raspbian. Please follow our previous guide on doing just that if you are unsure how to. Alternatively, you could create your own NOOBS microSD card.

Now that the Raspberry Pi is connected to the Analogue to Digital Converter and light dependent resistor, we will program it. 

Let's install GPIO Zero, a Python library which builds upon existing GPIO libraries such as RPI.GPIO, rPIO, and pigpio. It helps to  simplify the process by reducing boilerplate code. 

  1. Open a terminal window by clicking on the terminal icon on the top left hand corner. 
  2. Type the following:
    sudo apt-get update 
  3. Install GPIO Zero library using:
    sudo apt-get install python3-gpiozero

Step 21  Create a file

  1. Click on the Raspberry Pi icon on the top left hand corner to access the main menu. 
  2. Click on Programming > Python 3 (IDLE). 
  3. Create a new file by clicking File > New File. 
  4. Next, save the file by clicking File > Save, and naming it

Step 22  The code

              from gpiozero import MCP3008
from time import sleep
ldr = MCP3008(0)
while True:

Firstly, we import the MCP3008 class from the GPIO Zero library by using: 

from gpiozero import MCP3008

Then we import sleep from the time library:

from time import sleep

Since the light dependent resistor is connected to channel 0, we refer to it using  MCP3008(0). We then place it in a variable called 'ldr':

ldr = MCP3008(0)

Finally, we use a never-ending while loop. It will print the value of the photoresistor, then pause for one second, and continually do so. 

while True:



Step 23  Run the code

              sudo python

Run the code from a terminal window with the following command. 

A value will be outputted to the terminal window every second. Move the light dependent resistor closer to or further away from a light source and watch the value change.