/* turns on and off a light emitting diode(LED) depending on ambient light (using a photocell). Project tutorial: http://osoyoo.com/?p=123 */ int photocellPin = 2; // select the input pin for the photocell int ledPin = 13; // select the pin for the LED int val = 0; // variable to store the value coming from the sensor void setup() { pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT pinMode(photocellPin, INPUT); // declare the ledPin as an OUTPUT } void loop() { val = analogRead(photocellPin); // read the value from the sensor if(val<=512){ digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } }