0% found this document useful (0 votes)
3 views

090-PostLab21

The document outlines an experiment on interfacing a servo motor with NodeMCU, detailing the working principles of servo motors, including PWM signal control for precise angular positioning. It describes the components of a servo motor, the feedback mechanism, and provides code examples for controlling the servo using NodeMCU. Additionally, it includes steps for connecting the servo motor and generating PWM signals to manipulate its position.

Uploaded by

premjoshi.int
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

090-PostLab21

The document outlines an experiment on interfacing a servo motor with NodeMCU, detailing the working principles of servo motors, including PWM signal control for precise angular positioning. It describes the components of a servo motor, the feedback mechanism, and provides code examples for controlling the servo using NodeMCU. Additionally, it includes steps for connecting the servo motor and generating PWM signals to manipulate its position.

Uploaded by

premjoshi.int
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Marwadi University

Faculty of Engineering & Technology


Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

Aim: Interfacing Servo Motor with NodeMCU

Overview of Servo Motor

A servo motor is an electric device used for precise control of angular rotation. It is used in applications that
demand precise control over motion, like in the case of control of the robotic arm. The rotation angle of the servo
motor is controlled by applying a PWM signal to it. By varying the width of the PWM signal, we can change the
rotation angle and direction of the motor. Mostly 50Hz PWM is used to control the shaft position of the servo
motor.

Basic Concept of a Servo Motor

A servo motor is a rotary actuator that allows precise control of angular position, speed, and acceleration. It
consists of three main components:
1. DC Motor: Provides the rotational motion.
2. Gearbox: Reduces speed and increases torque.
3. Control Circuit: Processes input signals to position the motor accurately.
Servo motors are widely used in robotics, drones, industrial automation, and home automation due to their
precision and ease of control.

How a Servo Motor Works

Input Signal (PWM):

• The servo motor is controlled using a Pulse Width Modulation (PWM) signal.

• A PWM signal is a square wave signal where the width of the "ON" period determines the position of the
servo motor.

• The PWM signal is typically refreshed every 20 milliseconds (50 Hz frequency).

Angular Control Using PWM:

• The duration of the pulse within the 20ms period determines the angle:

o 1ms pulse width: Positions the servo at 0°.

o 1.5ms pulse width: Positions the servo at 90° (neutral).

o 2ms pulse width: Positions the servo at 180°.


Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

Feedback Mechanism:

• A potentiometer inside the servo provides feedback about its current position.

• The control circuit adjusts the motor’s position until the desired angle is achieved.

Introduction to PWM

Pulse Width Modulation (PWM) is a technique by which the width of a pulse is varied while keeping the
frequency of the wave constant.

PWM Generation
A period of a pulse consists of an ON cycle (VCC) and an OFF cycle (GND). The fraction for which the signal
is ON over a period is known as a duty cycle.

E.g. A pulse with a period of 10ms will remain ON (high) for 2ms.Therefore, the duty cycle will be
D = 2ms / 10ms = 20%
Through the PWM technique, we can control the power delivered to the load by using the ON-OFF signal. The
PWM signals can be used to control the speed of DC motors and to change the intensity of the LED. Moreover,
it can also be used to generate sine signals. Pulse Width Modulated signals with different duty cycle are shown
below.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

NodeMCU based ESP8266 has the functionality of PWM interfaces via software programming. It is achieved
with the timer interruption method. The PWM frequency range for ESP8266 is adjustable up to 1KHz.
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

Arduino function for NodeMCU PWM

analogWrite(pin, dutycycle): Enables software PWM on the specified pin. duty cycle is in the range
from 0 to PWMRANGE, i.e. 1023 by default.

analogWrite(pin, 0): Disables PWM on the specified pin.

analogWriteRange(new_range): This function is used to change the PWM range (duty cycle).

analogWriteFreq(new_frequency): PWM frequency is 1kHz by default. Call this function to change it


with new frequency.PWM frequency is in the range of 1 – 1000Khz.

Connection Diagram of Servo Motor with NodeMCU


Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

Steps to Control Servo Motor


1. Generate a PWM Signal:
o PWM signal must have a fixed frequency (50 Hz) with a variable duty cycle to control the servo's
angle.
2. Connect the Servo Motor:
o Signal: Connect the PWM pin of the microcontroller to the servo's signal pin.
o VCC: Power the servo motor (5V typically).
o GND: Connect the ground of the servo motor to the microcontroller's ground.
3. Adjust the Duty Cycle:
o Modify the duty cycle of the PWM signal to change the position of the servo.

Code:
#include <Servo.h> // Include the Servo library

Servo myServo; // Create a Servo object

void setup() {
myServo.attach(5); // Attach the servo to pin 9
myServo.write(0);
delay(2000);
}

void loop() {
// Rotate servo to 90°
myServo.write(90);
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

delay(1000); // Wait for 1 second

// Rotate servo to 0°
myServo.write(0);
delay(1000); // Wait for 1 second

// Rotate servo to 180°


//myServo.write(180);
//delay(1000); // Wait for 1 second
}

Post-Lab:
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

Fig. 1: Simulation Circuit

Code

#include <Servo.h> // Include the Servo library

Servo myServo; // Create a Servo object

void setup() {

myServo.attach(9); // Attach the servo to pin 9

myServo.write(0);

delay(2000);

void loop() {

// Rotate servo to 90°

myServo.write(90);

delay(1000); // Wait for 1 second

// Rotate servo to 0°

myServo.write(0);
Marwadi University
Faculty of Engineering & Technology
Department of Information and Communication Technology
Subject: Foundation skills in
Aim: Interfacing Servo Motor with NodeMCU
sensor interfacing (01CT1103)
Experiment No: 21 Date: 4 Dec’24 Enrollment No:92400133090

delay(1000); // Wait for 1 second

// Rotate servo to 180°

//myServo.write(180);

//delay(1000); // Wait for 1 second

You might also like