Example Code for Arduino-Ambient Light controlled LED
Last revision 2026/01/08
This guide explores how to control an LED with Arduino based on ambient light levels, featuring detailed instructions on hardware setup, software preparation, wiring, and sample code.
Wiring Diagram

Sample Code
/*
Ambient Light controlled LED
*/
int LED = 13; //Led pin
int val = 0;
void setup(){
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop(){
val = analogRead(0); // read voltage value
Serial.println(val);
if(val<1000){ // if the value is less than 1000,LED turns off
digitalWrite(LED,LOW);
}else{ // if the value is more than 1000,LED turns on
digitalWrite(LED,HIGH);
}
delay(10);
}
Was this article helpful?
