servo-balayage.ino

Arduino Duemilanove — Board Fiche · 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 Duemilanove (ATmega168 or 328P, 16 MHz, 5 V) with its mini-USB-B cable; the serial port appears once the FTDI driver is installed. 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 Duemilanove » (or « Diecimila » for very old boards) and the FTDI port, then Upload. The ATmega168 leaves only 14 KB for the sketch.

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