Jump to content

IPDL - 5: Integration of Actuators with NodeMCU

From cognifront

IPDL - 5: Integration of Actuators with NodeMCU (Servo Motor)

Objective

To interface and control a servo motor and a Buzzer independently using NodeMCU ESP8266.

Required Components

  • NodeMCU ESP8266
  • Servo motor
  • LED Bar
  • External 5 V power supply

Part A: Servo Motor Interfacing

Objective

To interface a servo motor with NodeMCU ESP8266 and control its angular position using a programmed control signal.

Circuit Diagram

Servo Motor Pin Connections

Servo Motor Pin / Connection Connection Details
Signal / Data Connect to NodeMCU D5 (GPIO14)
GND Connect to NodeMCU GND
VCC Connect to the positive (+) terminal of the external 5 V power supply
External Power Supply GND Connect to NodeMCU GND to provide a common ground
Power Adapter DC Jack Connect the DC jack pin of the adapter to the DC jack provided on the kit

Important: Use the external 5 V supply to power the servo motor. The NodeMCU GND and external power-supply GND must be connected together to provide a common reference for the servo control signal.

Working Principle

A servo motor is an actuator that can rotate its shaft to a specified angular position.

The NodeMCU generates a PWM-based control signal on D5. The servo interprets this signal and moves its shaft to the corresponding angular position.

In this experiment, the servo is programmed to move as per the angle given by the user via serial monitor. The user can specify the angle between 0° to 180°.

Experiment Code for Servo Motor

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: Servo Motor Interfacing with NodeMCU
// Board: NodeMCU ESP8266

#include <Servo.h>

// Create servo object
Servo myServo;

// Servo signal connected to D5
#define SERVO_PIN D5

void setup() {

  // Start Serial Monitor
  Serial.begin(9600);

  // Attach servo with pulse range
  myServo.attach(SERVO_PIN, 500, 2400);

  // Set initial position
  myServo.write(0);

  Serial.println("SG90 Servo Control");
  Serial.println("Enter angle (0-180):");
}

void loop() {

  // Check if a value is entered in Serial Monitor
  if (Serial.available() > 0) {

    // Read the angle
    int angle = Serial.parseInt();

    // Make sure angle is within range
    if (angle >= 0 && angle <= 180) {

      // Move servo
      myServo.write(angle);

      // Display current angle
      Serial.print("Servo moved to: ");
      Serial.print(angle);
      Serial.println(" degrees");

    } else {
      Serial.println("Enter an angle between 0 and 180.");
    }

    // Clear remaining Serial data
    while (Serial.available() > 0) {
      Serial.read();
    }
  }
}

Important Note

The program uses the Servo library.

Try compiling the code normally without edit first, if servo moves as expected no need for external library as while we install the NodeMCU in Arduino IDE, some basics libraries we get preinstalled, one of them is Servo.h For ESP8266 boards, install the ESP8266Servo library if the standard Servo library is not available or does not compile correctly with the selected NodeMCU board.

Observation

  • The servo moves to as per the ange specified by the user
  • The servo movement is controlled by the NodeMCU through D5.

Result

The servo motor was successfully interfaced with NodeMCU ESP8266 and controlled to move to different angular positions.

Conclusion

This experiment demonstrates the interfacing and control of a servo motor using NodeMCU ESP8266. The NodeMCU generates the required control signal and the servo motor responds by moving to the programmed angular positions.

Part B: 4-LED Bar Interfacing

Objective

To interface a 4-LED bar with NodeMCU ESP8266 and control the LEDs using digital output signals.

Circuit Diagram

4-LED Bar Pin Connections

4-LED Bar Connection NodeMCU Connection
LED 1 NodeMCU D1
LED 2 NodeMCU D2
LED 3 NodeMCU D3
LED 4 NodeMCU D4
GND NodeMCU GND

Working Principle

A 4-LED bar consists of four individual LEDs that can be controlled independently using digital output signals.

The NodeMCU ESP8266 is programmed to configure the four digital pins as outputs. By setting a digital output HIGH, the corresponding LED is turned ON, while setting it LOW turns the LED OFF.

Two programs are demonstrated in this experiment:

  • Program 1 – Simple LED Blinking: All four LEDs blink together at a fixed time interval.
  • Program 2 – Pattern-Based LED Control: The LEDs are switched ON and OFF in a sequence to create a running-light pattern.

Program 1: Simple 4-LED Blinking

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: 4-LED Bar Interfacing with NodeMCU
// Program 1: Simple LED Blinking
// Board: NodeMCU ESP8266

// LED connections
#define LED1 D1
#define LED2 D2
#define LED3 D3
#define LED4 D4

void setup() {

  // Configure LED pins as digital outputs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop() {

  // Turn ON all LEDs
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);

  delay(500);

  // Turn OFF all LEDs
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);

  delay(500);
}

Program 2: Pattern-Based LED Control

The user can select the LED pattern by entering a number through the Serial Monitor.

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: 4-LED Bar Interfacing with NodeMCU
// Program 2: Pattern-Based LED Control
// Board: NodeMCU ESP8266

// LED connections
#define LED1 D1
#define LED2 D2
#define LED3 D3
#define LED4 D4

void setup() {

  // Start Serial Monitor
  Serial.begin(9600);

  // Configure LED pins as digital outputs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  // Turn OFF all LEDs initially
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);

  Serial.println("4-LED Bar Pattern Control");
  Serial.println("Enter pattern number:");
  Serial.println("1 - Running LED");
  Serial.println("2 - Alternate LEDs");
  Serial.println("3 - All LEDs Blink");
}

void loop() {

  // Check if user has entered a value
  if (Serial.available() > 0) {

    int pattern = Serial.parseInt();

    // Pattern 1: Running LED
    if (pattern == 1) {

      Serial.println("Running LED pattern selected.");

      for (int i = 0; i < 3; i++) {

        digitalWrite(LED1, HIGH);
        delay(200);
        digitalWrite(LED1, LOW);

        digitalWrite(LED2, HIGH);
        delay(200);
        digitalWrite(LED2, LOW);

        digitalWrite(LED3, HIGH);
        delay(200);
        digitalWrite(LED3, LOW);

        digitalWrite(LED4, HIGH);
        delay(200);
        digitalWrite(LED4, LOW);
      }
    }

    // Pattern 2: Alternate LEDs
    else if (pattern == 2) {

      Serial.println("Alternate LED pattern selected.");

      for (int i = 0; i < 5; i++) {

        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, LOW);

        delay(400);

        digitalWrite(LED1, LOW);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, HIGH);

        delay(400);
      }
    }

    // Pattern 3: All LEDs blink
    else if (pattern == 3) {

      Serial.println("All LED blinking pattern selected.");

      for (int i = 0; i < 5; i++) {

        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);

        delay(500);

        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);

        delay(500);
      }
    }

    else {
      Serial.println("Invalid input. Enter 1, 2 or 3.");
    }

    // Turn OFF all LEDs after pattern execution
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);

    Serial.println();
    Serial.println("Enter pattern number:");
    Serial.println("1 - Running LED");
    Serial.println("2 - Alternate LEDs");
    Serial.println("3 - All LEDs Blink");

    // Clear remaining Serial data
    while (Serial.available() > 0) {
      Serial.read();
    }
  }
}

Serial Monitor Input:

  • Enter 1 → Running LED pattern
  • Enter 2 → Alternate LED pattern
  • Enter 3 → All LEDs blink

Observation

Program 1 – Simple Blinking:

  • All four LEDs turn ON simultaneously.
  • After a short delay, all four LEDs turn OFF simultaneously.
  • The ON/OFF cycle repeats continuously.

Program 2 – Pattern-Based Control:

  • The LEDs turn ON one after another from LED 1 to LED 4.
  • The sequence then moves back from LED 3 to LED 2.
  • The pattern repeats continuously, producing a running-light effect.

Result

The 4-LED bar was successfully interfaced with NodeMCU ESP8266. The LEDs were controlled using digital output signals, demonstrating both simple blinking and pattern-based LED control.

Conclusion

This experiment demonstrates the use of multiple digital output pins of the NodeMCU ESP8266 to control a 4-LED bar. Different LED patterns can be created by appropriately programming the digital output signals.