Two-axis analog input driving the robot arm's base and shoulder servos
đšī¸ Push the stick a little for slow motion â push it far for fast motion
analogRead()millis() timingThe joystick module has five pins: GND, +5V, VRx (X-axis voltage), VRy (Y-axis voltage), and SW (the click button inside the stick).
Reading the diagram: the joystick's five pins land as GND â ground rail, +5V â power rail, VRx (blue) â A0, VRy (green) â A1, SW (orange) â D2. Each servo's yellow wire is its signal (pins 9 and 10); red and black feed the breadboard power rails.
Diagram vs. desk: same connections, real hardware. Compare this photo to the diagram above â the joystick sits on the arm's control panel, the servo cables run down to the breadboard rails, and the Uno ties it all together. Your wiring doesn't need to look identical; it needs to match electrically.
The joystick itself uses almost no current â it can share the Arduino 5V rail safely. The servos are still the power-hungry part: if they jitter, feed them from an external 5V supply and keep all grounds tied together.
A two-axis joystick is really two potentiometers in one package, plus a pushbutton when you press the stick straight down. You already know how to read a potentiometer â analogRead() returns a value from 0 to 1023:
"Near 512" is doing a lot of work in that sentence â real joysticks rarely rest at exactly 512, and the value jitters by a few counts even when nobody is touching the stick. Sections 4 and 5 deal with that.
First, prove the wiring works. This tiny sketch prints both axes to Serial Monitor â no servos needed yet:
// Joystick raw value reader
const int joyX = A0;
const int joyY = A1;
void setup() {
Serial.begin(9600);
}
void loop() {
int x = analogRead(joyX);
int y = analogRead(joyY);
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.println(y);
delay(150); // slow the stream down so humans can read it
} Upload it, open Serial Monitor, and don't touch the stick. Write down the resting X and Y values â that's your joystick's real center. Now push full-left, full-right, full-up, full-down and note which axis changes and in which direction. Every module is a little different.
Two problems to solve before driving servos. First, your stick's center might be 503 or 521 â not 512. So we calibrate in setup(): average a bunch of readings while the stick rests, and remember that as the true center:
int centerX = 512;
int centerY = 512;
const int deadZone = 25; // ignore wiggles smaller than this
void calibrateJoystick() {
long sumX = 0, sumY = 0;
Serial.println("Calibrating - hands off the stick!");
for (int i = 0; i < 50; i++) {
sumX += analogRead(joyX);
sumY += analogRead(joyY);
delay(10);
}
centerX = sumX / 50; // true resting center
centerY = sumY / 50;
} Second, even a resting stick wobbles by Âą10 counts or so. The dead zone says: if the stick is within 25 counts of center, treat it as centered. Without it, your servos would creep and jitter on their own.
deflection = reading - center
|deflection| < deadZone â stick counts as centered (do nothing)
|deflection| >= deadZone â real input (move, and move faster if far) In Lesson 11, every timed step moved exactly 1°. For joystick control that feels wrong â a small push should creep, a big push should zip. The trick: keep the timing fixed, and scale the step size with deflection:
small deflection (just past dead zone) â 1° per step â slow creep
large deflection (stick slammed over) â 4° per step â fast move const int maxStep = 4; // degrees per step at full deflection
int signOf(int v) {
return (v > 0) - (v < 0); // +1, -1, or 0
}
// How many degrees to move this step, given the stick deflection
int stepFor(int deflection) {
int d = abs(deflection);
if (d < deadZone) return 0; // inside dead zone: hold still
return map(d, deadZone, 512, 1, maxStep); // scale 1..4 degrees
} signOf(deflection) picks the direction, stepFor(deflection) picks how much. Multiply them and you get a signed step â the rest is the same timed-update pattern from Lesson 11.
Everything together: the joystick drives X â base and Y â shoulder, clicking the stick sends both servos home to 90°, and all motion uses the non-blocking timed step:
// ============================================================
// Lesson 12: Joystick Control
// Two-axis joystick driving two SG90 servos:
// X axis -> base rotation (pin 9)
// Y axis -> shoulder (pin 10)
// SW (stick click) -> both servos return home to 90
// Features: startup calibration, dead zone, speed that scales
// with deflection, non-blocking millis() motion.
// ============================================================
#include <Servo.h>
Servo baseServo;
Servo shoulderServo;
// ---- Pins ----
const int joyX = A0;
const int joyY = A1;
const int joySW = 2; // stick click button on D2 (INPUT_PULLUP)
// ---- Servo angles ----
int baseAngle = 90;
int shoulderAngle = 90;
const int MIN_ANGLE = 20;
const int MAX_ANGLE = 160;
// ---- Joystick calibration ----
int centerX = 512;
int centerY = 512;
const int deadZone = 25;
// ---- Motion timing ----
unsigned long lastStepTime = 0;
const int stepInterval = 15; // ms between motion steps
const int maxStep = 4; // degrees per step at full deflection
// ---- Stick button state ----
int lastSWState = HIGH;
int signOf(int v) {
return (v > 0) - (v < 0); // +1, -1, or 0
}
// How many degrees to move this step, given stick deflection
int stepFor(int deflection) {
int d = abs(deflection);
if (d < deadZone) return 0; // ignore tiny wiggles
return map(d, deadZone, 512, 1, maxStep); // 1..4 degrees
}
// Measure the true resting center at startup
void calibrateJoystick() {
long sumX = 0, sumY = 0;
Serial.println("Calibrating - hands off the stick!");
for (int i = 0; i < 50; i++) {
sumX += analogRead(joyX);
sumY += analogRead(joyY);
delay(10);
}
centerX = sumX / 50;
centerY = sumY / 50;
Serial.print("Center X: "); Serial.print(centerX);
Serial.print(" Center Y: "); Serial.println(centerY);
}
void setup() {
Serial.begin(9600);
baseServo.attach(9);
shoulderServo.attach(10);
pinMode(joySW, INPUT_PULLUP);
baseServo.write(baseAngle);
shoulderServo.write(shoulderAngle);
calibrateJoystick();
Serial.println("Joystick control ready. Click stick = HOME.");
}
void loop() {
// 1. Timed motion step (non-blocking)
if (millis() - lastStepTime >= stepInterval) {
lastStepTime = millis();
int dx = analogRead(joyX) - centerX; // deflection from center
int dy = analogRead(joyY) - centerY;
baseAngle = constrain(baseAngle + signOf(dx) * stepFor(dx), MIN_ANGLE, MAX_ANGLE);
shoulderAngle = constrain(shoulderAngle + signOf(dy) * stepFor(dy), MIN_ANGLE, MAX_ANGLE);
baseServo.write(baseAngle);
shoulderServo.write(shoulderAngle);
}
// 2. Stick click -> return home
int swState = digitalRead(joySW);
if (swState == LOW && lastSWState == HIGH) {
baseAngle = 90;
shoulderAngle = 90;
Serial.println("HOME");
}
lastSWState = swState;
} Last lesson, inputs set a target and the servos walked toward it. Here the stick is the motion command â no targets, just "keep stepping this direction at this speed while the stick is held." Both are the same timed-step engine; only the input changes.
Your joystick may be mounted rotated relative to the arm. Flip the sign: use -dx or -dy in the angle update.
The dead zone is too small or calibration ran while the stick was touched. Raise deadZone to 35 and reset the board.
Tune feel with maxStep (how fast full deflection is) and stepInterval (how often steps happen). Try maxStep 3 and interval 20.
You're pushing against MIN_ANGLE/MAX_ANGLE or the mechanical stop. Widen the code limits only if the hardware allows it.