servo.ino

ESP32 (original, ESP32-WROOM-32 class) · File : servo.ino · Language : Arduino · Use case : Faire balayer un servo d'angle

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

A classic ESP32 board (ESP32-WROOM-32 module, dual-core Xtensa 240 MHz, Wi-Fi + Bluetooth) such as a DevKitC / DOIT V1, powered over micro-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, install the « esp32 » board package, pick « Dev Module », the port, then Upload (hold BOOT if detection fails). GPIO34-39 are input-only; avoid GPIO6-11 and GPIO12.

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
}

← Back to the board