Projects / Tilt-controlled LED ball

Tilt-controlled LED ball

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO13.8 mA

    Install the library

    Add LedControl.

  2. NANO

    Wire the sensor

    VCC, GND, SDA to A4, SCL to A5.

  3. NANO13.8 mA

    Wire the matrix

    DIN to D12, CLK to D11, CS to D10, plus power.

  4. NANO

    Upload the sketch

    A dot and a few wall pixels appear.

  5. NANO

    Tilt the board

    The dot rolls in the tilt direction.

  6. NANO

    Fix the direction

    Change SWAP_XY or INVERT constants.

  7. NANO

    Test the walls

    The dot stops at each wall.

  8. NANO

    Design a maze

    Edit the walls table.

In short

How do you build a tilt-controlled LED ball?

A dot on an LED matrix rolls as you tilt a board, like a ball in a maze. You need Arduino Nano, MPU6050 IMU, MAX7219 8x8 LED matrix module and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MPU6050 VCCNano 5VGY-521 style boards have a regulator, check yours
MPU6050 GNDNano GNDCommon ground
MPU6050 SDANano A4I2C data
MPU6050 SCLNano A5I2C clock
Matrix VCCNano 5VPower
Matrix GNDNano GNDCommon ground
Matrix DINNano D12Data
Matrix CLKNano D11Clock
Matrix CSNano D10Chip select

Code

The MPU6050 contains an accelerometer that measures the pull of gravity along each axis, which makes it a tilt sensor: tilt the board to the right and the x reading goes up. The sketch reads the x and y values over I2C without any library, and turns them into a small push on a ball on the 8 by 8 matrix. The dead zone stops the ball from drifting when the board is almost flat. Walls are stored in a short table with one byte per row, and the ball is stopped if the next position is a wall, so you can design a maze by changing the bits. The matrix and the sensor may be mounted at different angles on your board, so if the ball goes the wrong way, change the swap and invert constants at the top. A matrix at low brightness uses little current, but at full brightness it can draw a couple of hundred milliamps, which is more than is good for a USB port shared with other things. Keep the two modules fixed to the same piece of card so they tilt together.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <LedControl.h>

LedControl lc = LedControl(12, 11, 10, 1);   // DIN, CLK, CS, one matrix
const byte MPU = 0x68;

// Wall pattern: one byte per row, a 1 bit is a wall. Edit to make your own maze.
byte walls[8] = {
  0b00000000,
  0b00010000,
  0b00010000,
  0b00000000,
  0b00001000,
  0b01101000,
  0b00000000,
  0b00000000
};

float x = 0, y = 0;                // ball position, 0 to 7
const bool SWAP_XY = false;        // change these if the ball moves the wrong way
const bool INVERT_X = false, INVERT_Y = false;

void readAccel(float &ax, float &ay) {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, (byte)6, (byte)true);
  int16_t rx = (Wire.read() << 8) | Wire.read();
  int16_t ry = (Wire.read() << 8) | Wire.read();
  ax = rx / 16384.0;               // g, with the default plus or minus 2 g range
  ay = ry / 16384.0;
}

bool isWall(int r, int c) { return walls[r] & (1 << (7 - c)); }

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);                // wake the sensor
  Wire.write(0);
  Wire.endTransmission();
  lc.shutdown(0, false);
  lc.setIntensity(0, 4);
  lc.clearDisplay(0);
}

void loop() {
  float ax, ay;
  readAccel(ax, ay);
  if (SWAP_XY) { float t = ax; ax = ay; ay = t; }
  if (INVERT_X) ax = -ax;
  if (INVERT_Y) ay = -ay;

  // Tilt sets the speed. A small dead zone stops the ball from creeping on a flat board.
  float dx = (fabs(ax) > 0.08) ? ax * 0.4 : 0;
  float dy = (fabs(ay) > 0.08) ? ay * 0.4 : 0;

  float nx = constrain(x + dx, 0, 7.49), ny = constrain(y + dy, 0, 7.49);
  if (!isWall((int)round(ny), (int)round(x))) y = ny;     // move up and down
  if (!isWall((int)round(y), (int)round(nx))) x = nx;     // move left and right

  lc.clearDisplay(0);
  for (int r = 0; r < 8; r++)
    for (int c = 0; c < 8; c++)
      if (isWall(r, c)) lc.setLed(0, r, c, true);
  lc.setLed(0, (int)round(y), (int)round(x), true);
  delay(40);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Board flatDot stays still
Tilt rightDot moves right
Tilt toward a wallDot stops at the wall
Tilt to the edgeDot stops at the edge
Tilt backDot moves back

If it does not work

Dot never moves

Check the SDA and SCL wires and that the sensor is awake.

Dot goes the wrong way

Change SWAP_XY, INVERT_X or INVERT_Y.

Matrix blank

Check DIN, CLK and CS.

Dot drifts when flat

Increase the dead zone from 0.08.

Dot jumps over a wall

Lower the 0.4 speed factor.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the tilt-controlled LED ball?

Arduino Nano, MPU6050 IMU, MAX7219 8x8 LED matrix module and Breadboard 830 Tie Points.

How long does the tilt-controlled LED ball take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the tilt-controlled LED ball works?

Board flat: Dot stays still. Tilt right: Dot moves right. Tilt toward a wall: Dot stops at the wall.

Dot never moves: what should I check?

Check the SDA and SCL wires and that the sensor is awake.

Dot goes the wrong way: what should I check?

Change SWAP_XY, INVERT_X or INVERT_Y.

Matrix blank: what should I check?

Check DIN, CLK and CS.

Dot drifts when flat: what should I check?

Increase the dead zone from 0.08.