servo-balayage.ino

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

The Servo library of the UNO R4 core drives standard servos (1-2 ms pulse at 50 Hz) without the AVR timer conflicts. A full 0-180° sweep serves as a test bench before mounting it in a mechanism.

Hardware

An Arduino UNO R4 Minima (Renesas RA4M1, Cortex-M4 @ 14 MHz, 5 V, 12-bit DAC, no radio) powered by USB or 6-24 V. 1 hobby servo (SG90/MG90S); external 5 V supply for bigger models.

Wiring

Signal (orange) to D9, + (red) to 5 V, - (brown) to common GND.

How it works

myservo.write(angle) sends the position pulse; the loop increments the angle by 1° every 20 ms and reverses at the stops.

Running it

Arduino IDE 2.x (with the « Arduino UNO R4 Boards » core installed), pick « Arduino UNO R4 Minima », select the port and Upload. The serial monitor runs at 9600.

Variants

Add a potentiometer on A1 for angle following, or show the angle on the LED matrix (R4 WiFi).

servo-balayage.ino

// Balayage servo avec la bibliotheque Servo (portage RA4M1).
#include <Servo.h>

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

void setup() {
  myservo.attach(9);
  myservo.write(0);
  Serial.begin(9600);
}

void loop() {
  myservo.write(angle);
  static unsigned long t0 = 0;
  if (millis() - t0 < 20) return;
  t0 = millis();

  angle += sens;
  if (angle >= 180) sens = -1;
  if (angle <= 0) sens = 1;
}

// Sur R4, la lib Servo utilise les timers du RA4M1 sans
// entrer en conflit avec analogWrite() comme sur les AVR.
// Alimentation externe conseillee au-dela du micro-servo.

← Back to the board