Step1 – Stuff you  need

This project involves using the RPi’s GPIO ports, and you will need some more kit than just the Pi. You will need:

 

If you don’t have any of this stuff I would recommend that you buy one of these Electronic Starter Kits For Raspberry Pi. They contain everything you need for this and many other projects.

Step 2 – Make the Resistor-Capacitor circuit

In order to measure light using the Raspberry Pi we are going to be using an Light Dependent Resistor(LDR) combined with something called a Resistor-Capacitor circuit. This simple circuit is a rough way of measuring changes in the amount of light entering the LDR.

The first thing you need to do is make the circuit.

Circuit Diagram and Breadboard Connections

 

ldr rpi connections_schem

Circuit Diagram

ldr rpi connections breadboard

Breadboard Diagram

  • Notice the black wire I used to connect the negative rail to the ground(GND) and the red wire I used to connect the positive rail to the +3V? This is a good habit to get in to as it helps remind you which is positive and which is negative.

When you are connecting up the capacitor, make sure you connect it the right way round. The negative side MUST be connected to the negative/ ground.  ( Notice the white strip on the capacitor above that is pointed towards the negative rail?)

capacitor intro

‘][sistoggle title=’Connecting up the LDR’ desc=’

ldr

The LDR can be connected up either way round safely.

When connecting to the Raspberry Pi will be using the Pin numbers, which works as you can see below. Have a look at your pi and see if you can see which pin is which.

raspberry pi gpio ports

Pin number 1 6 10
 Pin Function 3.3V Supply Ground Input/Output

 

In this tutorial we are talking to the GPIO ports using pin numbers, or GPIO.BOARD mode.  There is another way to talk to the GPIO ports, and this is known as GPIO.BCM mode and involves talking to the GPIO ports using all sorts of funny names, as can be seen here. Be aware of this when you are looking at code from different people on the web.

 

Step 3 – Power up the Raspberry Pi

Okay, so you have wired up all the components and you are ready to go? Not yet!!! First double check that all your components are wired up to the correct pins and that you capacitor is wired the correct way round! Get it wrong and you could brick your Pi!

 

LDR connected up to Pi

Here I’ve used a cobbler and rainbow connector lead to keep things nice and tidy. You don’t need to use these but it keeps things looking pretty.

 

It should now look something like the picture above. Here I have used a Pi-Cobbler and a base board to keep everything nice and tidy.

Step 4 -Create your python script

First you need to create the python file, using your editor of choice. Open lxterminal in your home directory and

type the following:

$ pi - nano ldrtimer.py

And enter the code below. Be careful to add the 4 spaces for indents, or you will get an error!

import RPi.GPIO as GPIO, time # Get all the libraries we need
GPIO.setmode(GPIO.BOARD) # Set the GPIO library to use pin numbers from the Pi Board

def timer (pin): # Create a new function
    reading = 0    # Create our counter and set it to zero
    GPIO.setup(pin, GPIO.OUT) # Set the pin to output
    GPIO.output(pin, GPIO.LOW) # Set the pin to low to discharge the capacitor
    time.sleep(0.1) # wait for 100ms whilst the capacitor discharges
    GPIO.setup(pin, GPIO.IN) # Set the pin to input
    while (GPIO.input(pin) == GPIO.LOW): # keep looping until the capacitor is charged and the input hits high
        reading += 1 # add one to our counter each loop
    return reading # when the loop finishes, return the reading
while True: 
    result = timer(10)
    print result

Save your file ( Cntrl + x, then y, then enter) and close the editor.

Step 5 – Run your script

$ pi - sudo python ldrtimer.py
Why do we need the sudo(administrator rights) command?You are talking to the GPIO ports and so you need special permission otherwise it work work.

Step 6 – Take it further

There are plenty of ways you can take this project further.

Here is an example where I connected the LDR Resistor-Capacitor circuit to a flappy bat game.

https://www.youtube.com/watch?v=rHDZKM6kIxU