Want to move a robot, a servo or a camera by hand? A joystick module is the simplest way. It's like a games controller stick, and inside it are things you already know well.
What's inside?
- A potentiometer for the X axis, and one for the Y axis (usually 10k).
- A button that operates when you press the stick down.
Each potentiometer is wired as a voltage divider: one end at 5 V, one at ground, and the wiper to the pin. So the stick in the middle → about 2.5 V, and at the end → 0 or 5. The Arduino reads that with the ADC.
Wiring
| Module | Arduino Uno |
|---|---|
| GND | GND |
| +5V | 5V |
| VRx | A0 |
| VRy | A1 |
| SW | A digital pin (for example 2) |
With an ESP32: power it from 3.3 V (not 5) so the output doesn't exceed the pin's voltage, and the reading will be from 0 to 4095.
The code
const int X = A0, Y = A1, SW = 2;
void setup() {
pinMode(SW, INPUT_PULLUP); // the button needs a pull-up
Serial.begin(9600);
}
void loop() {
int x = analogRead(X);
int y = analogRead(Y);
bool pressed = digitalRead(SW) == LOW;
Serial.print(x); Serial.print(" ");
Serial.print(y); Serial.print(" ");
Serial.println(pressed);
delay(100);
}
Open the Serial Plotter and move the stick: you'll see the two lines move.
The centre isn't 512
With the stick released, the reading is usually something like 498 or 521, and not perfectly steady. If you base movement on it directly, the robot will creep by itself.
The fix: a dead zone
int center = 512; // or read it in setup
int v = analogRead(X) - center;
if (abs(v) < 40) v = 0; // ignore small movements
Better still, read the real centre in setup() with the stick released, and use it instead of 512.
Calibrating in setup
int cx, cy;
void setup() {
long sx = 0, sy = 0;
for (int i = 0; i < 20; i++) {
sx += analogRead(X); sy += analogRead(Y);
delay(5);
}
cx = sx / 20; cy = sy / 20; // the average of 20 readings with the stick released
}
Don't touch the stick while powering up, because the program assumes it's centred.
From reading to motor
int speed = map(v, -512, 512, -255, 255);
speed = constrain(speed, -255, 255);
- The sign (positive or negative) = direction.
- The magnitude = speed via PWM.
Send it to the motor driver. And if you're moving a servo, convert it to an angle from 0 to 180. See servos and steppers.
Each half separately
If the centre is 520, the lower half has 520 steps and the upper only 503. With a single map, the top speed in one direction will be lower than the other. More accurate:
int raw = analogRead(X);
int speed;
if (raw > cx) speed = map(raw, cx, 1023, 0, 255);
else speed = map(raw, 0, cx, -255, 0);
Smoother control
You want small movements near the centre to be precise, and big ones to be fast. Square the value while keeping its sign:
long s = (long)speed * abs(speed) / 255; // a curve instead of a straight line
Now the first half of the travel gives small speeds, and the end gives full speed.
A two-motor car (tank mixing)
Want one stick to drive a two-wheeled car: Y for forward and back, and X for turning:
int left = constrain(y + x, -255, 255);
int right = constrain(y - x, -255, 255);
- Stick forward only → both at the same speed.
- Stick right only → left forward and right backward, so the car spins on the spot.
- If the car turns the opposite way to what you expect, invert the sign of X.
A pan/tilt servo
Two servos for a camera: X turns left and right, and Y tilts up and down:
pan.write(map(analogRead(X), 0, 1023, 0, 180));
tilt.write(map(analogRead(Y), 0, 1023, 20, 160)); // safety limits
The tilt limits of 20 to 160 stop the servo hitting the mount. And if you want the camera to stay where it is when you release the stick, use the stick to nudge the angle gradually instead of setting it directly.
The button
The button closes to ground, so it needs INPUT_PULLUP and a press = LOW. And it bounces like any button, so use debouncing if it will do something like toggle a mode.
Tips
- The axes may be reversed depending on how the module is mounted. Invert them in code instead of turning the module.
- A cheap joystick isn't accurate at the extremes. Don't rely on it reaching exactly 0 and 1023.
- Want wireless control? See wireless modules and the HC-05.
- With an ESP32: its ADC isn't linear at the extremes, so calibration and the dead zone matter more. And use ADC1 pins if Wi-Fi is on.
Common faults
| Fault | Usual cause |
|---|---|
| The robot creeps by itself | No dead zone or calibration |
| The reading is stuck at 1023 or 0 | The +5V or GND wire is disconnected |
| The button is always pressed | Missing INPUT_PULLUP |
| One axis affects the other | A cheap module, or noise from the motor on the ground |
FAQ
Can I connect two joysticks to an Uno? Yes, the Uno has 6 analog pins, so up to 3 joysticks.
What if I need more precision? A Hall-sensor joystick (instead of potentiometers) is smoother and doesn't wear out, and is found in expensive game controllers.