Jump to content

IDPL - 5: Integration of Actuators with NodeMCU

From cognifront

IDPL - 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 Connections

LED

Working Principle

A 4-LED bar consists of four LEDs that can be individually controlled using digital output signals from the NodeMCU.

Each LED is connected to a separate digital output. By changing the output state of each connection between HIGH and LOW, different LED patterns can be created.

In this experiment, the 4-LED bar is programmed to demonstrate different LED blinking patterns.

The following five patterns are provided:

Pattern

Experiment Code 1: Simple LED Bar Blinking

This program makes all four LEDs blink together.

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

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

int led1 = D1;
int led2 = D2;
int led3 = D5;
int led4 = D6;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  // Turn all LEDs OFF initially
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
}

void loop() {

  // Turn all LEDs ON
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);

  delay(1000);

  // Turn all LEDs OFF
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);

  delay(1000);
}

Experiment Code 2: Five LED Blinking Patterns

Select one pattern by changing the value of `pattern` in the program.

For example:

int pattern = 1;

Change the value to 1, 2, 3, 4, or 5 to select the required pattern.

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

// Experiment: 4-LED Bar Interfacing with NodeMCU
// Program: Five LED Blinking Patterns
// Board: NodeMCU ESP8266

int led1 = D1;
int led2 = D2;
int led3 = D5;
int led4 = D6;

// Select pattern: 1 to 5
int pattern = 1;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  // Turn all LEDs OFF initially
  allOff();
}

void loop() {

  switch (pattern) {

    // Pattern 1: All LEDs blink together
    case 1:
      allOn();
      delay(500);
      allOff();
      delay(500);
      break;

    // Pattern 2: Left to right
    case 2:
      allOff();
      digitalWrite(led1, HIGH);
      delay(300);

      digitalWrite(led2, HIGH);
      delay(300);

      digitalWrite(led3, HIGH);
      delay(300);

      digitalWrite(led4, HIGH);
      delay(300);

      allOff();
      delay(500);
      break;

    // Pattern 3: Right to left
    case 3:
      allOff();
      digitalWrite(led4, HIGH);
      delay(300);

      digitalWrite(led3, HIGH);
      delay(300);

      digitalWrite(led2, HIGH);
      delay(300);

      digitalWrite(led1, HIGH);
      delay(300);

      allOff();
      delay(500);
      break;

    // Pattern 4: Alternate LEDs
    case 4:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      digitalWrite(led4, LOW);

      delay(500);

      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      digitalWrite(led4, HIGH);

      delay(500);
      break;

    // Pattern 5: Running/chasing LEDs
    case 5:
      allOff();
      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);
      delay(300);
      break;

    default:
      allOff();
      break;
  }
}

// Turn all LEDs ON
void allOn() {

  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
}

// Turn all LEDs OFF
void allOff() {

  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
}

Pattern Selection

The user can select any one of the five patterns by changing the value of the `pattern` variable.

Value

For example, to select the running LED pattern:

int pattern = 5;

Testing the 4-LED Bar

  1. Connect the four LED connections as shown in the 4-LED Bar Connections table.
  1. Connect the NodeMCU to the computer using the USB cable.
  1. Select the NodeMCU board and the appropriate COM port in Arduino IDE.
  1. Upload Experiment Code 1 to test simple blinking.
  1. Verify that all four LEDs turn ON and OFF together.
  1. Upload Experiment Code 2 to test the different LED patterns.
  1. Change the value of `pattern` to select the required pattern.
  1. Upload the program again after changing the pattern value.
  1. Observe the selected LED pattern.

Observation

  • All four LEDs can be controlled independently.
  • In Pattern 1, all LEDs blink together.
  • In Pattern 2, the LEDs glow sequentially from left to right.
  • In Pattern 3, the LEDs glow sequentially from right to left.
  • In Pattern 4, alternate LEDs blink.
  • In Pattern 5, the LEDs produce a running/chasing effect.

Result

The 4-LED bar was successfully interfaced with NodeMCU ESP8266 and controlled using digital output signals. Different LED blinking patterns were successfully implemented.

Conclusion

This experiment demonstrates the interfacing and digital control of a 4-LED bar using NodeMCU ESP8266. Different LED patterns can be created by controlling the individual LEDs through digital output signals.