🛠 Piloter un micro-servo avec le joystick

Arduino Esplora — Board Fiche · intermediaire · Schéma : IMG-esplora-intermediaire-3-esplora-servo-joystick.svg — Esplora vue de dessus, joystick à gauche, câble vers un micro-servo branché sur la sortie externe D3 avec alimentation et masse annotées.

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

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

  1. 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.
  2. 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.
  3. 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.
  4. Forgetting the 0/180 range: some servo manufacturers specify 10–170; if the servo stalls at the limits, reduce the range in the map function.
  5. Timer conflict: the Servo library and tone share 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

← Back to the board 🛠 All tutorials