servo-balayage.ino

Arduino Due · File : servo-balayage.ino · Language : Arduino · Use case : Faire balayer un servo-moteur de 0 à 180 degrés

The Servo library generates the control pulse (1-2 ms) and sweeps the angle end to end. Feeding a mid-size servo from a dedicated 5 V rail avoids brown-out resets from current spikes.

Hardware required

An Arduino Due (AT91SAM3X8E, ARM Cortex-M3 at 84 MHz) connected over USB to its « Programming » port. Caution: 3.3 V logic, never 5 V on the pins. 1 standard servo (SG90/MG90S) + recommended external 5 V supply.

Wiring

Orange (signal) to pin 3, red to +5 V, brown to GND (common ground with the board).

How it works

myservo.write(angle) sends a pulse whose width maps the angle; 5° steps every 50 ms give smooth motion. In the Arduino IDE, select « Arduino Due (Programming Port) » then Upload; the serial monitor runs at the speed set in the code.

Going further

Add a potentiometer on A0 for a position → servo feedback loop (robot radar).

servo-balayage.ino

// Balayage servo 0..180 degres avec la bibliotheque Servo.
#include <Servo.h>

Servo myservo;
int angle = 0;
int sens = 1;

void setup() {
  myservo.attach(3); // broche avec pin signal du servo
  Serial.begin(9600);
}

void loop() {
  myservo.write(angle);
  angle += sens * 5;
  if (angle >= 180 || angle <= 0) sens = -sens;
  delay(50);
}

← Back to the board