Skip to content
Hibiiro

HIBIIRO

Controlling a 16-LED NeoPixel Ring with an Arduino and Analog Joystick

Photosensitivity Warning
This article includes animated GIFs with flashing LED lights.

Parts Used

  • 1× Arduino Uno R3 (or compatible board)
  • 1× 2-Axis Analog Thumb Joystick Module (with push switch)
  • 1× 16-Bit WS2812B RGB LED Ring (NeoPixel compatible)
  • 10× Jumper Wires (AWG 22–24 hookup wire)
  • 1× Half-Size Breadboard

Development Environment

Arduino IDE 2.3.4

Library

Adafruit_NeoPixel v1.12.3

Wiring Diagram

Both modules connect directly to the Arduino’s 5V, GND, analog inputs (A0, A1), and digital pins (D6, D7). Using a mini breadboard makes sharing the 5V and GND rails simple. (Need to check other pins? See our Interactive Arduino Uno Pinout Viewer.)

Arduino Uno wiring diagram with 16-LED NeoPixel ring and analog joystick

Arduino Sketch

Here is the complete sketch, including startup center calibration, deadzone filtering, directional angle mapping (atan2), and push-button LED latching.

Tip: If the joystick tilt direction doesn’t match the physical orientation of your LED ring, swap the VRX and VRY analog pins or invert the axis sign in atan2().

#include <Adafruit_NeoPixel.h>
#define PIN        6
#define NUMPIXELS  16
#define BRIGHTNESS 50

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const int VRX = A0;
const int VRY = A1;
const int SW = 7;
const int threshold = 20; // Deadzone threshold

int center_x, center_y;
bool red_lit[NUMPIXELS] = {false};

void calibrate_center() {
  long sum_x = 0, sum_y = 0;
  const int samples = 100;

  for (int i = 0; i < samples; i++) {
    sum_x += analogRead(VRX);
    sum_y += analogRead(VRY);

    int led_index = i % NUMPIXELS;
    pixels.clear();
    pixels.setPixelColor(led_index, pixels.Color(0, 0, 150));
    pixels.show();

    delay(10);
  }

  center_x = sum_x / samples;
  center_y = sum_y / samples;

  for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));
  }
  pixels.show();
  delay(500);
  pixels.clear();
  pixels.show();
}

void setup() {
  pinMode(SW, INPUT_PULLUP);
  pixels.begin();
  pixels.setBrightness(BRIGHTNESS);

  calibrate_center();
}

void loop() {
  pixels.clear();

  int xpos = analogRead(VRX) - center_x;
  int ypos = analogRead(VRY) - center_y;

  // Apply deadzone
  if (abs(xpos) < threshold) xpos = 0;
  if (abs(ypos) < threshold) ypos = 0;

  if (digitalRead(SW) == LOW) {
    if (xpos == 0 && ypos == 0) {
      for (int i = 0; i < NUMPIXELS; i++) {
        pixels.setPixelColor(i, pixels.Color(0, 0, 150));
      }
      pixels.show();
      delay(100);

      for (int i = 0; i < NUMPIXELS; i++) {
        red_lit[i] = false;
      }
    } else {
      float angle = atan2(-ypos, xpos) * 180 / PI;
      if (angle < 0) angle += 360;
      int direction = int(angle / 22.5) % NUMPIXELS;

      red_lit[direction] = true;
    }
  }

  for (int i = 0; i < NUMPIXELS; i++) {
    if (red_lit[i]) {
      pixels.setPixelColor(i, pixels.Color(150, 0, 0));
    }
  }

  if (xpos != 0 || ypos != 0) {
    float angle = atan2(-ypos, xpos) * 180 / PI;
    if (angle < 0) angle += 360;
    int direction = int(angle / 22.5) % NUMPIXELS;
    pixels.setPixelColor(direction, pixels.Color(0, 150, 0));

    if (red_lit[direction]) {
      pixels.setPixelColor(direction, pixels.Color(150, 150, 150));
    }
  }

  pixels.show();
}

Startup Center Calibration

In setup(), calibrate_center() samples the neutral resting position of the joystick 100 times while spinning a blue loading animation around the ring, then flashes green when ready. Make sure not to touch the stick during this startup calibration!

Startup calibration animation on the 16-LED ring

How It Works in Action

Controlling the LED ring direction and latching pixels with the joystick
  • Tilting the joystick lights up the corresponding green LED in that direction (360° divided into 16 segments of 22.5°).
  • Returning the stick to the neutral center position turns the green cursor LED off.
  • Clicking the joystick button while tilting latches that LED on in red (remaining lit even after you release the stick).
  • Pointing the cursor over an already-latched red LED highlights it in white.
  • Clicking the joystick while centered flashes the entire ring blue and clears all latched red LEDs.

A rotary encoder would offer more precise step-by-step selection across 16 LEDs, but since analog joystick modules cost only a few dozen yen each, it was a fun experiment for radial menu input!