Created
February 12, 2022 12:34
-
-
Save arduinounomagic/cb009c688768060f383d433867f1c2f3 to your computer and use it in GitHub Desktop.
How to operate LDR using Arduino uno (with analog input)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*How to operate LDR using Arduino uno (with analog input) | |
*This code discribes that how an Light dependent resistor can be operated with analog input using Arduino Uno. | |
* | |
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]> | |
* | |
*for more detail please visit:https://www.arduinounomagic.com/2019/01/how-to-operate-ldr-using-arduino-uno.html | |
* | |
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/ | |
*/ | |
#define LDR A2 //Light dependent resisotr (LDR) connected to analog pin A2 | |
#define LED 5 //LED connected to digital pin 5 | |
int LDR_value; //variable to store result value read by LDR | |
void setup() | |
{ | |
Serial.begin(9600); //intialize the serial port for communicaion | |
pinMode(LED, OUTPUT); // set LED as output | |
pinMode(LDR, INPUT); //set light dependent resistor as input | |
} | |
void loop() | |
{ | |
LDR_value=analogRead(LDR); //read the data from analog pin A2 and store in LDR_value | |
if (LDR_value<=300) //if LDR_value value is less than or equal to 300 that means its dark outside so LED should be turn on | |
{ | |
digitalWrite(LED, HIGH);//turn on the LED | |
Serial.println("its dark turn on the LED");// display "its dark turn on the LED" on serial monitor and set the cursor to the new line | |
Serial.println("LDR value:"); //display "LDR value:" on serial monitor and set the cursor to the new line | |
Serial.println(LDR_value); // print the value of sensor | |
} | |
else //if LDR_value value is greater than 300 that means its bright outside so LED should be turn off | |
{ | |
digitalWrite(LED, LOW); //turn off the LED | |
Serial.println("its bright turn off the LED"); // display "its bright turn off the LED" on serial monitor and set the cursor to the new line | |
Serial.println("LDR value:"); //display "LDR value:" on serial monitor and set the cursor to the new line | |
Serial.println(LDR_value); // print the value of sensor | |
} | |
delay(200); //delay | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment