arduino + photoresistor + led

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);
}

Published byJosh McGinnis

Josh is a software engineer, leader, startup advisor for the LA Chamber of Commerce and consultant residing in the Los Angeles area. Josh is passionate about helping entrepreneurs and businesses reach their maximum potential.

6 Comments

  • ril3y

    February 13, 2009 at 10:30 pm Reply

    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

  • alwolf

    March 3, 2009 at 6:01 pm Reply

    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?

  • John

    May 27, 2009 at 6:24 pm Reply

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

    should be

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

  • ricky

    June 19, 2010 at 3:28 pm Reply

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

  • Andy

    December 5, 2010 at 6:15 am Reply

    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.

Post a Comment