🛠 Piloter un servo avec le PWM (LEDC)

Arduino Nano ESP32 · intermediaire · Schéma : IMG-nano-esp32-intermediaire-3-nanoesp32-pwm-servo.svg — Servo SG90 alimenté par une source 5 V externe, signal sur D9, masse commune avec la carte.

Objectives

Control a micro-servo using the Servo library (built on the ESP32-S3's LEDC PWM controller) and make it sweep from 0 to 180°. Understand the power supply: the board has no 5 V pin, while a standard servo operates at 4.8–6 V.

Hardware

Complete code

#include <Servo.h>

Servo balai;
const uint8_t PIN_SERVO = 9;   // D9, PWM via LEDC dans le core Arduino

void setup {
  Serial.begin(115200);
  balai.attach(PIN_SERVO);     // PWM 50 Hz, standard servo
}

void loop {
  for (int a = 0; a <= 180; a += 15) { balai.write(a); delay(300); }
  for (int a = 180; a >= 0; a -= 15) { balai.write(a); delay(300); }
}

Expected verification

The servo arm sweeps 0 → 180 → 0° in a smooth loop. Powered from an external 5 V source with a common ground, the movement is complete without the board restarting or the USB LED flickering.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials