Aug 27, 20084
arduino + photoresistor + led
Tags: arduino, led, photoresistor, wiring
i spent a little time last night playing with arduino and i thought i’d share this little experiment i put together.
Arduino + Photo Resistor + LED from Joshua McGinnis on Vimeo.
here’s the source:
/*
* Power LED on Dark by Joshua McGinnis
*
* This basic example uses a photo resistor to determine
* whether light is present or not. If it is not, an led
* will be set to high.
*
* http://www.joshuamcginnis.com
*/
int photoPin = 1; // designate pin 1 as the analog in for the photo resitor
int val = 0;
int ledPin = 13; // designate pin 1 as the analog in for the photo resitor
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT); // set the pin
pinMode(photoPin,OUTPUT); // set the pin
}
void loop() // run over and over again
{
val = analogRead(photoPin); // set val equal to the resitor input
if(val == LOW) { // if nothing from photo resistor (low light), turn on led
digitalWrite(ledPin, HIGH);
delay(500);
} else {
digitalWrite(ledPin, LOW);
}
Serial.println(val);
}
4 Comments, Comment or Ping
ril3y
Very cool stuff man… I just got my arduino trying to see all the different things people are doing with them..
-ril3y
http://www.synthetos.com
Feb 13th, 2009
alwolf
Hello, I copied and pasted your code above into the IDE and recieve this error:
In function ‘void setup()’:
error: ‘ledPin2′ was not declared in this scope
Do we need to initialize an “ledpin2″ like you did for ledPin?
Mar 3rd, 2009
John
pinMode(ledPin2,OUTPUT); // set the pin
should be
pinMode(photoPin,OUTPUT); // set the pin
May 27th, 2009
Joshua
thanks!
Jun 2nd, 2009
Reply to “arduino + photoresistor + led”