Still not quite working?
You may be in a room which is
either too bright or dark. Try
turning the lights on or off to
see if this helps. Or if you have
a flashlight near by give that a
try.
It Isn't Responding to
Changes in Light.
Given that the spacing of the
wires on the photo-resistor is
not standad, it is easy to
misplace it. Double check its in
the right place
25
CIRC-09
Code
(no need to type everything in just)
Not Working?
(3 things to try)
More, More, More:
More details, where to buy more parts, where to ask more questions.
http://tinyurl.com/cpa83c
Making it Better
LED is Remaining Dark
This is a mistake we continue
to make time and time again, if
only they could make an LED
that worked both ways. Pull it
up and give it a twist.
Reverse the response:
Light controlled servo:
Perhaps you would like the opposite response. Don't
Lets use our newly found light sensing skills to control a servo
worry we can easily reverse this response just change.
(and at the same time engage in a little bit of Arduino code
analogWrite(ledPin, lightLevel); ---->
hacking). Wire up a servo connected to pin 9 (like in CIRC-04).
analogWrite(ledPin, 255 - lightLevel);
Then open the Knob example program (the same one we used
Upload and watch the response change.
in CIRC-08)
File > Examples > Library-Servo > Knob
.
Upload the code to your board and watch as it works
Night light:
Rather than controlling the brightness of the LED in
unmodified.
Using the full range of your servo:
response to light, lets instead turn it on or off based on a
You'll notice that the servo will only operate over a limited
threshold value. Change the loop() code with.
void loop(){
portion of its range. This is because with the voltage dividing
int threshold = 300;
circuit we use the voltage on analog pin 0 will not range from
if(analogRead(lightPin) > threshold){
digitalWrite(ledPin, HIGH);
0 to 5 volts but instead between two lesser values (these
}else{
digitalWrite(ledPin, LOW);
values will change based on your setup). To fix this play with
}
the val = map(val, 0, 1023, 0, 179); line. For hints on what to
}
do visit
http://arduino.cc/en/Reference/Map .
Download the Code from ( http://tinyurl.com/crdum6 )
(copy the text and paste it into an empty Arduino Sketch)
/*
}
* A simple programme that will change the
/*
* intensity of an LED based on the amount of
* loop() - this function will start after setup
* light incident on the photo resistor.
* finishes and then repeat
*
*/
*/
void loop()
{
//PhotoResistor Pin
int lightLevel = analogRead(lightPin); //Read the
int lightPin = 0; //the analog pin the
// lightlevel
//photoresistor is
lightLevel = map(lightLevel, 0, 900, 0, 255);
//connected to
//adjust the value 0 to 900 to
//the photoresistor is not
lightLevel = constrain(lightLevel, 0, 255);
//calibrated to any units so
//make sure the value is betwween 0 and 255
//this is simply a raw sensor
analogWrite(ledPin, lightLevel); //write the value
}
value (relative light)
//LED Pin
int ledPin = 9;//the pin the LED is connected to
//we are controlling brightness so
//we use one of the PWM (pulse
//width modulation pins)
void setup()
{
pinMode(ledPin, OUTPUT); //sets the led pin to
//output
24
CIRC-09
What We’re Doing:
.:Light:.
.:Photo Resistors:.
Whilst getting input from a potentiometer can be useful
for human controlled experiments, what do we use
when we want an environmentally controlled experiment?
We use exactly the same principles but instead of a
potentiometer (twist based resistance) we use a photo resistor (light based
resistance). The Arduino cannot directly sense resistance (it senses voltage) so we
set up a voltage divider ( http://tinyurl.com/2sunta ). The exact voltage at the
sensing pin is calculable, but for our purposes (just sensing relative light) we can
experiment with the values and see what works for us. A low value will occur when
the sensor is well lit while a high value will occur when it is in darkness.
The Circuit:
Wire
Photo-Resistor
x1
560 Ohm Resistor
Green-Blue-Brown
x1
2 Pin Header
x4
CIRC-09
Breadboard sheet
x1
Green LED
x1
10k Ohm Resistor
Brown-Black-Orange
x1
Schematic:
Arduino
pin 13
LED
resistor
(560ohm)
gnd
(ground) (-)
+5 volts
photo
resistor
resistor
(10k ohm)
Arduino
analog
pin 0
Parts:
.:download:.
breadboard layout sheet
http://tinyurl.com/cmzfdu
.:view:.
assembling video
http://tinyurl.com/cdldd6
The Internet