Arduino with darkness Sensor and LED (Code)

Arduino with darkness Sensor and LED

Today we shall learn to use a LDR with arduino.
The required kits are :

  1. An arduino and its adapter
  2. Few jumper Wires
  3. LDR with 10k OHM resistor
  4. A LED with 220 OHM resistor
  5. A breadboard
There are two legs on a LED. The longer leg is positive and the shorter leg is negative. LDR has too legs, too. The steps to setup hardware are listed below:
  • Fix the LED on the breadboard.
  • Connect longer LED leg to pin 12 on the arduino.
  • Connect shorter LED leg to GND pin on the breadboard through a 220 ohm resistor.
  • Connect 5V pin on the arduino to +VE on the breadboard .
  • Connect GND pin on the arduino to -VE on the breadboard .
Watch the video to learn to set up the LDR. The setup is complete. Now, upload the code and enjoy.

Check out the tutorial

(Code)

const int ledpin=12;
const int ldrpin=A2;
int darknesssensvalue=0;
int threshold=500;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(ledpin,OUTPUT);
pinMode(ldrpin,INPUT);
}
void loop() {
darknesssensvalue=analogRead(ldrpin);
if (darknesssensvalue<=threshold) {
digitalWrite(ledpin,HIGH);
delay(100);
digitalWrite(ledpin,LOW) ;
delay(100);
}
else {
digitalWrite(ledpin,LOW);
}
}

Comments

  1. Hi there,
    Thank you sharing such a useful information.
    We are leading motion sensors, smart lights, home automation products manufacturers in Vadodara

    Visit our website for more information - Smart Home Automation

    ReplyDelete
  2. Very good & informative content on topic
    Thanks for sharing such a useful tips.

    Visit our website for Web development company in Vadodara

    ReplyDelete
  3. Thank you for sharing such a useful information.

    iMagic Baroda
    Apple authorized store in Vadodara

    ReplyDelete

Post a Comment

If you have any problems, please do notify me.