Control Servo Using LDR With NodeMCU




Introduction: Control Servo Using LDR With NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

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.

Step 1: Things You Need to Collect

List of parts required for this instructable :

Hardware Required

  • NodeMCU
  • LDR / photoresistor
  • 10k ohm resistor
  • Servo Motor
  • Breadboard
  • Micro USB cable
  • Connecting Wires

Software Required

  • Arduino IDE (with ESP8266 Library installed)

Step 2: Circuit Connection

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.

"Interfacing Servo Motor with NodeMCU"

"NodeMCU With LDR"

Step 3: Code Is Right Here

#include <Servo.h>

Servo servo;

const int ldrPin = A0;
void setup() {


pinMode(ldrPin, INPUT);

servo.attach(2); //D4 servo.write(0);



void loop() {

int ldrStatus = analogRead(ldrPin);

if (ldrStatus <=300) {


delay(1000); Serial.println("Servo Closed");


else {



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.

Step 4: Output

That's all makers!

It takes less time to create this instructable, and its fun too.

Thank you for taking your time to read my instructable. I hope you enjoy it as I enjoy making it and documenting it to show and tell to other fellow makers here.

CIAO!! with another interesting Instructables!

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

1 Person Made This Project!


  • Teach With Tinkercad Contest

    Teach With Tinkercad Contest
  • Microcontroller Contest

    Microcontroller Contest
  • Crayons Challenge

    Crayons Challenge



1 year ago on Introduction

The Arduino Servo library will not work with this project, when you upload the sketch it errors out. Maybe it will work with the Kevin Harrington Esp-32 library. I have not try yet.


Reply 1 year ago

It's likely because you're not using the esp8266 Servo library.

On your Arduino IDE got to. Sketch >>Include Library>>Manage Library. Then search for servo esp8266. When the field loads just click on "install" in the lower right corner of the tab. Then try compiling your program again.

You can also use the example code, "Sweep" under File>>Examples>>Servo(Esp8266).
Above is the guide for using libraries in arduino.


Question 2 years ago on Introduction

Can you link me to locations that sell that particular servo motor?