In this Raspberry Pi light sensor tutorial I show you how to correctly connect the photo resistor sensor up to the GPIO pins. Lastly I show you how it can be used in a simple python script so you’re able to gather and use the data from it.
This is yet another sensor that I will be looking at incorporating into future projects such as a light activated alarm clock.
I explain a bit further down each of the parts that I will be using in this circuit. Be sure to read up on it if you need more information on these. It is important to note for this tutorial I am just using a simple photocell sensor whilst these are perfect for some tasks they might not be as accurate as you would like.
If you want to see visually step by step on how to set up the light sensor circuit & code, then be sure to check out the video below. It contains almost everything the text version of this tutorial does. You can find text instructions & information right underneath the video.
You will need the following equipment to be able to complete this Raspberry Pi light sensor tutorial. You can do this without any breadboard gear but I would highly recommend investing in some if you plan on doing a lot of circuitry work.
- Raspberry Pi
- 8 GB SD Card or Micro SD Card if you’re using a Raspberry Pi 2 or B+
- Ethernet Cord or Wifi dongle
- Light sensor (LDR Sensor)
- 1 1uF Capacitor
The Raspberry Pi Light Sensor Circuit
The circuit we are going to make for this tutorial is super simple and is great for anyone who is just starting out with circuitry.
The light dependent resistor or also known as the LDR sensor is the most important piece of equipment in our circuit (obviously). Without it we wouldn’t able to detect whether it is dark or light. In the light this sensor will have a resistance of only a few hundred ohms whilst in the dark it can have a resistance of several megohms.
The capacitor in our circuit is there so we’re able to measure the resistance of the LDR sensor. A capacitor essentially acts like a battery charging up whilst receiving power and then discharging when no longer receiving power. Using this in series with the LDR we can work out how much resistance the LDR is giving out thus whether it is light or dark.
To get the light sensor circuit built correctly follow the steps below or check out the circuit diagram right underneath the steps. In the following steps I am referring to the physical numbers of the pins (Logical order).
- First connect pin #1 (3v3) to the positive rail on the breadboard.
- Next connect pin #6 (ground) to the ground rail on the breadboard.
- Now place the LDR sensor onto the board and have a wire go from one end to the positive rail.
- On the other side of the LDR sensor place a wire leading back to the Raspberry Pi. Hook this to pin #7.
- Finally place the capacitor from the wire to the negative rail on the breadboard. Make sure you have the negative pin of the capacitor in the negative rail.
We’re now ready to move onto the Python code, if you have any trouble with the circuit refer to the diagram below.
The code for this project is pretty simple and will tell us roughly whether it is light, shady or completely dark.
The biggest problem we face with this circuit is the fact that the Pi doesn’t have any analogue pins. They’re all digital so we can’t accurately measure the variance in resistance on our input. This wasn’t a problem in the motion sensor tutorial since the output from it was either high or low (Digital). Instead we will be measuring the time it takes for the capacitor to charge and send the pin high. This is an easy but inaccurate way of telling whether it is light or dark.
I will briefly explain the Raspberry Pi light sensor code and what it does, if you just want the code you can simply copy and paste it below or download it from here.
To begin we import the GPIO package that we will need so that we can communicate with the GPIO pins. We also import the time package so we’re able to put the script to sleep for when we need to.
#!/usr/local/bin/python import RPi.GPIO as GPIO import time
We then set the GPIO mode to GPIO.BOARD this means all the numbering we use in this script will refer to the physical numbering of the pins. Since we only have one input/output pin we only need to set one variable. Set this variable to the number of the pin you have acting as the input/output pin.
GPIO.setmode(GPIO.BOARD) #define the pin that goes to the circuit pin_to_circuit = 7
Next we have function called rc_time that requires one parameter which is the pin number to the circuit. In this function we initialise a variable called count, we will return this value once the pin goes to high.
We then set our pin to act as an output and then set it to low, we then have the script sleep for 10ms.
After this we then set the pin to become an input and then we enter a while loop. We stay in this loop until the pin goes to high, this is when the capacitor charges to about 3/4. Once it goes high we return the count value to the main function. You can use this value to turn on and off an LED, activate something else or just log the data and keep statistics on any variance in light.
def rc_time (pin_to_circuit): count = 0 #Output on the pin for GPIO.setup(pin_to_circuit, GPIO.OUT) GPIO.output(pin_to_circuit, GPIO.LOW) time.sleep(0.1) #Change the pin back to input GPIO.setup(pin_to_circuit, GPIO.IN) #Count until the pin goes high while (GPIO.input(pin_to_circuit) == GPIO.LOW): count += 1 return count #Catch when script is interrupted, cleanup correctly try: # Main loop while True: print rc_time(pin_to_circuit) except KeyboardInterrupt: pass finally: GPIO.cleanup()
Deploying & The Code Running on your Raspberry Pi
This step is incredibly easy but I will quickly go through the steps so you can have it up and running on your Pi as quickly and smoothly as possible. Like all the tutorials on this website I am using Raspbian, if you need help on getting Raspbian installed then check out my guide on installing Raspbian.
Whilst all the software packages should already be installed in some cases it may not be. If you want to learn more about the GPIO pins and how to update/install the software be sure to check out my tutorial on setting up the GPIO pins on the Raspberry Pi.
You can simply download the code by using wget. The following command will do exactly just that:
sudo wget http://www.mediafire.com/download/7f4f4eh9u1yxixh/light_sensor.py
Alternatively, you can copy and paste the code just make sure the file is a python script.
sudo nano light_sensor.py
Once you’re done in the file simply use ctrl x then y to save and exit.
Finally run the code by using the following command:
sudo python light_sensor.py
I hope you now have the script working and you’re receiving data that correctly reflects the changes in light on the sensor. If you’re having trouble, please don’t hesitate to leave a comment below.
Improving Accuracy & Possible Uses
There are countless uses for a light sensor in a circuit. I will just name a few that I thought of whilst I was writing up this tutorial.
- Light Activated Alarm – I mentioned this one earlier but you can use the LDR to detect when it starts to get light so you can sound an alarm to wake up. If your program & sensor is accurate enough, you could have it slowly get louder as it gets lighter.
- Garden monitor – A light sensor could be used in a garden to check how much sun a certain area of the garden is getting. This could be useful information if you’re planting something that needs lots of sun or vice versa.
- Room Monitor – Want to make sure lights are always turned off in a certain room? You could use this to alert you whenever light is detected where it shouldn’t be.
There is so much you can do with this cool little sensor but also remember if you require something a little more accurate than the photocell try looking at something like the Adafruit high dynamic range digital light sensor.
I hope you have been able to set up this Raspberry Pi light sensor without any issues. If you do come across a problem, have feedback, I have missed something or anything else you would like to say then feel free to drop a comment below.
Credit: This tutorial is based off Adafruit’s Resistor Sensor tutorial.