Module 1 • Actuators & Motion Classes 8th, 9th & 10th Zero-Library C++

Session 4: SG90 Micro Servo (50Hz PWM) & Smart Barrier

Master 50Hz hardware PWM pulse-width control (500µs–2500µs), sweep servo horn angles smoothly across 0°–180°, build an automated parking barrier, and lock robotic arm angles with hand tilt!

← Course Book 🏫 Satpuda Lab
Connect Robo Logo Connect Robo Kit

⚡ 4-Step Visual System Flow

No Text Walls • 1-Glance Architecture
Step 1 📐
Target Angle Input
0° to 180° Degree Range

Target angle requested by code, toll barrier, or sensor.

Step 2 🧠
ESP32-S3 LEDC PWM
50Hz Standard Frequency

Timer generates precision 500µs – 2500µs high pulses.

Step 3 ⚡
Pulse Transmission
Orange Signal Wire

Sends microsecond duration pulse to SG90 logic chip.

Step 4 🛡️
SG90 Gear Rotation
0° Toll Down / 90° Up

Internal nylon gearbox turns horn arm to exact position!

1

🧰 Components Used in Session 4

ESP32-S3 Dev Board
ESP32-S3 Dev Board 50Hz Hardware PWM Controller
SG90 9g Micro Servo
SG90 9g Micro Servo 180° Geared Motor
Servo Horns & Screws
Servo Horns & Mount Screws Arm & Gate Attachments
Breadboard & Wires
Breadboard & Jumper Wires 5V Power & Signal Bus
2

🔌 Circuit Diagram & Exact Pins

SG90 Servo Wiring SG90 3-Pin Wiring (Orange = Signal, Red = 5V, Brown = GND)
SG90 VCC (Red Wire)
Red Wire to 5V / VIN Rail (CRITICAL: Never 3.3V!)
5V / VIN
SG90 GND (Brown / Black)
Black or Brown Wire to Common Ground
GND
SG90 PWM Signal (Orange)
Orange Wire from ESP32 to Servo Control Pin
GPIO 4
⚡ Critical Lab Power Rule: Never power the SG90 servo motor from the 3.3V pin! The servo draws up to 500mA during motor stalls which will brown out the ESP32 chip. Always connect the Red wire to the 5V / VIN pin!
3

🕹️ Interactive 0°–180° Servo Horn Simulator

🦾 Virtual SG90 Servo Actuator PWM 50Hz • 20ms Frame
SG90 9g MICRO SERVO CONNECT ROBO LAB
PWM PULSE-WIDTH & DUTY: 1500 µs (7.5%) Formula: duty = 410 + (angle * 1638) / 180
4

⚡ Verified Codes (1-Click Open in IDE)

step1_servo_50hz_pwm_sweep.ino ESP32-S3 36 lines
/*
 * Connect Robo - Super-Kit Session 4
 * Step 1: Zero-Library 50Hz PWM Servo Sweep (0° to 180°)
 * Board: ESP32-S3 (Uses native ledcAttach / ledcWrite)
 * Wiring: Orange -> GPIO 4 | Red -> 5V/VIN | Brown -> GND
 */
Preview (8 of 36 lines) — Click ⚡ Open in IDE to auto-load code & board!
✅ Output: Servo horn smoothly sweeps back and forth between 0° and 180°!
step2_smart_toll_barrier.ino ESP32-S3 46 lines
/*
 * Connect Robo - Super-Kit Session 4
 * Step 2: Automated Parking & Toll Plaza Barrier Gate
 * Smoothly lifts barrier to 90°, pauses for car, then safely closes.
 */
Preview (8 of 46 lines) — Click ⚡ Open in IDE to auto-load code & board!
✅ Output: Barrier smoothly opens to 90°, waits 3s, and closes safely!
step3_mpu_hand_tilt_servo_arm.ino ESP32-S3 64 lines
/*
 * Connect Robo - Super-Kit Session 4
 * Step 3: Robotic Servo Arm Controlled by MPU-6050 Hand Tilt
 * Tilt your hand left/right -> Servo follows your motion in real-time!
 */
Preview (8 of 64 lines) — Click ⚡ Open in IDE to auto-load code & board!
✅ Output: Tilt MPU left/right and SG90 servo mirrors your hand gesture!
5

💡 Viva & Lab Practice Questions

Q1: What is the frequency and frame period of a standard RC servo like SG90? ▾
Standard RC analog servos use a 50Hz frequency, which corresponds to a 20 millisecond (ms) frame period. The pulse width varies from 500µs (0°) to 2500µs (180°), with 1500µs representing neutral 90°.
Q2: Why must the SG90 VCC pin connect to 5V instead of 3.3V? ▾
The SG90 internal DC motor and gear set require a minimum of 4.8V to provide nominal torque (1.8 kg·cm). When powered from 3.3V, the motor stalls easily and draws a spike current up to 500mA, causing the ESP32 power rail to brown out and restart.
Q3: How does ESP32 generate precise PWM signals without external timer libraries? ▾
ESP32 includes native hardware LEDC (LED Control) PWM peripheral timers. Calling ledcAttach(pin, 50, 14) routes a 50Hz, 14-bit timer (16,384 discrete steps) to the pin, producing jitter-free pulses in pure hardware with zero CPU load.
📥 33 Sketches (.ZIP) ← Previous: Session 3 (Motion Game) Next: Session 5 (Live Radar) →