🛠 Asservir un servo moteur avec un potentiomètre (PWM)

Arduino Leonardo — Board Fiche · intermediaire · Schéma : IMG-leonardo-intermediaire-6-leonardo-pwm-servo.svg — Leonardo commandant un servo moteur sur la broche PWM 9, potentiomètre sur A0, alimentation externe 5V avec masse commune

Objectives

Control a standard servo motor (pulse-width signal) from the Leonardo using the Servo library, driven by a potentiometer. You will identify the 7 PWM pins on the board (3, 5, 6, 9, 10, 11, 13, using 8-bit PWM via analogWrite) — the Servo library generates pulses using hardware timers on its side — and most importantly, the power supply rules for a servo.

Hardware

Complete code

#include <Servo.h>

Servo monServo;
const uint8_t POT_PIN   = A0;
const uint8_t SERVO_PIN = 9; // broche PWM (3, 5, 6, 9, 10, 11 ou 13)

void setup {
  monServo.attach(SERVO_PIN); // impulse ~50 Hz, 1 a 2 ms
}

void loop {
  int brut = analogRead(POT_PIN);            // 0..1023
  int angle = map(brut, 0, 1023, 0, 180);    // 0..180 degres
  monServo.write(angle);
  delay(15);
}

Expected verification

The servo arm follows the potentiometer between its two mechanical stops (~0° and ~180°). The motor buzzes slightly when stationary: this is normal for a standard servo. Power the servo from the board's 5V pin for a single small SG90; as soon as it heats up or the board restarts due to insufficient current, switch to an external power supply with a common GND.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials