Labs / Motors · Beginner · 25 min
Servo angle control
Map 0 to 180 degrees to 500 to 2500 us pulses.
Engineering instrumentation: This lab includes an instrument-style virtual test setup and parameter-driven waveform. For physical measurements, use the Practical Bench.
Parts
- SG90 servo
- ESP32
- 5 V supply
- ESP32Servo library
Steps
- Connect signal, 5 V and GND.
- Attach the servo with 500 to 2500 us limits.
- Write angles from code.
- Observe the pulse width in the simulation.
Safety: check polarity and power off before changing the circuit.
ESP32 code (Arduino IDE)
#include <ESP32Servo.h>
Servo s;
void setup(){ s.attach(13,500,2500); }
void loop(){ s.write(0); delay(1000); s.write(90); delay(1000); s.write(180); delay(1000); }Viva questions
Pulse for 90 degrees?
About 1500 us.
Refresh rate?
Usually 50 Hz, 20 ms per cycle.
Why power from 5 V, not the GPIO?
The servo draws more current than a pin can supply.