🛠 Piloter un micro-servo avec le joystick
Objectives
Move beyond the all-in-one setup: the Esplora leaves very few free pins (A0–A6, A11, D5, D6, D9, D10, and D13 are consumed by internal hardware), and the library comments officially expose only D3 and D11 as external outputs. This tutorial uses D3 to control an angular micro-servo with the analog joystick — a mechanical version of the "gamepad" gesture.
Hardware
- Arduino Esplora + micro-USB cable.
- 1 SG90-type micro-servo (warning: do not exceed the USB port current budget, protected by a ~500 mA polyfuse; a single lightweight SG90 is fine).
- 3 male-to-female wires: signal → D3, +5 V, GND.
- Esplora and Servo libraries.
Complete code
/* Joystick -> servo sur la sortie externe D3 (seules D3 et D11 sont
documentees comme sorties externes sur Esplora). */
#include <Esplora.h>
#include <Servo.h>
const byte BROCHE_SERVO = 3; // sortie externe documentee
Servo moteur;
void setup {
moteur.attach(BROCHE_SERVO); // la lib Servo utilise un timer de l'ATmega32u4
moteur.write(90); // position neutre
Serial.begin(9600);
}
void loop {
int x = Esplora.readJoystickX; // -512 (gauche) .. 511 (droite)
// Mise a l'echelle vers 0..180 degres avec zone morte centrale
if (abs(x) < 20) x = 0;
int angle = map(x, -512, 511, 0, 180);
moteur.write(angle);
Serial.print("joyX="); Serial.print(x);
Serial.print(" angle="); Serial.println(angle);
// Bonus : le buzzer (D6) bipe proportionnellement a l'angle
if (Esplora.readButton(SWITCH_UP) == PRESSED) Esplora.tone(200 + angle * 4);
else Esplora.noTone;
delay(20);
}
Expected verification
Push the joystick to the left: the servo turns to 0°. To the right: 180°. In the center (±20 dead zone), it holds 90° without buzzing. The serial monitor confirms the joystick/angle mapping. Press the up button: the buzzer emits a sound whose pitch follows the angle.
Common errors
- Connecting to an occupied pin: D5/D6/D9/D10 (RGB + buzzer) and D13 (L LED) are taken; A0–A6/A11 are used for the multiplexer and accelerometer. Use only D3 or D11.
- Servo buzzing in the center: without a dead zone, joystick noise (±10) causes the servo to vibrate;
if (abs(x) < 20) x = 0;is essential. - Drawing too much power from the 5 V rail: multiple servos or a large model cause the USB voltage to drop and trigger the polyfuse; power them from an external source with the ground connected to the Esplora's ground.
- Forgetting the 0/180 range: some servo manufacturers specify 10–170; if the servo stalls at the limits, reduce the range in the
mapfunction. - Timer conflict: the Servo library and
toneshare timers on the ATmega32u4; on this board, both work together, but if you add additional PWM libraries, check the timer conflicts declared by each.
Other tutorials for this board
Code examples for this board
joystick-couleur.inoArduinojeu-boutons.inoArduinoslider-luminosite.inoArduinothermometre-couleur.inoArduinosonometre.inoArduinoaccelerometre-tilt.inoArduino