Arduino

arduino + photoresistor + led

Added by on August 27th, 2008, filed under Arduino

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * 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);
}

Enjoy this Post?

Spread the word by promoting this post on FaceBook and Twitter.

Reader Comments (6)

  1. ril3y February 13, 2009 at 10:30 pm

    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

  2. alwolf March 3, 2009 at 6:01 pm

    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?

  3. John May 27, 2009 at 6:24 pm

    pinMode(ledPin2,OUTPUT); // set the pin

    should be

    pinMode(photoPin,OUTPUT); // set the pin

  4. ricky June 19, 2010 at 3:28 pm

    Its called a protoSheild not a borduino….but a borduino may be another name……

  5. Andy December 5, 2010 at 6:15 am

    you really should have a current limiting resistor on your LED — (see how bright it gets in your demo video). Try something in the range of 330ohms or so to prevent damage to your board. that, or use pwm.

What do you think?