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.
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.
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
#define accel_module (0x53)
byte values ;
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.
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.
int i = 0;
values[i] = Wire.read();
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) << 8) | values; y = (((int)values)<< 8) | values; z = (((int)values) << 8) | values; 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.