Light Dependent Resistors 1. Light dependent resistors in serial

advertisement
Aberystwyth Robotics Club - sensors workshop – Light
Dependent Resistors
1. Light dependent resistors in serial
Wire up an Arduino to a Light Dependent Resistor (an LDR) and a LED in serial: from pin
13 of the Arduino, to the LED, to the LDR, then back to ground on the Arduino.
Use a breadboard: remember how they're wired up.
Install the “Blink” sketch on the Arduino, which should make the LED on pin 13 blink. This
LED is attached to an LDR now which means that the resistance will change depending
upon the amount of light that gets to the LDR. Play around with this – use your hands to
cover the LDR, shine a torch on it... and see what happens to the light.
Outcomes: remember about breadboards, remember about circuits, learn about light
dependent resistors. Think about what a sensor is.
---------------------------------------------------------------------------------------2. Reading the value of an
LDR using the serial
monitor
Using
- a 10kΩ resistor
- the LDR
- and 5 wires
Set up your breadboard and Arduino
like the picture here.
This uses a large resistor (10kΩ) which sits between the LDR and the ground pin – that
effectively protects the Arduino.
First, you need to change your code. You can do this just by editing the blink code from
step one.
Remember that all Arduino programs have two parts: the setup which happens once, and
is used to set up anything that you need to use in the program. This is where you tell the
Arduino which components are wired up to which pin, and stuff like that. There is also a
loop, and the loop is repeated again and again until the Arduino is turned off (or runs out
of battery power).
What we are going to do is to read the value of the LDR using pin A0 (one of the
Arduino's analogue input pins). Then we are going to write that out to the Serial
monitor – which is a way of getting the Arduino to print values to the computer screen.
There are step-by-step instructions on the next page.
a) Save the blink code using a different name.
b) In the setup function, we need to tell the Arduino which pin has the LDR on it, and we
need to tell the Arduino to start the Serial monitor. So edit your code so that the setup
function looks like this:
void setup() {
pinMode(0, INPUT);
Serial.begin(9600);
}
This says that we are setting up pin 0 as input (that is the number zero, not a letter o).
This is the pin we have connected to the LDR. It also sets up the serial monitor so we are
ready to start monitoring stuff.
c) In the loop function, we need to read the value of the resistor and write it out to serial.
void loop() {
int d = analogRead(0);
Serial.print(“The value of the LDR is: “);
Serial.println(d);
delay(1000);
}
Notes:
- Serial has a capital S.
- Serial.print(“something”); will print the word something on the serial monitor
- Serial.print(s); will print the value of the variable s on the serial monitor
- Serial.println(s); will print the value of the variable s on the serial monitor and then a
new line (as if you have pressed return at the end)
- We are using the number zero in the analogRead statement, not the letter o.
d) When you have typed this in, check it by clicking on the tick, then upload it to your
Arduino by clicking on the arrow button.
To open the serial monitor, click on the
button that looks a bit like a magnifying glass
in the top right corner of the Arduino IDE
window (it will say “Serial Monitor” when you
hover over it). That will open up a new
window which contains the stuff you have printed to Serial.
Play around with the circuit – what happens when you cover the LDR? What happens
when you shine a torch on it?
Outcomes: capture the value from a sensor in a variable, read from an analogue input
pin, use the Serial monitor to see some output, monitor the value of a variable.
---------------------------------------------------------------------------------------3. Using the value of an LDR to switch the onboard Arduino
LED on and off appropriately
There is an LED wired into the board on Pin 13 (we have used this before). This activity
takes the value of the LDR (which we found out in the last activity) and uses it to switch
on the LED, but only if it is dark.
a) Find out what kind of value is dark, and what kind of value is light, using the program
you wrote in step 2. Is it big when it is dark, and small when it is light? Or the other way
around?
b) Modify the setup function in the code so that it uses pin 13 as an output pin, by
adding this line:
pinMode(13, OUTPUT);
c) Modify the loop function in the code so that it turns on pin 13 when it gets dark.
To turn it on you will need this code:
digitalWrite(12, HIGH);
To turn it off again you will need this code:
digitalWrite(13, LOW);
To work out when you need to turn it on, and when you need to turn it off, you'll need to
think. Is it dark when the value of d is high? Or is it dark when the value of d is low?
Can you work it out? Here is a bit of code which switches on the light when the value of d
is bigger than 500:
if (d > 500) {
digitalWrite(13,HIGH);
}
That might be a sensible thing to do but you'll have to work it out for yourself!
---------------------------------------------------------------------------------------4. Add an LED to your breadboard
Next step involves using LEDs on your breadboard.
a) Put an LED on one corner of the breadboard, next to an LDR. You might have to move
things around. Wire it up with a resistor of X ohms. If you're not sure how to wire it up,
ask someone.
b) Change your code so it lights up the new LED, not the one on the Arduino.
c) Test your code.
----------------------------------------------------------------------------------------4. Add another LDR and another LED
What we want now is another LDR and another LED. This is a bit tricky but you've already
done one so it should be possible! Try to get them on the corners of the breadboard, a
bit like eyes.
The steps are:
a) Move the first LDR and LED so they're in the corner of the breadboard.
b) Test the code so you know it still works.
c) Add the second LDR, on the other corner of the breadboard. You can wire up exactly
the same way you did with the first (but on a different analogue pin). Look at page one of
this document if you need to see a picture.
d) Work out what pin it is on: I'd recommend wiring it to Analogue pin 1 (A1 on the
board).
e) Add a line to the setup function telling the program what pin the LDR is on.
f) Add lines to the loop function which read this second LDR and print its value to Serial
g) Add another LED next to the second LDR
You now have a breadboard with two sensors – like two eyes.
Can you program it so that the light by LDR2 comes on when it's dark at LDR1?
Can you program it so that the light by LDR1 comes on when it's dark at LDR2?
----------------------------------------------------------------------------------------5. Stretch activity
Using random numbers, make a game. Here's one idea (but if you have other ideas that
is fine).
• Set a score variable to be zero
• Choose a random number between the minimum and maximum resistance, and
call this target
• Print the target's value on the serial monitor
• When the actual, measured resistance is near target, print “well done” and add
one to the score
• Choose another random value for target and start again, keeping score all
along.
Download