Free Nationwide Doorstep Delivery on orders over ₹499
← All 10 Projects•Intermediate•45 mins

Precision Servo Motor Angle Control via PWM

Generate Pulse Width Modulation (PWM) signals to position a servo motor shaft precisely from 0° to 180°.

Real-World Engineering Application

Robotic arm joints, camera pan-tilt stabilizers, RC aircraft rudders, and automated gate barriers.

Physics & Working Principle

A servo motor houses a DC motor, reduction gear train, potentiometer feedback, and control IC. A periodic 50Hz PWM signal (20ms frame) where pulse width varies between 1.0ms (0°) and 2.0ms (180°) dictates the target angular position.

Key Learning Outcome:Understand closed-loop feedback systems, PWM duty cycle vs pulse width, and motor actuation limits.

Pin Connection & Wiring Table

Component Pin / TerminalMicrocontroller Pin (ESP32 / Arduino)Wiring Note
Brown / Black Wire (GND)GNDCommon ground
Red Wire (VCC)5V / VINMotor power
Orange / Yellow Wire (PWM Signal)GPIO 13 / Pin 9Control signal line

Step-by-Step Plug & Play Assembly

1

Connect the brown wire to ground and the red wire to 5V.

2

Connect the orange control signal wire to GPIO 13.

3

Mount a horn indicator on the motor splines so you can visibly track the rotation angles.

Complete Tested Source Code

// NovaCircuit Project 5: Servo Motor Angle Sweeper
#include <ESP32Servo.h> // Or <Servo.h> for Arduino Uno

Servo myServo;
const int SERVO_PIN = 13;

void setup() {
  Serial.begin(115200);
  myServo.attach(SERVO_PIN);
  Serial.println("Servo Motor Sweeper Activated");
}

void loop() {
  // Sweep from 0 to 180 degrees
  for (int pos = 0; pos <= 180; pos += 15) {
    myServo.write(pos);
    Serial.print("Angle: ");
    Serial.println(pos);
    delay(100);
  }
  
  delay(500);

  // Sweep back from 180 to 0 degrees
  for (int pos = 180; pos >= 0; pos -= 15) {
    myServo.write(pos);
    Serial.print("Angle: ");
    Serial.println(pos);
    delay(100);
  }
  
  delay(500);
}
Code Logic Breakdown:

myServo.attach(pin) allocates a hardware PWM timer. myServo.write(angle) converts the degree parameter (0-180) into the exact microsecond duty pulse needed by the servo potentiometer control loop.

Expected Physical Output

The servo arm sweeps smoothly back and forth across 180 degrees, pausing momentarily at each extreme.
Troubleshooting Tips
  • •Servo jittering or resetting MCU: Servos draw peak current when starting; power from a dedicated 5V power supply or external battery if microcontroller brown-outs occur.
  • •Never force the servo horn by hand beyond its mechanical stops.
Electrical Safety Directives
  • •Keep fingers away from moving horn arms and do not stall the motor.
Companion Resources

Related Book Chapter & Hardware Kit

Covered in depth within Chapter 8: Introduction to ESP32. Build this easily with the Sensor Explorer Kit.