Curious about smart parking systems engineering? Click here to learn more !

ADXL345 Module Communication with Arduino UNO Board

Posted by Momen 18/06/2017 0 Comment(s)

 

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.li8uiEoa.dpuf

 

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.li8uiEoa.dpuf

 

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.li8uiEoa.dpuf

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.li8uiEoa.dpuf

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.li8uiEoa.dpuf

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

 

Our Arduino training series:

1. Start with Arduino - Zeroohm Arduino Training

2. Start with Arduino - Arduino with Motors and sensors

3. Start with Arduino - Wireless communication

4. How to setup Arduino with room temperature monitoring using LM36 sensor

5. Introduction to IoT using Arduino

5. Introduction to IoT using Arduino

6. IoT: Humidity and Temperature Sensor

7. ADXL345 Module Communication with Arduino UNO Board

 

In this tutorial, I will try to explain how to communicate between a ADXL345 module which is a 3-axis digital accelerometer well suited for measuring static acceleration of gravity in tilt-sensing applications as well as dynamic acceleration which results from motion or shock with a famous Arduino board known as Arduino UNO. I got my module and my Arduino board from Zero Ohm Electronics which is a Dubai based company specialising in selling electronic parts. All the supporting hardware in this tutorial are provided by them as well. Below is a list of all hardware components used in this tutorial.

Hardware Components:

Hardware Design:

Let’s start by the hardware connection of UNO and ADXL345. This module will be connected to UNO which is powered by ATmega328P microcontroller via a breadboard. Follow the steps below for hardware connections:

Step 1- Power up your UNO by connecting it to your PC.

Step 2- Complete the hardware connection as shown in the pin configuration table.

Arduino UNO

ADXL345

5V

VCC

GND

GND

A4

SDA + resistor in parallel

A5

SCL + resistor in parallel

NOTE: The ADXL345 module can take up to 5V and regulate it to 3.3V.

Your hardware connection should look something like below:

arduino-uno-connection-with-adxl345

Software Design:

Step 1- Do the basic settings for setting up the UNO with your PC.

NOTE: I have assumed that you are aware how to install the drivers for your Arduino Boards.

Before we head to the code, you should know that there are two type of communication protocols for ADXL345: I2C and SP1. In this tutorial, I have used I2C.

i2c-communcation

NOTE: I have also assumed you understand the technical background behind I2C protocol.

Now on to the code!

Step 2- First we need to include some libraries, define some things and initialize the accelerometer.

#include <Wire.h> //I²C protocol library

#define accel_module (0x53) //Address of ADXL345

#define xyzregister (0x32) //Starting with initial value of x-axis

byte values[6]; //2 bytes for each axis

char output[512];//String output for serial monitor

void setup()

{

  Wire.begin();

  Serial.begin(9600);//Baud transmission rate

  Wire.beginTransmission(accel_module);

  Wire.write(0x2D); //POWER_CTL Register

  Wire.write(0);// Wake up

  Wire.endTransmission();

  Wire.beginTransmission(accel_module);

  Wire.write(0x2D);

  Wire.write(16);//Auto Sleep

  Wire.endTransmission();

  Wire.beginTransmission(accel_module);

  Wire.write(0x2D);

  Wire.write(8);//Measure

  Wire.endTransmission();

  Wire.beginTransmission(accel_module);

  Wire.write(0x31); //DATA_FORMAT Register

  Wire.write(0x09); //±4 g range in full resolution (Can be changed)

  Wire.endTransmission();

}

Comment: As in the code, the accel_module (0x53) address is the universal address on I2C bus for accelerometer module. The 0x2D address is to access the power control register of accelerometer and the Wire.write(0), Wire.write(16) & Wire.write(8) commands to set the accelerometer to Wake up, Auto sleep and Measure. This will prevent power wastage as the accelerometer won’t be awake all the time. The Wire.write(0x31) command is to access the DATA_FORMAT Register where we set the sensitivity to +/-4g range by writing Wire.write(0x09) command. After initializing is complete, we end Transmission.

NOTE: The sensitivity range of ADXL345 can range from +/-2g to +/-16g. I’m assuming you have had a look with the module’s datasheet and are aware of its feature and specifications.

Step 3- Reading values from ADXL345.

void loop() {

      int x, y, z;

      Wire.beginTransmission(accel_module);//Beginning of reading values

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

      x= ((int)values[1] <<8) | values[0];// The most significant byte is shifted to the left

      y= ((int)values[3] <<8) | values[2];

      z= ((int)values[5] <<8) | values[4];

      sprintf(output, "%d %d %d", x, y, z);//Printing values on Serial Monitor

      Serial.print(output);

      Serial.write(10);

      delay(1000);

}

Comment: After the module starts getting the readings, it stores it in an array named values[] which has a size of 6 bytes, with each axis allowed to have 2 bytes. Since we are using I2C communication, the first 2 bytes will belong to x-axis and so on. The xyzregister is used for the prime reason of shifting the most significant byte to the left as the least significant byte comes in, this is done for each axis.

Step 4- Open Serial Monitor to see the values of x, y and z. The table which you see in the serial monitor is not necessarily aligned with the axes, it needs to calibrated.

adxl345-serial-monitor-values

Step 5- You can play around with the values by tilting and shaking the ADXL345 module. And that’s it we are done!

Signing Off Note: You can use ADXL345 for a number of applications which include robots such as self- balancing robot on a single wheel where the module will measure the tilt the robot falls and the UNO will correct the robot’s position accordingly. One more good application using this module can be servo control in quadcopters where the module will measure the tilt and shocks received by the copter and the UNO will guide the servos accordingly.

Since I have nothing to add anymore, I’ll say my goodbyes and I hope this tutorial was useful for you and you enjoyed it!

 

 

 

 

Leave a Comment