Exercise 12 - Auto Brightness
Exercise 12 - Auto Brightness
Aim:
To design an IoT-based system that adjusts the brightness of an LED based
on the ambient light intensity sensed by an LDR (Light Dependent Resistor).
Algorithm:
Step 1: Start
Step 2: Set up pin A0 as INPUT to read the analog value from the LDR.
Step 3: Set up pin 6 as OUTPUT to control the LED brightness.
Step 4: Read the analog value from the LDR.
Step 5: Map the LDR reading to the brightness range.
Step 6: Adjust the brightness of the LED based on the mapped value.
Step 7: Repeat Step 3 to 5
Step 8: Stop
Program:
// Setup function to initialize pins and variables
void setup() {
// Set pin A0 as INPUT to read analog value from LDR
pinMode(A0, INPUT);
// Set pin 6 as OUTPUT to control LED brightness
pinMode(6, OUTPUT);
}
// Loop function to continuously read LDR value and adjust LED brightness
void loop() {
// Read analog value from LDR
int ldr = analogRead(A0);
// Map LDR reading to brightness range (0-255)
int bri = map(ldr, 0, 1023, 0, 255);
// Adjust LED brightness based on mapped value
analogWrite(6, bri);
}
Result:
Thus the experiment to design an IoT-based system that adjusts the brightness
of an LED based on the ambient light intensity sensed by an LDR (Light Dependent
Resistor) is written, executed and output is verified.