Example Code for Arduino-Ambient Light Sensor Control
This article offers a detailed guide on controlling ambient light sensors using Arduino, including hardware setup, software preparation, wiring diagrams, and sample code execution for effective sensor management and real-time LED control.
Hardware Preparation
- UNO Dashboard x1
- DFR0375 Expansion Shield x1
- Analog Sensors (Ambient Light Sensor) x1
- Jumper Cables
Software Preparation
Arduino IDE V1.6.5 click to download Arduino IDE
Diagram

Sample Code
int light=0;
void setup() {
// put your setup code here, to run once:
pinMode(3,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
light=analogRead(0);
Serial.println(light);
if(light>100)
digitalWrite(3,HIGH);
else digitalWrite(3,LOW);
delay(100);
}
Result
When there is low ambient light, the LED will turn ON.
Was this article helpful?
