Skip to content

Instantly share code, notes, and snippets.

@bastos
Created May 9, 2009 21:10
Show Gist options
  • Save bastos/109405 to your computer and use it in GitHub Desktop.
Save bastos/109405 to your computer and use it in GitHub Desktop.
Light Sensor Example with Arduino
int LDR = 0; // select the input pin for the LDR
int lightOnPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int turnOnValue = 1000;
void setup() {
pinMode(LDR, INPUT); // declare the LDR as an INPUT
pinMode(lightOnPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(LDR); // read the value from the sensor
Serial.print(val);
if (val >= turnOnValue) {
digitalWrite(lightOnPin, HIGH); // turn the ledPin on
} else {
digitalWrite(lightOnPin, LOW); // turn the ledPin off
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment