Arduino #13 – 3 axis Accelerometer ADXL 345 I2C – Robots, Quadcopters, etc.

Arduino #13 – 3 axis Accelerometer ADXL 345 I2C – Robots, Quadcopters, etc.

In the thirteenth Arduino tutorial, we are taking a look at the ADXL345 Module which features a triple axis accelerometer IC. The focus of the tutorial is to leverage the Arduino Wire tutorial in order to communicate over the Arduino I2C protocol.

Hardware Setup

Connecting the Accelerometer is quite simple. You will need several wire jumpers and two pull up resistors. The value of the said resistors can range from 5k all the way to 100k. Their purpose is to keep the I2C lines high because the protocol relies on pulling them down only. It is important to note that it may be possible to omit these resistors, but communication issues may show themselves depending on your set-up.

Connect your Arduino to the sensor as per the diagram shown below:
Arduino I2c

Programming the Arduino I2C

As mentioned above, we will rely on the Wire.h library. Below are the functions we will be using.

  • Wire.begin() – Initiates the Arduino I2C Protocol.
  • Wire.beginTransmission(X) – Initiates communication to an I2C capable device with the Address X.
  • Wire.write(X) – Transmits the specified data X from the Master Device.
  • Wire.endTransmission() – Terminates transmission.
  • Wire.requestFrom(X, Y) – Requests data of length Y from the device located at address X.

Full Documentation of Wire.h: Arduino Wire.h Library

———-
#include

#define accel_module (0x53)
byte values[6] ;
char output[512];

void setup(){
Wire.begin();
Serial.begin(9600);
———-
The above code initializes our program. The address of the ADXL345 is saved in memory. This address is specified in the datasheet as 0x53. We also call out 6 bytes of data in order to save the information from the Accelerometer. The string output will be used to send information to the Serial Port. Finally, in the setup function, we call out the use of the Arduino I2C protocol as well as the Serial protocol.

———-
Accelerometer Arduino I2C
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accel_module);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
}
———-
The above code is used to initialize the Power Control Register. Its address is specified by 0x2D. The first code segment clears the register. The D4 and D3 registers are set to high. This is done in order to set the module into Measure mode and to make sure it is not in sleep mode.

———-
Accelerometer Arduino
void loop(){
int xyzregister = 0x32;
int x, y, z;

Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();

Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);

int i = 0;
while(Wire.available()){
values[i] = Wire.read();
i++;
}
Wire.endTransmission();
———-
The first section of the above code is used to specify the first data register. This information comes from the datasheet. We begin transmission to the module; a request is sent to the address of the module. Each retrieved value is stored in the values register follower by an end of transmission.

———-
x = (((int)values[1]) << 8) | values[0]; y = (((int)values[3])<< 8) | values[2]; z = (((int)values[5]) << 8) | values[4]; sprintf(output, "%d %d %d", x, y, z); Serial.print(output); Serial.write(10); delay(1000); } ---------- Each axis is composed of two bytes of data. We break down the data into the three axis. This data is passed to our string output and sent to the serial port of the Arduino. A delay is used to make sure we do not spam the console.

Output of the Module

The module will now send data about the three axis over the Arduino I2C protocol. You should be able to define a 0 for your particular module; keep in mind that the accelerometer axis will need calibration.

Watch the full video tutorial

Relevant Links