In this tutorial we use a photo-resistor light sensor to measure the intensity of ambient light. Find out with a Python script on your Raspberry Pi, if it’s dark or light. When you have completed this tutorial, you will be able to connect a light sensor module with a digital output to your Pi via the GPIO pins. You’ll also have the basic code to convert the output signal of the sensor to usable information in your Python script.
What you’ll need for this tutorial
First, you need to have a Raspberry Pi 3 running on the latest version of Raspbian. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “First Python program on the Raspberry Pi” and/or “Raspberry Pi first program using GPIO” to have a quick introduction.
Next you’ll need some extra hardware:
- a breadboard (we are using a 400 points breadboard)
- a light sensor module with digital output
- Dupont jumper wires
- a T-cobbler (optional)
- a 40 pin GPIO cable (optional)
If you miss any equipment, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.
The light sensor module
The light sensor module is equipped with a LDR (Light Dependent Resistor). The LDR has a variable resistance that changes with the light intensity that falls upon it. This allows the component to measure the intensity of the ambient light.
The resistance of the LDR decreases when the intensity of the light increases. It results in a analogue signal which is not suitable for the GPIO-pins of our Raspberry Pi. For this reason we use a module equipped with the LDR of course but also with a voltage comparator and a potentiometer. These components allow us to have a digital output with an adjustable set point:
- light intensity > set point : output = low
- light intensity < set point : output = high
The output signal of the “DO” pin can be connected directly to a GPIO-pin of our Raspberry Pi. The operating voltage of the module is 3.3V ot 5V.
Some light sensor modules have a fourth pin, this is the analogue output (left pin on the picture). This output signal can’t be connected directly to the GPIO-pins of the Raspberry Pi. It needs a more complex circuit or another type of board which is capable of reading analogue signals.
And finally, the module is equipped with 2 LEDs: to indicate that the module is on and to indicate the measured state of the ambient light.
Setting up the hardware part
Before starting connecting wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable of the board!
- connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
- plug the cobbler onto the breadboard as shown in the figure above or below
- plug the other end of the 40 pin cable in the T-cobbler
- connect the VCC pin of the sensor with a 5V pin (red wire)
- wire the DO pin of the sensor to pin 23 (yellow wire)
- connect the GND of the sensor to a GPIO GND pin (black cable)
Writing the code
The aim of this tutorial is to write a very simple Python script that allows us to visualize when dark or light is detected by the module. To write the code, we use the Thonny IDE. You can find Thonny under the application menu of your Raspberry Pi.
Write or paste following code in the IDE:
import RPi.GPIO as GPIO
import time GPIO.setmode(GPIO.BCM)
LIGHT_PIN = 23
lOld = not GPIO.input(LIGHT_PIN) print('Starting up the LIGHT Module (click on STOP to exit)')
time.sleep(0.5) while True:
if GPIO.input(LIGHT_PIN) != lOld:
lOld = GPIO.input(LIGHT_PIN)
Some explanations about the code:
GPIO.setmode (GPIO.BCM): The GPIO.BCM option means that we are referring to the pins by the “Broadcom SOC channel” number, these are the numbers after “GPIO”
GPIO.setwarnings (False): We use this line of code to avoid warning messages.
GPIO.setup(LIGHT_PIN, GPIO.IN): We define the light sensor-pin (= 23) as an input pin
lOld = not GPIO.input(LIGHT_PIN): We use a variable “lOld” to retain the last sensor status, this will allow us to compare the last measured value with the previous one and only print when the value has changed. To be sure to print the status when launching the script, we assign the opposite value of the light sensor to this variable.
time.sleep(0.5)We wait for 0.5 second to settle the sensor
while True:is an infinitive loop (until we stop the program)
- Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the code line after the while command.
if GPIO.input(LIGHT_PIN) != lOld:if the sensor-pin value is different from the previous measured status
if GPIO.input(LIGHT_PIN):if the sensor-pin is high, the light intensity is low => dark
print ('\u263e')print the moon symbol (ASCII code), feel free to change the ASCII code into text of your choice
print ('\u263c')print the sun symbol (ASCII code)
lOld = GPIO.input(LIGHT_PIN)we save the last measured status in the lOld variable
time.sleep(0.2)Waiting for 0.2 second before we start the loop again
Once you are done with the code, click on the Run button. You will see the status of the light intensity appear on the screen. If the light intensity changes, above or below the set point, the new status will be printed on the screen.
Feel free to adjust the potentiometer on the module to adjust the set point.
To stop the program just click on the STOP button.
Congratulations! With this setup you can define the light intensity status of ambient light. You can further use the script to process the result in an application where you want to control a light bulb for example. Have fun with it!