How To Interface ADXL345 Accelerometer With Arduino UNO
How To Interface ADXL345 Accelerometer With Arduino UNO
I n t e r f a c i n g A DX L 3 4 5 A c c e l e r o m e t e r w i t h A r d u i n o
UNO
We all know about accelerometer and gyroscope, they are primarily used to detect
acceleration. While Accelerometer (https://circuitdigest.com/tags/accelerometer)
can measure linear acceleration, the gyroscope can help �nd the rotational
acceleration. A gyroscope (https://circuitdigest.com/microcontroller-projects
/arduino-based-digital-protractor-using-mpu6050-gyroscope) is used to measure
angular velocity that uses the earth’s gravity to determine the orientation of the object
in motion. There is a sensor like MPU6050 which has both accelerometer and
gyroscope and it works as an Inertial Measurement Unit (IMU) to �nd the orientation,
position, and velocity.
1 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
Here we are discussing about ADXL345 Accelerometer which is used to measure the
acceleration or change in velocity in x, y, and z-axis. These small sensors are used in
cars and bikes to detect accidents to deploy the airbags and are also used in mobile
phones for a variety of applications like compass and location tracking.
We used the accelerometer to build various useful applications which you can check at
below links:
1. ADXL335
2. ADXL345
3. ADXL356
2 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
These accelerometer sensors are very popular and apart from these three, there are
many other accelerometer sensors like ADXL354, ADXL355, ADXL372, etc.
C o m p a r i s o n : A DX L 3 3 5 v s A DX L 3 4 5 v s A DX L 3 5 6
A DX L 3 5 6 A DX L 3 4 5 A DX L 3 3 5
Low Low
Power Standard
Typical: Typical:
Consumption Typical: 350µ
150µ 140µ
Among the above three, ADXL345 is easy to interface because of its digital nature. But
its programming is dif�cult as it works on the SPI/I2C protocol. ADXL345 can measure
static and dynamic accelerations and suitable for mobile applications. Also, these
sensors are laboratory calibrated and don’t require any further calibrations.
3 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
Here we will use the Adafruit library for the ADXL345 sensor to interface it with
Arduino Uno.
Components Required
1. Arduino UNO
2. ADXL345 Accelerometer
3. Male-female wires
4. Breadboard
C i r c u i t D i a g ra m
Circuit diagram for ADXL345 accelerometer interface with Arduino is given below:
4 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
(/fullimage?i=circuitdiagram_mic/ADXL345-Accelerometer-Interface-with-Arduino-
Circuit-Diagram.png)
A DX L 3 4 5 A r d u i n o C o n n e c t i o n s :
5 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
A DX L 3 4 5 A r d u i n o C o d e E x p l a n a t i o n
For this ADXL345 Arduino project, we need two libraries for the ADXL345 Sensor.
1. Adafruit ADXL345
2. Adafruit Uni�ed sensor
To download the above libraries, open Arduino IDE and go to Sketch -> Include
Library -> Manage Libraries. Search for Adafruit ADXL345 and install it. Similarly,
search for Adafruit Uni�ed sensor and install.
Now, we are ready to write the code. Example code can be found in Files -> Example
6 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
- > A d a f r u i t A DX L 3 4 5 - > s e n s o r t e s t
1. First, include all the required libraries header �les to support the functionality of the
sensor. Here we are also using wire library for I2C communication.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
2. Make an instance by using any variable like accel to use the various functions of the
ADXL345 Arduino library.
void setup()
{
Serial.begin(9600);
if(!accel.begin())
{
Serial.println("No valid sensor found");
while(1);
}
}
4. In void loop() function, create a variable to use sensors_event_t structure. Use this
variable (in this case “event”) to �ll the structure members using .getevent() function.
Now, print the values of acceleration in x,y, z-axis using event.acceleration.x()
function.
7 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
void loop()
{
sensors_event_t event;
accel.getEvent(&event);
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("");
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("");
Serial.println("m/s^2 ");
delay(500);
}
Complete code with a Demonstration video is given at the end of this article.
Try to move the sensor slowly in all the directions and observe the readings.
8 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
So this is how an Accelerometer can be used with Arduino UNO to detect the
variations in x, y, and z-axis.
Code
9 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
void setup(void)
Serial.begin(9600);
if(!accel.begin())
Vi deo
Tags
Accelerometer (/Tags/Accelerometer)
H a v e a ny q u e s t i o n r e a l a t e d t o t h i s A r t i c l e ?
(Https://Chat.Whatsapp.Com/JR4e0Fc0V20H6jTMDd1daT)
TELEGRAM DISCORD
(Https://T.Me/Circuitdigest) (Https://Discord.Com/Invite/UXJrFJSWpz)
10 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
FORUM
(Https://Circuitdigest.Com/Forums)
Log in (/user/login?destination=/microcontroller-projects/interface-adxl345-
accelerometer-with-arduino-uno%23comment-form) or register (/user
/register?destination=/microcontroller-projects/interface-adxl345-accelerometer-
with-arduino-uno%23comment-form) to post comments
(https://bit.ly/48v2Jrq
)
(https://bit.ly/3IXzC60
)
(https://bit.ly/3UUJjcl )
11 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
(https://bit.ly/4bNoPIx
)
(https://bit.ly/3VUjEkP
)
(https://bit.ly/3PJS99t
)
(https://bit.ly/3IbP7qe
)
12 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
(https://bit.ly/3IU6r3o
)
13 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
(https://www.facebook.com/circuitdigest/) (https://twitter.com
/CircuitDigest) (https://www.youtube.com/channel
/UCy3CUAIYgZdAOG9k3IPdLmw) (https://www.linkedin.com/company
/circuit-digest/)
COMPANY
Advertise (/advertise)
14 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
P RO J E C T
(/microcontroller-projects)
OUR NETWORK
(https://circuitdigest.com)
(https://components101.com)
15 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...
(https://iotdesignpro.com)
16 of 16 28/04/2024, 17:21