Materials:-
- Arduino uno = https://bit.ly/30v5XcY
- LDR sensor module = https://bit.ly/367jiK4
- Relay = http://bit.ly/38ToKBs
Diagram:-
Connections:-
LDR sesnor to Arduino
- Sensor D0 pin = Arduino digital pin 7
- Sensor GND pin = Arduino GND pin
- Sensor VCC pin = Arduino 5v pin
Relay to Arduino
- Relay VCC pin = Arduino 5v
- Relay GND pin = Arduino GND
- Relay IN2 pin = Arduino D13 pin
Code:-
const int light = 13;
void setup() {
pinMode(ldr,INPUT);//define pin as input sensor
pinMode(light,OUTPUT);//define pin as output sensor
}
void loop() {
int y =digitalRead(ldr);//read the sensor
if (y==1){
digitalWrite(light,HIGH);//turn on led
}else{digitalWrite(light,LOW);//turn off led
}
delay(50);
}