Built-in project · Arduino Uno R3

Joystick bar graph on an Arduino Uno

A thumb stick moves a bar along eight LEDs, and pressing the stick swaps the bar to the other axis. The whole build, an Arduino Uno and 17 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

IntermediateRuns in your browser. Free, and no account needed.

Step by step, with the wiring and the common mistakes: Arduino Joystick Module: Wiring, Code, Live Demo

Joystick bar graph · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Bar graph: a thumb stick's X axis drawn on eight LEDs.
//
//   pins 2..9  eight LEDs, each through 220 ohm to GND, left to right
//   A0         the stick's VRX        A1  its VRY
//   pin 12     the stick's SW, to GND, internal pull-up
//
// analogRead gives 0..1023 for 0 to 5 V, and map() turns that into 0..8 lamps.
// That is the whole of a bar graph: the meter is the arithmetic, the LEDs are
// just where the answer is written down.
//
// Press the stick and the bar switches to the Y axis, so the same eight lamps
// read the other pot. The mode is printed when it changes, so the serial
// monitor says which axis you are looking at.
//
// A stick does not spring back to exactly 512, and the driver in
// mokxi_joystick.h samples where it rests at begin() and measures from there.
// Here the raw reading is what is wanted (the bar should be half lit when
// the stick is centered), so this one calls x() and y() rather than dx()
// and dy().

#include "mokxi_joystick.h"

const int LED_PINS[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int LEDS = 8;

Joystick stick(A0, A1, 12);

bool useY = false;

static void bar(int lit) {
  for (int i = 0; i < LEDS; i++) {
    digitalWrite(LED_PINS[i], i < lit ? HIGH : LOW);
  }
}

void setup() {
  for (int i = 0; i < LEDS; i++) {
    pinMode(LED_PINS[i], OUTPUT);
  }
  stick.begin();
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: joystick bar graph");
  Serial.println("push the stick left and right; press it to swap axis");
}

void loop() {
  if (stick.clicked()) {
    useY = !useY;
    Serial.print("reading the ");
    Serial.println(useY ? "Y axis" : "X axis");
  }

  int raw = useY ? stick.y() : stick.x();
  int lit = (int)map(raw, 0, 1023, 0, LEDS);
  if (lit > LEDS) {
    lit = LEDS;
  }
  bar(lit);

  static int last = -1;
  if (lit != last) {
    last = lit;
    Serial.print(useY ? "Y " : "X ");
    Serial.print(raw);
    Serial.print(" -> ");
    Serial.print(lit);
    Serial.println(" lamps");
  }

  delay(40);
}

Parts list

19 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

21 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 12; Thumb stick pin SW
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (8) pin 1
  • Arduino Uno R3 pin 8; Resistor, 220 Ω (7) pin 1
  • Arduino Uno R3 pin 7; Resistor, 220 Ω (6) pin 1
  • Arduino Uno R3 pin 6; Resistor, 220 Ω (5) pin 1
  • Arduino Uno R3 pin 5; Resistor, 220 Ω (4) pin 1
  • Arduino Uno R3 pin 4; Resistor, 220 Ω (3) pin 1
  • Arduino Uno R3 pin 3; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 2; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 5V; Thumb stick pin VCC
  • Ground: Arduino Uno R3 pin GND; Thumb stick pin GND; LED, green (1) pin C; LED, green (2) pin C; LED, green (3) pin C; LED, yellow (1) pin C; LED, yellow (2) pin C; LED, yellow (3) pin C; LED, red (1) pin C; LED, red (2) pin C
  • Arduino Uno R3 pin A0; Thumb stick pin VRX
  • Arduino Uno R3 pin A1; Thumb stick pin VRY
  • Resistor, 220 Ω (1) pin 2; LED, green (1) pin A
  • Resistor, 220 Ω (2) pin 2; LED, green (2) pin A
  • Resistor, 220 Ω (3) pin 2; LED, green (3) pin A
  • Resistor, 220 Ω (4) pin 2; LED, yellow (1) pin A
  • Resistor, 220 Ω (5) pin 2; LED, yellow (2) pin A
  • Resistor, 220 Ω (6) pin 2; LED, yellow (3) pin A
  • Resistor, 220 Ω (7) pin 2; LED, red (1) pin A
  • Resistor, 220 Ω (8) pin 2; LED, red (2) pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.