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 |
|---|---|
| Signal | NodeMCU D5 (GPIO14) |
| VCC | External 5 V supply |
| GND | Common GND |
Important: The servo motor should preferably be powered from an external 5 V supply. 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 (GPIO14). The servo interprets this signal and moves its shaft to the corresponding angular position.
In this experiment, the servo is programmed to move sequentially to:
- 0°
- 90°
- 180°
- 0°
The servo remains at each position for a specified time before moving to the next position.
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>
Servo myServo;
int servoPin = D5; // GPIO14
void setup() {
// Attach servo to D5
myServo.attach(servoPin);
// Set servo to initial position
myServo.write(0);
}
void loop() {
// Move servo to 0 degrees
myServo.write(0);
delay(1000);
// Move servo to 90 degrees
myServo.write(90);
delay(1000);
// Move servo to 180 degrees
myServo.write(180);
delay(1000);
// Return servo to 0 degrees
myServo.write(0);
delay(1000);
}
Required Arduino Library
The program uses the Servo library.
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.
Testing the Servo
- Connect the servo signal wire to D5 (GPIO14).
- Connect the servo VCC to an appropriate external 5 V supply.
- Connect the servo GND and NodeMCU GND together.
- Upload the program to the NodeMCU.
- Power the NodeMCU and servo.
- Observe the servo movement.
- The servo should move approximately to 0°, 90°, and 180° positions and then return to 0°.
Observation
- The servo moves to approximately 0°.
- The servo then moves to approximately 90°.
- The servo then moves to approximately 180°.
- The servo returns to approximately 0°.
- The servo movement is controlled by the NodeMCU through D5 (GPIO14).
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.