Introduction

While the Raspberry Pi can control digital inputs and outputs, what if you wanted to read analog signals? 

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!
    • 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

    • 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.

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

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

    • Join the gap in your Power rail.

    • Extend your Ground Rail

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

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

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

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

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

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

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

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

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

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

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

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

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

    • 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. 
      First, open a terminal window by clicking on the terminal icon on the top left hand corner. 

    • Type the following: sudo apt-get update 

    • Install GPIO Zero library using: sudo apt-get install python3-gpiozero

    • Click on the Raspberry Pi icon on the top left hand corner to access the main menu. 

    • Click on Programming > Python 3 (IDLE). 

    • Create a new file by clicking File > New File. 

    • Next, save the file by clicking File > Save, and naming it mcp3008.py

  1. from gpiozero import MCP3008
    from time import sleep
    
    ldr = MCP3008(0)
    
    while True:
        print(ldr.value)
    time.sleep(1.0)
    • 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' using:

      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:

          print(ldr.value)

      time.sleep(1.0)

  2. sudo python mcp3008.py
    
    • 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.