Well Guys!!
I'm back with another cool instructable, in this Instructable you will Control Servo by varying the intensity of light incident to the LDR.
Generally, LDR is a Light Dependent Resistor, whose resistance changes depending upon the intensity of the light incident on it.
Connection of LDR
Output is analog in nature, so it gets connected to the A0 pin of the NodeMCU.
Connection to Servo
Orange wire connects to Digital pin D4.
Brown wire connects to GND pin.
Red wire connects to 3v3 Pin.
Connections are quite simple right, just wire your prototype up like the schematic.
To know how LDR works with simple code you can check out my previous Instructable.
#include <Servo.h>
Servo servo;
const int ldrPin = A0;
void setup() {Serial.begin(9600);
pinMode(ldrPin, INPUT);
servo.attach(2); //D4 servo.write(0);
delay(2000);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <=300) {
servo.write(0);
delay(1000); Serial.println("Servo Closed");
}
else {
servo.write(90);
delay(1000);
Serial.println("Servo Opened");
}
}
Download the "LDR_3.ino" file and open it up in the Arduino IDE.
Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload. You can tinker with it if you like based on the application, or just use it as it is.