Project tutorial
Visual Accelerometer

Visual Accelerometer © GPL3+

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

  • 4,022 views
  • 1 comment
  • 12 respects

Components and supplies

Necessary tools and machines

hands

Apps and online services

About this project

Introduction

To run this project, simply follow the wiring diagram, and then run the code in your Arduino IDE.

I made this project as a stepping stone toward larger projects that involve the ADLX345 accelerometer. This was made as a demonstration of how you can utilize both the chip, and how to display its results in the Serial monitor and physically through the LED matrix.

I decided to use the MAX7219 LED Driver to minimize the setup and the number of wires needed. It also happens to take up only 3 pins on the Arduino Uno which is a plus. As for how we orientate the matrix, unfortunately for it to follow the orientation of the accelerometer, I was not able to secure it, instead I taped it down to the board to hold it securely.

Challenges

The biggest challenge in my opinion when building this was understanding exactly what each address represents internally in the accelerometer chip. I will provide a sheet sheet that I found online that helped in setting up which addresses to READ and WRITE to.

Code

ADXL345_Matrix_DisplayC/C++
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

int MATRIX_WIDTH = 8;
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
{
  public:
    int x = 0;
    int y = 0;
    int mass = 1;
};
Grain *g;



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

  ClearDisplay();

  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(x);
  Serial.print("   Y: ");
  Serial.print(y);
  Serial.print("   Z: ");
  Serial.print(z);
  Serial.print("\n");

  ClearDisplay();
  // 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
  delay(10);
}



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);
    lc.clearDisplay(address);
  }
}



//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] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}

Schematics

Schematic
*** USE the MAX7219 Matrix (5 pin) ***
Acceleratormatrixled bb 3hd9dhizvz

Comments

Similar projects you might like

Level Platform Using Accelerometer

Project tutorial by Mark Tashiro

  • 16,966 views
  • 15 comments
  • 29 respects

8×8 LED and Accelerometer

Project tutorial by Brink.IO

  • 1,234 views
  • 0 comments
  • 3 respects

Accelerometer Using Arduino 101

by Jefferson Paredes

  • 2,663 views
  • 0 comments
  • 3 respects

Gesture Recognition Using Accelerometer and ESP

Project tutorial by mellis

  • 37,800 views
  • 17 comments
  • 70 respects

Memsic2125 Accelerometer (Mx2125)

by MicroBob

  • 3,600 views
  • 0 comments
  • 12 respects

Visual Capturing with OV7670 on Arduino

by Techmirtz

  • 56,323 views
  • 54 comments
  • 74 respects
Add projectSign up / Login