SALES INQUIRIES: 1 (888) 767-9864

What to do when you just don’t know :: Arduino, ADXL345 triple axis accelerometer and an RGB LED

This lesson discusses what to do when you open an existing program and realize that you simply don’t understand all the stuff that is going on. It also talks about using the ADXL345 triple axis accelerometer breakout board to control the hue of a Red-Green-Blue (RGB) LED.

Here are some links to the SparkFun website for the ADXL345 breakout board and the getting started guide.

ADXL345 Breakout Board

ADXL345 Getting Started Guide

6 Comments

  1. Avatar Oh Baby! on February 13, 2018 at 5:08 pm

    This is exactly what I’m trying to do with the Arduino 9 axis shield .
    BTW, why is there no current limiting resistor used for the RGB LED? Did I miss it or is it not needed?

    • Avatar MICHAEL JAMES on February 13, 2018 at 9:09 pm

      Yes, there should be current limiting resistors with an RGB LED, I left them out for simplicity sake.

  2. Avatar Bob Mason on July 20, 2019 at 12:50 pm

    My code for the ADXL345 won’t run. Double checked it several times and can see no errors.
    Can you find the problem?

    // Programming Electronics Academy
    // using the ADXL345 accelerometer breakout board

    #include

    // assign ‘ chip select ‘ signal to pin 10
    int CS = 10;

    // set up some of the registers available

    char POWER_CTL = 0x2D; // power control register
    char DATA_FORMAT = 0x31;
    char DATAX0 = 0x32; // X-Axis Data 0
    char DATAX1 = 0x33; // X-Axis Data 1
    char DATAY0 = 0x34; // Y-Axix Data 0
    char DATAY1 = 0x35; // Y-Axis Data 1
    char DATAZ0 = 0x36; // Z-Axis Data 0
    char DATAZ1 = 0x37; // Z-Axis Data 1

    // this buffer holds values from registers
    char values[10];

    // these variables hold values of x, y, and z axis
    int x,y,z;

    void setup()
    {
    // initiate SPI communications
    SPI.begin();

    // configure the SPI connection
    SPI.setDataMode(SPI_MODE3);

    Serial.begin(9600);

    // set up chip select pin
    pinMode(CS,OUTPUT);
    digitalWrite(CS, HIGH);

    // put board into +/- 4G range
    writeRegister(DATA_FORMAT, 0x01);

    // put board into measurement mode
    writeRegister(POWER_CTL, 0x08);
    }

    void loop()
    {
    // read 6 bytes of data starting at DATAX0
    readRegister(DATAX0, 6, values);

    // gives 10 bit acceleration values but are stored as 8 bit
    // x value stored in [0] and [1]
    x = ((int)values[1]<<8)|(int)values[0];
    // y value stored in [2] and [3]
    y = ((int)values[3]<<8)|(int)values[2];
    // z value stored in [4] and [5]
    z = ((int)values[5]< 1) address = address | 0x40;
    digitalWrite(CS, LOW);
    SPI.transfer(address);

    for (int i = 0; i < numBytes; i++)
    {
    values[i] = SPI.transfer(0x00);
    }

    digitalWrite(CS, HIGH);
    }

  3. Avatar Bob Mason on July 20, 2019 at 6:26 pm

    re: my prior question about my ADXL345 not running

    I used the spi library from the Arduino ide and the code compiles fine

    I don’t get any change in the color of the RGB LED and no change in the data
    displayed on the serial monitor whether I go with the original code or when
    I map the x, y, z variables.

    In fact the displayed data on the monitor does not format as it should based on the code

    • Avatar Michael James on July 23, 2019 at 8:01 am

      I would try a ADXL345 Arduino library and test your circuit with one of the example sketches that comes with the library.

      If that doesn’t work, then it may be that issue lies in the circuit.

Leave a Comment