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
  • Buzzer
  • 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: Buzzer Interfacing

Objective

To interface a buzzer with NodeMCU ESP8266 and control the buzzer using a digital output signal.

Circuit Diagram

Working Principle

A buzzer is an electronic device used to produce an audible sound when an electrical signal is applied to it.

The NodeMCU controls the buzzer through D1 using a digital output signal.

When the output signal is HIGH, the buzzer is activated. When the output signal is LOW, the buzzer is deactivated.

In this experiment, the buzzer is programmed to:

  • Turn ON for 2 seconds.
  • Turn OFF for 2 seconds.
  • Repeat continuously.

Buzzer Pin Connections

Buzzer Connection Connection Details
Data Connect to NodeMCU D1
GND Connect to NodeMCU GND

Experiment Code for Buzzer

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

// Experiment: Buzzer Interfacing with NodeMCU
// Board: NodeMCU ESP8266

int buzzerPin = D1;

void setup() {

  // Set buzzer pin as output
  pinMode(buzzerPin, OUTPUT);

  // Turn buzzer OFF initially
  digitalWrite(buzzerPin, LOW);
}

void loop() {

  // Turn buzzer ON
  digitalWrite(buzzerPin, HIGH);
  delay(2000);

  // Turn buzzer OFF
  digitalWrite(buzzerPin, LOW);
  delay(2000);
}

Testing the Buzzer

  1. Connect the buzzer Signal pin to D1.
  1. Connect the buzzer GND pin to NodeMCU GND.
  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 the program to the NodeMCU.
  1. Observe the buzzer operation.
  1. The buzzer should turn ON for approximately 2 seconds.
  1. The buzzer should turn OFF for approximately 2 seconds.
  1. The process repeats continuously.

Observation

  • The buzzer remains OFF initially.
  • The buzzer turns ON for approximately 2 seconds.
  • The buzzer turns OFF for approximately 2 seconds.
  • The buzzer continuously switches between ON and OFF states.

Result

The buzzer was successfully interfaced with NodeMCU ESP8266 and controlled using a digital output signal.

Conclusion

This experiment demonstrates the interfacing and digital control of a buzzer using NodeMCU ESP8266. The buzzer is controlled by the NodeMCU through D1 using a digital output signal.