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

Buzzer Pin Connections

Buzzer Pin

Important: Use a 3.3 V-compatible buzzer or buzzer module suitable for GPIO control. Do not connect a high-current buzzer directly to a NodeMCU GPIO pin.

Template:Note

Working Principle

A buzzer is an electronic sound-producing device that converts an electrical signal into an audible sound.

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

When the GPIO output is activated, the buzzer produces an audible sound. When the GPIO output is deactivated, the buzzer stops producing sound.

In this experiment, the buzzer is programmed to:

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

Experiment Code for Buzzer

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

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

int buzzerPin = D1;   // GPIO5

void setup() {

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

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

void loop() {

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

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

Note: The above program assumes that the buzzer/module is active HIGH. If the buzzer module is active LOW, interchange the `HIGH` and `LOW` values in the program.

Testing the Buzzer

  1. Connect the buzzer signal/+ pin to D1 (GPIO5).
  1. Connect the buzzer GND/- pin to NodeMCU GND.
  1. If using a 3-pin buzzer module, connect VCC to 3.3 V, GND to GND, and IN/S to D1 (GPIO5).
  1. Connect the NodeMCU to the computer using a USB cable.
  1. Select the correct NodeMCU board and COM port in Arduino IDE.
  1. Upload the program to the NodeMCU.
  1. Power the NodeMCU.
  1. Observe the buzzer operation.
  1. The buzzer should sound for approximately 2 seconds.
  1. The buzzer should remain OFF for approximately 2 seconds.
  1. The process repeats continuously.

Observation

  • The buzzer is initially OFF.
  • The buzzer turns ON and produces an audible sound for approximately 2 seconds.
  • The buzzer turns OFF for approximately 2 seconds.
  • The buzzer continues switching between ON and OFF states.
  • The buzzer is controlled by the NodeMCU through D1 (GPIO5).

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 NodeMCU generates a digital control signal through D1 to turn the buzzer ON and OFF.

Safety Note

Use a 3.3 V-compatible, low-current buzzer or buzzer module for direct GPIO interfacing. Do not connect a high-power or high-current buzzer directly to the NodeMCU GPIO. If the buzzer requires more current than the GPIO can safely provide, use a suitable transistor or driver circuit with an external power supply.