Download Worksheet 10 - Input sensors: Light sensors

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Switched-mode power supply wikipedia , lookup

Capacitor wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Rectiverter wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
technoJAM – Programming the Raspberry Pi Series
10 – Input Sensors: Light
Project
In this project, you will learn how to detect how bright it is in your room.
Components
In addition to your Raspberry Pi, you will need:
•
•
•
•
•
•
A breadboard
Two LEDs with associated current limiting resistors
Seven female-to-male jumper wires to connect your breadboard to the Raspberry Pi’s GPIO
Four male-to-male jumper wires for making connections on the breadboard
Light dependent resistor (LDR)
1μF electrolytic capacitor
Components
The buzzer supplied is an ‘active’ buzzer, which means that it only needs an electric current to make
a noise. In this case, you are using the Raspberry Pi to supply that current.
A Light Dependent Resistor (pictured above left), or LDR, is a resistor whose resistance changes
depending on how much light is falling on its surface. It will not accurately measure the light, but just
measures the change in light falling on the sensor. For example, it could be used to see whether it is
light or dark in a room.
A capacitor (pictured above right) is an electronic device that stores electric energy. It is similar to a
battery, but is smaller, lightweight and charges up much quicker.
As with batteries, there is a negative and a positive leg. The negative leg is the one that is usually
marked with a white bar.
In this circuit it is being used as a way to convert an analogue signal (the resistance of the LDR) by
timing how long it takes to charge the capacitor, which will depend on how much energy is supplied
to the capacitor, and in turn that is limited by the resistance the LDR gives to the flow of energy in
the circuit.
Created by Derek Hughes – 2015
1
www.technojam.uk
technoJAM – Programming the Raspberry Pi Series
Making the connections
Please note, as with the previous experiment, this experiment references the GPIO pins on the
Raspberry Pi differently than we have done in previous experiments. Previously, we have identified
GPIO pins by their physical number; pin 1 being in the top left corner and pin 26 being in the bottom
right corner of the GPIO connector.
This experiment references the pins by their ‘label’; GPIO 18 (physical pin 12), GPIO 22 (physical pin
15) and GPIO 24 (physical pin 18) – see diagram below for clarification.
Building the circuit
Build the circuit as shown in the diagram on the following page; you will need the buzzer and LEDs
on the right of the circuit for the second part of this experiment.
The left leg of the capacitor in the diagram is the negative leg, marked by the white bar. The
negative leg of the capacitor is connected to ‘ground’ on the Pi, and the positive leg is connected to
the 3.3v of the Pi via the LDR.
The resistance of the LDR will affect how quickly the capacitor will charge up, which is read by Pin 27
of the Raspberry Pi. The higher the resistance, the longer it takes to charge.
Created by Derek Hughes – 2015
2
www.technojam.uk
technoJAM – Programming the Raspberry Pi Series
You are now ready to write some code to switch the LEDs on and make the buzzer sound (please see
next page).
Created by Derek Hughes – 2015
3
www.technojam.uk
technoJAM – Programming the Raspberry Pi Series
The code
Create a new program called ldr.py and type the following code in to it…
#Load Libraries
import RPi.GPIO as GPIO
import time
# Set the GPIO naming convention
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# A variable with the LDR reading pin number
PINLDR = 27
def ReadLDR():
LDRCount = 0 # Sets the count to 0
GPIO.setup(PINLDR, GPIO.OUT)
GPIO.output(PINLDR, GPIO.LOW)
time.sleep(0.1) # Drains all charge from the capacitor
GPIO.setup(PINLDR, GPIO.IN) # Sets the pin to be input
# While the input pin reads ‘off’ or Low, count
while (GPIO.input(PINLDR) == GPIO.LOW):
LDRCount+= 1 # Add one to the counter
return LDRCount
while True:
print ReadLDR()
time.sleep(1) # Wait for a second
Now run your program by typing…
sudo python ldr.py
Created by Derek Hughes – 2015
4
www.technojam.uk
technoJAM – Programming the Raspberry Pi Series
Experiment with the program to see what happens when no light is allowed to reach the LDR, when
maximum light is allowed to reach the LDR and when a really bright light (light on your phone) is
shone on the LDR.
How the circuit works
So, how does the circuit measure how much light is falling on the LDR?
When the function ReadLDR is called, the measurement pin (GPIO27, physical pin 13) is first set to
be an output pin and is set to ‘low’, or 0 volts, for a short time. This will empty the capacitor of
charge.
The measurement pin (GPIO27) will then be set as an input pin, which will then detect the voltage
across the capacitor. Because the Raspberry Pi GPIO input is digital only, it only knows when the
input is either ‘off’ (0v) or on (3.3v). The Pi actually considers any voltage on an input pin that is
between 0 and about 1.4v to be ‘off’ (or 0), and anything between 1.4v and 3.3v to be ‘on’.
As the capacitor charges up, the Pi is able to time how long it takes for the input pin to change from
‘off’ to ‘on’ by using a simple counter (LDRCount).
LDRCount represents how much light is on the light dependent resistor. The more light, the lower
the resistance the LDR provides, and therefore the quicker the capacitor charges and the lower the
value of LDRCount.
LDRs are not accurate pieces of electronics. Each one will differ. Therefore, they cannot be used to
accurately measure how bright the light falling on them is. In addition, if other programs are running
on the Pi, the counter loop may run a little slower.
How the program works
The code that starts def ReadLDR(): is declaring/defining a function; a function is a piece of code
that can be executed several times within a program. As the code is read by Python and executed
from top to bottom, the indented code beneath the function declaration is skipped and control
passes to the loop that starts while True:.
Within the ‘while’ loop there is the command print ReadLDR(), it is this instruction that ‘calls’ the
function (executes the indented code beneath the function declaration).
The code within the function discharges (empties the capacitor) and then increments a counter
(LDRCount) as the capacitor charges up.
The last line of the function returns control back to the instruction that called it (print ReadLDR())
together with the value that is stored in the LDRCount variable.
That function calling instruction (print ReadLDR()) is then executed and it displays the value
contained in the LDRCount variable on the screen.
Created by Derek Hughes – 2015
5
www.technojam.uk
technoJAM – Programming the Raspberry Pi Series
The program then waits one second and loops back around to ‘call’ the ReadLDR function again.
To break out of the program, press <CTRL>+<C>.
Challenge
Extend the code above to turn on the LEDs under different conditions. As mentioned above, each
LDR measures light brightness differently, therefore you are free to choose what light level you turn
the LEDs on and off.
•
•
•
When the light falling on the LDR is bright enough, light the red LED.
When it goes below a certain level, turn on the BLUE LED.
If the light gets really bright, sound the buzzer.
Acknowledgments
This worksheet is a modified version of one that was first published on the Cambridge Raspberry Jam
web site 1.
1
https://www.dropbox.com/s/g5ynqxop9vwxk7p/CamJam%20EduKit%20Sensors%20Worksheet%204%20%20Light.pdf?dl=0
Created by Derek Hughes – 2015
6
www.technojam.uk