servo.ino
The absolute mechanics standard: an MG90/SG90 servo sweeps 0-180° driven by 50 Hz PWM. On ESP32 the Servo library rides on LEDC, so almost any pin works.
Hardware required
An ESP32-S2 board (single-core Xtensa, Wi-Fi, native USB-OTG on GPIO19/20, 14 capacitive touch pads) such as a DevKitC-1 or QT Py, powered over USB. 1 hobby servo (SG90/MG90S) + separate 5 V supply beyond a micro servo.
Wiring
Signal on GPIO13, + to the external supply, grounds common with the board. Don't power an average servo from the board alone.
How it works
write(angle) converts the angle into a 1-2 ms pulse (20 ms period) through an LEDC channel. A 5° step every 30 ms gives a smooth sweep without straining the mechanics. In the Arduino IDE (esp32 package), select « ESP32S2 Dev Module »; the S2 flashes straight over its native USB (double reset on some boards to enter the bootloader).
Going further
Close the loop on an ultrasonic sensor (anti-collision), or drive it over BLE with the dedicated service sketch.
servo.ino
// Servo d'angle sur GPIO13 — bibliotheque Servo (impl. LEDC du paquet esp32).
#include <Servo.h>
Servo servo;
int angle = 0;
int pas = 5;
void setup() {
Serial.begin(115200);
servo.attach(13); // n'importe quel GPIO PWM-safe
Serial.println("Balayage 0-180 degres");
}
void loop() {
angle += pas;
if (angle >= 180 || angle <= 0) pas = -pas;
servo.write(angle);
if (angle % 45 == 0) Serial.printf("%d degres\n", angle);
delay(30); // vitesse de balayage raisonnable
}