» » » ADXL345 Connection Guide with ESP8266 Board

ADXL345 Connection Guide with ESP8266 Board

posted in: Others | 0

The small, thin still packed with powerful performance, the ADXL345 is a 3-axis accelerometer sensor with the resolution of 13-bit measurement at up to +/-16g. The Output data is formatted as 16-bit twos complement and is accessible through the I2C digital interface.
In this connection guide, I will show you how to connect this amazing device with the ESP8266 board. The ESP8266 is an incredible platform for Home Automation and IoT projects. It is packed with Wi-Fi chip with a full TCP/IP protocol making it one of the important and ever growing device.


Step 1: Hardware We Need

1. ADXL345 I2C Mini Module

ADXL345 Sensor
ADXL345 Accelerometer Sensor

First of all, we need an ADXL345 Sensor. The sensor is packed with high resolution (4.0 mg/LSB) which enables the determination of very small inclination changes less than 1.0 degree. The I²C Mini Modules are designed to operate at 5VDC. Using a convenient 4-Pin plug, devices can be daisy-chained onto the I²C Bus, eliminating the need for soldering.

 

2. Adafruit Huzzah ESP8266

ESP8266
Adafruit Huzzah ESP8266

Now, we need an Adafruit Huzzah ESP8266 board. Adafruit Huzzah ESP8266 is a low-cost Wi-Fi chip with full a TCP/IP stack and microcontroller capability.The ESP8266 provides a mature platform for monitoring and control applications using the Arduino Wire Language and the Arduino IDE. The ESP8266 module is an extremely cost effective board with a huge, and ever growing community.

 

3. Adafruit Huzzah ESP8266 Host Adapter (USB Programmer)

ESP8266 USB Programmer
ESP8266 USB Programmer

This ESP8266 host adapter was designed specifically for the Adafruit Huzzah version of the ESP8266, providing an I²C interface. The integrated USB port supplies power and programming to the ESP8266.

 

4. I2C Connecting Cable

I2C Cable
I2C Connecting Cable

These are I2C Connecting Cable which is designed specifically for I2C communication. These cables are available at:

 

5. Mini USB cable
The mini USB cable Power supply is an ideal choice for powering the Adafruit Huzzah ESP8266.


Step 2: Hardware Connections

In general, the connections are very simple. Follow the instructions and images below, and you should have no difficulties.

Connection of the Adafruit Huzzah ESP8266 and USB Programmer
First of all, take the Adafruit Huzzah ESP8266 and place it on the USB Programmer (with Inward Facing I²C Port). Press the ESP8266 gently into the USB Programmer and we are done with this step (See the picture below).

ESP8266 and USB Programmer Conn
ESP8266 and USB Programmer Conn

 

Connection of the Sensor and Adafruit Huzzah ESP8266
Take the sensor and connect the I²C Cable to it. For proper operation of this cable, please remember I²C Output ALWAYS connects to the I²C Input. The same should be done for the Adafruit Huzzah ESP8266 with the USB Programmer mounted over it (See the picture below).

ADXL345 ESP8266 Conn
ADXL345 ESP8266 Connections

 

With the help of the ESP8266 USB Programmer, it is very easy to program the ESP8266. All you need to do is plug the sensor into the USB Programmer and you are good to go. I prefer to use this adapter because it makes it a lot easier to connect the hardware. Without this plug and play USB Programmer, there would be a huge risk of making a wrong connection as one wrong wire can kill your wifi as well as your sensor.

Note: The brown wire should always follow the Ground (GND) connection between the output of one device and the input of another device.

Powering of the Circuit
Plug in the Mini USB cable into the power jack of Adafruit Huzzah ESP8266. Light it up and voila, we are good to go!

The final assembly will look like this:

Final Connections with ESP8266
Final Connections

Step 3: Getting the Adafruit Huzzah ESP8266 Arduino Code

The ESP code for the Adafruit Huzzah ESP8266 and ADXL345 Sensor is available on our Control Everything GitHub repository. Here, you can discover the most state-of-the-art codes of any sensor on different platforms.

Before proceeding on to the code, make sure to read the instructions given in the Readme file and setup your Adafruit Huzzah ESP8266 accordingly. It will just take a moment to do so.

Note: Before uploading, make sure you enter your SSID network and password in the code.

You can copy the working ESP code for this sensor from here also:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>

// ADXL345 I2C address is 0x53(83)
#define Addr 0x53

const char* ssid = "your ssid network";
const char* password = "your password";
float xAccl, yAccl, zAccl;

ESP8266WebServer server(80);

void handleroot()
{
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select bandwidth rate register
  Wire.write(0x2C);
  // Normal mode, Output data rate = 100 Hz
  Wire.write(0x0A);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select power control register
  Wire.write(0x2D);
  // Auto-sleep disable
  Wire.write(0x08);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select data format register
  Wire.write(0x31);
  // Self test disabled, 4-wire interface, Full resolution, Range = +/-2g
  Wire.write(0x08);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  for (int i = 0; i < 6; i++)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((50 + i));
    // Stop I2C transmission
    Wire.endTransmission();

    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);

    // Read 6 bytes of data
    // xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
    if (Wire.available() == 1)
    {
      data[i] = Wire.read();
    }
  }

  // Convert the data to 10-bits
  int xAccl = (((data[1] & 0x03) * 256) + data[0]);
  if (xAccl > 511)
  {
    xAccl -= 1024;
  }
  int yAccl = (((data[3] & 0x03) * 256) + data[2]);
  if (yAccl > 511)
  {
    yAccl -= 1024;
  }
  int zAccl = (((data[5] & 0x03) * 256) + data[4]);
  if (zAccl > 511)
  {
    zAccl -= 1024;
  }

  // Output data to serial monitor
  Serial.print("Acceleration in X-Axis : ");
  Serial.println(xAccl);
  Serial.print("Acceleration in Y-Axis : ");
  Serial.println(yAccl);
  Serial.print("Acceleration in Z-Axis : ");
  Serial.println(zAccl);
  delay(300);

  // Output data to web server
  server.sendContent
  ("<html><head><meta http-equiv='refresh' content='3'</meta>"
   "<h1 style=text-align:center;font-size:300%;color:blue;font-family:britannic bold;>CONTROL EVERYTHING</h1>"
   "<h3 style=text-align:center;font-family:courier new;><a href=http://www.controleverything.com/ target=_blank>www.controleverything.com</a></h3><hr>"
   "<h2 style=text-align:center;font-family:tahoma;><a href=  https://www.controleverything.com/content/Accelorometer?sku=ADXL345_I2CS#tabs-0-product_tabset-2/ \n"
   "target=_blank>AXDL345 Sensor I2C Mini Module</a></h2>");
  server.sendContent
  ("<h3 style=text-align:center;font-family:tahoma;>Acceleration in X-Axis : " + String(xAccl));
  server.sendContent
  ("<h3 style=text-align:center;font-family:tahoma;>Acceleration in Y-Axis : " + String(yAccl));
  server.sendContent
  ("<h3 style=text-align:center;font-family:tahoma;>Acceleration in Z-Axis : " + String(zAccl));
}

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin(2, 14);
  // Initialise serial communication, set baud rate = 115200
  Serial.begin(115200);

  // Connect to WiFi network
  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);

  // Get the IP address of ESP8266
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Start the server
  server.on("/", handleroot);
  server.begin();
  Serial.println("HTTP server started");
}

void loop()
{
  server.handleClient();
}

Step 4: Practicality of the Code

Download (git pull) or copy the code and open it in the Arduino IDE.

Compile and Upload the code and see the output on your Serial Monitor. After few seconds, it will display all the parameters. The output of the sensor on Serial Monitor is shown below.

Copy the IP address of ESP8266 from the Serial Monitor and paste it in your web browser. You will see a web page with acceleration reading in all the 3-axis. The output of the sensor on Web Server displayed as follows.


Step 5: Applications and Features

The ADXL345 Accelerometer I2C Mini Module has important features like Activity/Inactivity Monitoring and Free-Fall Detection. There are many conditions and activities for which a quick warning to a likely fall, particularly from a large height, would be entirely useful – for example, mountain climbers, construction workers, window washers, painters, and roofers. In such cases, ADXL345 Acceleration Sensor can be very useful for making Fall Detecting Devices.

Some of the more applications include smartphones, cameras, video game devices, airplanes, rockets, building and structural monitoring and much more. With the assistance of Adafruit Huzzah ESP8266, you can convert your simple project into an IoT project.

The Adafruit Huzzah ESP8266 board is incredibly versatile, cheap and accessible to all tech geeks like me. You can collect and manage the data over the Internet or control our Home Appliances over a Wi-Fi Network. You can use them in Home Automation, Sensor Networks, Wearable Electronics or WiFi Location-aware Devices and much more like this.

Leave a Reply