Devices & Components
Arduino Uno Rev3
High Brightness LED, White
LDR, 5 Mohm
Darlington High Power Transistor
Hardware & Tools
Breadboard, 270 Pin
Project description
Code
program to control LED light intensity as per LDR input
c_cpp
the LED light intensity varies as per the ambient light sensed by LDR
1#include <LiquidCrystal.h> 2 3LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 4const int LDR_pin = A0; // pin that the LDR sensor is attached to 5const int set_pot_pin = A1; 6const int led_light_pin = 9; // pin that the LED array is attached to 7 8// variables: 9int sensorValue = 0; // the sensor value 10int sensorMin = 1023; // minimum sensor value 11int sensorMax = 0; // maximum sensor value 12int threshold = 0; 13int light_intensity = 55, led_intensity = 20; 14void setup() 15{ 16 lcd.begin(16, 4); 17 lcd.clear(); 18 lcd.print("calibrating....."); 19 analogWrite(led_light_pin, 0); 20 digitalWrite(13, HIGH); 21 while (millis() < 10000) 22 { 23 sensorValue = analogRead(LDR_pin); 24 if (sensorValue > sensorMax) sensorMax = sensorValue; 25 if (sensorValue < sensorMin) sensorMin = sensorValue; 26 } 27 lcd.clear(); 28 lcd.print("calibration over"); 29 lcd.setCursor(0, 1); 30 lcd.print("max light:"); 31 lcd.print(sensorMax); 32 lcd.setCursor(0, 2); 33 lcd.print("min light:"); 34 lcd.print(sensorMin); 35 delay(2000); 36 lcd.clear(); 37 lcd.print("light intensity"); 38 lcd.setCursor(0, 2); 39 lcd.print("threshold:"); 40 lcd.setCursor(0, 3); 41 lcd.print("LED light:0%"); 42} 43 44void loop() 45{ 46 sensorValue = analogRead(LDR_pin); 47 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 100); 48 sensorValue = constrain(sensorValue,0,100); 49 lcd.setCursor(0, 1); 50 lcd.print(sensorValue); 51 lcd.print('%'); 52 threshold = analogRead(set_pot_pin); 53 threshold = map(threshold, 0, 1023, 0, 100); 54 lcd.setCursor(11, 2); 55 lcd.print(threshold); 56 lcd.print('%'); 57 if (sensorValue < threshold) 58 { 59 analogWrite(led_light_pin, light_intensity); 60 lcd.setCursor(11, 3); 61 lcd.print(led_intensity); 62 lcd.print('%'); 63 if(light_intensity<255) 64 { 65 light_intensity += 25; 66 led_intensity += 10; 67 } 68 } 69 delay(2000); 70} 71
Downloadable files
close loop control demo with arduino using LED and LDR
it controls LED light intensity as per ambient light sensed by LDR
close loop control demo with arduino using LED and LDR

close loop control demo with arduino using LED and LDR
it controls LED light intensity as per ambient light sensed by LDR
close loop control demo with arduino using LED and LDR

Comments
Only logged in users can leave comments