🛠 Piloter un micro-servo avec la PWM du GIGA R1

Arduino GIGA R1 · intermediaire · Schéma : IMG-giga-r1-i1-1-pwm-servo.svg — Micro-servo alimenté par une source externe 5 V, fil de commande relié à une broche PWM du GIGA R1, masses communes entre la carte et l'alimentation.

Objectives

Master PWM (Pulse Width Modulation) output to control an angle micro-servo. You will learn how to use the Arduino core Servo library, correctly power an external load, and respect the 3.3 V logic of the GIGA R1 (76 GPIO pins available on the board).

Hardware

Complete code

// GIGA R1 WiFi — balayage de micro-servo avec la bibliotheque Servo
#include <Arduino.h>
#include <Servo.h>

const int BROCHE_SERVO = D9;  // broche PWM de la sérigraphie
Servo monServo;
int angle = 0;
int sens = 1;

void setup {
  Serial.begin(115200);
  monServo.attach(BROCHE_SERVO);   // attache la broche au signal servo
  monServo.write(0);
}

void loop {
  angle += sens * 5;               // balle 0 -> 180 -> 0
  if (angle >= 180) sens = -1;
  if (angle <= 0)   sens = 1;
  monServo.write(angle);
  Serial.printf("angle = %d\n", angle);
  delay(30);
}

Wiring: orange wire (signal) to D9, red wire to external +5 V, brown wire to external GND, and external GND connected to a board GND (common ground is mandatory).

Expected verification

The servo sweeps back and forth regularly from 0° to 180°. The Serial Monitor (115200 baud) displays the current angle. The movement is smooth, without jitter or saturation whining.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials