servo.ino

ESP32-C6 · 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

An ESP32-C6 board (RISC-V 160 MHz, Wi-Fi 6 + BLE 5 + 802.15.4) such as a DevKitC-1, 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 (recent esp32 package), select « ESP32C6 Dev Module » then Upload; for Zigbee/Thread use ESP-IDF (idf.py set-target esp32c6).

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