IDPL - 5: Integration of Actuators with NodeMCU
IDPL - 5: Integration of Actuators with NodeMCU (Servo Motor/Relay)
Objective
To interface and control a servo motor and a 5 V control relay independently using NodeMCU ESP8266.
Required Components
- NodeMCU ESP8266
- Servo motor
- Single-channel 5 V relay module
- External 5 V power supply
- Jumper wires
- Breadboard
Part A: Servo Motor Interfacing
Objective
To interface a servo motor with NodeMCU ESP8266 and control its angular position using a programmed control signal.
Required Components
- NodeMCU ESP8266
- Servo motor
- External 5 V power supply
- Jumper wires
- Breadboard
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: Relay Interfacing
Objective
To interface a single-channel 5 V relay module with NodeMCU ESP8266 and control the relay using a digital output signal.
Required Components
- NodeMCU ESP8266
- Single-channel 5 V relay module
- External 5 V power supply
- Jumper wires
- Breadboard
- Suitable electrical load, if required
Circuit Diagram
Relay Pin Connections
| Relay Module Pin | Connection |
|---|---|
| IN | NodeMCU D1 (GPIO5) |
| VCC | 5 V supply |
| GND | Common GND |
Important: Connect the NodeMCU GND and the relay power-supply GND together when required by the relay module's control interface.
Working Principle
A relay is an electrically operated switch that allows a low-voltage control circuit to switch a separate electrical load.
The NodeMCU controls the relay module through D1 (GPIO5).
When the relay is activated, its internal contacts change state. The relay contacts can then be used to control a suitable external load within the relay's rated voltage and current limits.
In this experiment, the relay is programmed to:
- Turn ON for 2 seconds.
- Turn OFF for 2 seconds.
- Repeat continuously.
Note: The relay module used in this experiment is assumed to be a 5 V relay module with a control input compatible with the NodeMCU interface. Verify the specific relay module before connecting it.
Experiment Code for Relay
// © 2026, Cognifront. Member of The IoT Kits Family.
// Experiment: Relay Interfacing with NodeMCU
// Board: NodeMCU ESP8266
int relayPin = D1; // GPIO5
void setup() {
// Set relay pin as output
pinMode(relayPin, OUTPUT);
// Keep relay OFF initially
digitalWrite(relayPin, LOW);
}
void loop() {
// Turn relay ON
digitalWrite(relayPin, HIGH);
delay(2000);
// Turn relay OFF
digitalWrite(relayPin, LOW);
delay(2000);
}
Note: The above program assumes that the relay module is active HIGH. If the relay module is active LOW, interchange the `HIGH` and `LOW` values in the program.
Testing the Relay
- Connect the relay IN pin to D1 (GPIO5).
- Connect relay VCC to the appropriate 5 V supply.
- Connect relay GND to common GND.
- Upload the program to the NodeMCU.
- Power the NodeMCU and relay module.
- Observe the relay indicator LED and listen for the relay switching sound.
- The relay should turn ON for approximately 2 seconds.
- The relay should then turn OFF for approximately 2 seconds.
- The process repeats continuously.
Observation
- The relay is initially OFF.
- The relay turns ON for approximately 2 seconds.
- The relay turns OFF for approximately 2 seconds.
- The relay continues switching between ON and OFF states.
- The relay is controlled by the NodeMCU through D1 (GPIO5).
Result
The single-channel relay module was successfully interfaced with NodeMCU ESP8266 and controlled using a digital output signal.
Conclusion
This experiment demonstrates the interfacing and control of a relay module using NodeMCU ESP8266. The NodeMCU provides a low-voltage digital control signal, while the relay can be used to switch a suitable external electrical load within its specified ratings.
Safety Note: The NodeMCU and relay control connections are low-voltage circuits. If the relay is used to control a 230 V AC load, the mains/load side presents a serious electric-shock and fire hazard. Mains wiring must be performed only by a qualified person using appropriate electrical protection, insulation, enclosure, and safety procedures. Always disconnect mains power before modifying the load-side wiring.