Reid Paulhus
Published © GPL3+

Visual Accelerometer

This project connects the ADLX345 3-Axis accelerometer to display a location on an 8x8 LED matrix using the MAX7219 LED driver chip.

IntermediateFull instructions provided1 hour12,731
Visual Accelerometer

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
Jumper wires (generic)
Jumper wires (generic)
Female/Female Jumper Wires
Female/Female Jumper Wires
SparkFun Triple Axis Accelerometer Breakout - ADXL345
SparkFun Triple Axis Accelerometer Breakout - ADXL345
MAX7219 LED Driver Chip

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines



Read more



*** USE the MAX7219 Matrix (5 pin) ***



Comments are in the code - Plug it in and run it
#include <LedControl.h>
#include <Wire.h>

#define DEVICE (0x53)   //ADXL345 device address
#define TO_READ (6)     //num of bytes we are going to read (two bytes for each axis)

byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
char str[512];          //string buffer to transform data before sending it

LedControl lc = LedControl(12, 11, 10, 1); // DIN, CLK, CS, NRDEV
unsigned long delaytime = 50;
int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;

// object that represents a single light location
// future update with gravity
class Grain
    int x = 0;
    int y = 0;
    int mass = 1;
Grain *g;

void setup()
  // set up a grain object
  g = new Grain();


  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output

  //Turning on the ADXL345
  writeTo(DEVICE, 0x2D, 0);
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);

void loop()
  // The first axis-acceleration-data register
  int regAddress = 0x32;
  int x, y, z;

  readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from ADXL345

  // Combine the two bytes of each direction
  // Least significant bit first
  x = (((int)buff[1]) << 8) | buff[0];
  y = (((int)buff[3]) << 8) | buff[2];
  z = (((int)buff[5]) << 8) | buff[4];

  // Convert the values into values that can be represented on the matrix
  x = map(x, -300, 300, 0, 8);
  y = map(y, -300, 300, 0, 8);
  z = map(z, -300, 300, 0, 8);

  //we send the x y z values as a string to the serial port
  Serial.print("X: ");
  Serial.print("   Y: ");
  Serial.print("   Z: ");

  // assign the grain to this location
  g->x = x;
  g->y = y;
  lc.setLed(0, g->x, g->y, true);

  //add some delay between each update

void ClearDisplay()
  // sets up the lcd display
  int devices = lc.getDeviceCount();

  for (int address = 0; address < devices; address++)
    lc.shutdown(address, false);
    lc.setIntensity(address, 1);

//Writes val to address register on device
void writeTo(int device, byte address, byte val) 
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        // send register address
  Wire.write(val);        // send value to write
  Wire.endTransmission(); //end transmission

//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) 
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission

  Wire.beginTransmission(device); //start transmission to device
  Wire.requestFrom(device, num);    // request 6 bytes from device

  int i = 0;
  while (Wire.available())   //device may send less than requested (abnormal)
    buff[i] =; // receive a byte
  Wire.endTransmission(); //end transmission


Reid Paulhus

Reid Paulhus

2 projects • 6 followers
Got hooked on Arduinos! I’m a game developer. Check out my twitter:
Thanks to Bill Earl.