Arduino Analog Joystick: Wiring, Code and Simulator
To build this Arduino Analog Joystick project you need an Arduino Uno and an analog joystick. An analog joystick: HORZ on A0, VERT on A1, and its push-switch on pin 2. Make the 5 connections in the wiring table, upload the Arduino C++ code, and move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | analog joystick | HORZ and VERT to analog pins (about 512 at rest; left and up read higher), SEL is a button to GND. VCC to 5V. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | analog joystick VCC |
| 2 | Arduino Uno GND | analog joystick GND |
| 3 | Arduino Uno A0 | analog joystick HORZ |
| 4 | Arduino Uno A1 | analog joystick VERT |
| 5 | Arduino Uno pin 2 | analog joystick SEL |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add an analog joystick from the parts palette.
- Wire 1. Connect Arduino Uno 5V to analog joystick VCC.
- Wire 2. Connect Arduino Uno GND to analog joystick GND.
- Wire 3. Connect Arduino Uno A0 to analog joystick HORZ.
- Wire 4. Connect Arduino Uno A1 to analog joystick VERT.
- Wire 5. Connect Arduino Uno pin 2 to analog joystick SEL.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.
Arduino C++ code
// Joystick: HORZ on A0, VERT on A1, the push-switch SEL on pin 2.
void setup() {
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int x = analogRead(A0);
int y = analogRead(A1);
bool pressed = digitalRead(2) == LOW;
Serial.print("x=");
Serial.print(x);
Serial.print(" y=");
Serial.print(y);
Serial.println(pressed ? " pressed" : "");
delay(200);
}
What to try
Move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.
Frequently asked questions
How do I wire the Analog Joystick project on an Arduino Uno?
Make 5 connections: Arduino Uno 5V to analog joystick VCC; Arduino Uno GND to analog joystick GND; Arduino Uno A0 to analog joystick HORZ; Arduino Uno A1 to analog joystick VERT; Arduino Uno pin 2 to analog joystick SEL.
What parts do I need for this Arduino project?
An Arduino Uno and an analog joystick.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
An analog joystick: HORZ on A0, VERT on A1, and its push-switch on pin 2.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator