This is a simple NodeMCU project.
This circuit will turn ON the LED when it's dark and turn OFF when there is light.
Generally, LDR is a Light Dependent Resistor, whose resistance changes depending upon the intensity of the light incident on it.
LDR : Output is analog in nature, so it gets connected to the A0 pin of the NodeMCU.
LED : Anode is connected to D1 pin and Cathode to Ground (GND) pin of NodeMCU.
Really 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.
const int ledPin = 5;
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <=300) {
digitalWrite(ledPin, HIGH); Serial.print(ldrStatus); Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW); Serial.println("LED is OFF");
}
}
Download the "LDR_2.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.