How To Interface ADXL345 Accelerometer With Arduino UNO

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...

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

Published January 17, 2020 0

Rishabh Jain (/users/rishabh-jain)


R
Author

Interfacing ADXL345 Accelerometer with Arduino 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:

• Vehicle Accident Alert System (https://circuitdigest.com/microcontroller-projects


/arduino-based-accident-alert-system-using-gps-gsm-accelerometer)
• Hand Gesture Controlled Robot using Arduino (https://circuitdigest.com
/microcontroller-projects/accelerometer-based-hand-gesture-controlled-robot-
using-arduino)
• Earthquake Detector Alarm (https://circuitdigest.com/microcontroller-projects
/arduino-earthquake-detector-alarm-circuit)
• Ping Pong Game (https://circuitdigest.com/microcontroller-projects/ping-pong-
game-using-arduino-accelerometer)

You can see all the accelerometer-based projects here (https://circuitdigest.com


/tags/accelerometer).

Types of Accelerometer Sensors


There are many types of MEMS accelerometer sensors available in the market. They
can be classi�ed on the basis of precision, power consumption, and interfacing. All
these sensors are portable and can be �tted in any kind of device like wearables. These
sensors measure acceleration in 3-axis (x,y,z).

Some widely used Sensors are:

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.

Let’s see the difference between these sensors.

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

±10g to ±3g, small range with precise


Range ±16g
±40g readings

Interface Analog SPI, I2C Analog

Low Low
Power Standard
Typical: Typical:
Consumption Typical: 350µ
150µ 140µ

Pricing High Low Lowest

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 :

• Connect A4 pin (SDA) of Arduino -> SDA pin of adxl345


• Connect A5 pin (SCL) of Arduino -> SCL pin of adxl345
• Connect GND of Arduino -> GND pin of adxl345
• Connect 5V of Arduino -> Vcc of adxl345

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.

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

3. In void setup() function, initialize serial communication to print data on a serial


monitor using Serial.begin() function. Now, check whether the valid ADXL sensor is
connected or not. We will check the result returned by accel.begin() function if it
returns false then print a message that no valid sensor found.

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.

Testing ADXL345 accelerometer Arduino Interfacing


Finally, connect the ADXL345 sensor with Arduino UNO properly and upload the code
in the Arduino Uno board. Then open Serial monitor and you will see acceleration
readings in x, y, z-axis as shown below.

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.

Complete ADXL345 Arduino Code with a video that is given below.

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>

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

void setup(void)

Serial.begin(9600);

if(!accel.begin())
Vi deo

Tags

Arduino (/Tags/Arduino) Arduino Uno (/Tags/Arduino-Uno)

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 ?

Ask Our Community Members

WHATSAPP

(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

S-82L1/T1/U1/V1 Series 1-Cell Battery Protection IC


(https://bit.ly/48v2Jrq )
ABLIC's battery protection IC supports safe, highly ef�cient fast-charging

(https://bit.ly/48v2Jrq
)

WISE-750-02A1E Intelligent Vibration Sensing Gateway


(https://bit.ly/3IXzC60 )
Advantech's WISE-750 vibration PHM gateway is developed to perform predictive
maintenance.

(https://bit.ly/3IXzC60
)

ACS72981 Current Sensor IC Series (https://bit.ly/3UUJjcl )


ACS72981 family of current sensor ICs provides economical and precise AC or DC
sensing solutions.

(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...

Conan® Lite 1.00 mm Connectors (https://bit.ly/4bNoPIx )


Conan® lite 1.00 mm connectors feature an audible click to ensure perfect mating

(https://bit.ly/4bNoPIx
)

Embedded and Microcontrollers (https://bit.ly/3VUjEkP )


Master evaluation boards, microprocessors and other expertise at DigiKey!

(https://bit.ly/3VUjEkP
)

MCE Series AC/DC Power Supplies (https://bit.ly/3PJS99t )


3W-40W PCB mount Medical AC-DC Power Supplies for Class II and BF-rated
medical applications

(https://bit.ly/3PJS99t
)

PolarFire® SoC FPGAs (https://bit.ly/3IbP7qe )


Microchip's PolarFire is a low-power, multi-core RISC-V SoC FPGA

(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...

Automotive PCB Mount Relay - EP1/EP2 Series (https://bit.ly


/3IU6r3o )
KEMET's automotive PCB-mount relays unique structure offers high performance
and productivity

(https://bit.ly/3IU6r3o
)

Join 100K+ Subscribers


Your email is safe with us, we don’t spam.

Type your email address Subscribe

Be a part of our ever growing community.

Semicon Media is a unique collection of online media, focused purely on the


Electronics Community across the globe. With a perfectly blended team of

13 of 16 28/04/2024, 17:21
How to Interface ADXL345 Accelerometer with Arduino UNO https://circuitdigest.com/microcontroller-projects/interface-adxl345-a...

Engineers and Journalists, we demystify electronics and its related


technologies by providing high value content to our readers.

(https://www.facebook.com/circuitdigest/) (https://twitter.com

/CircuitDigest) (https://www.youtube.com/channel

/UCy3CUAIYgZdAOG9k3IPdLmw) (https://www.linkedin.com/company

/circuit-digest/)

COMPANY

Privacy Policy (/privacy-policy) Cookie Policy (/cookie-policy)

Terms of Use (/terms-of-use) Contact Us (/contact)

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

555 Timer Circuits (/555-timer-circuits) Op-amp Circuits (/op-

amp-circuits) Audio Circuits (/audio-circuits) Power Supply

Circuits (/smps-power-supply-circuits) Arduino Projects

(/arduino-projects) Raspberry Pi Projects (/simple-raspberry-pi-

projects-for-beginners) MSP430 Projects (/msp430-projects)

STM32 Projects (/stm32-projects-and-tutorials) ESP8266

Projects (/esp8266-projects) PIC Projects (/pic-microcontroller-

projects) AVR Projects (/avr-microcontroller-projects) 8051

Projects (/8051-microcontroller-projects) ESP32 Projects

(/esp32-projects) IoT Projects (/internet-of-things-iot-projects)

PCB Projects (/diy-pcb-projects) Arduino ESP8266 Projects

(/arduino-esp8266-projects) All Microcontroller Projects

(/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)

Copyright © 2023 Circuit Digest (/). All rights reserved.

16 of 16 28/04/2024, 17:21

You might also like