How to use a ESP32 development board to read from an ADXL345 accelerometer

When you want to detect movement of your IOT project, you can use an ADXL345 accelerometer. With this in mind, let's look at how we can use a ESP32 development board to read from an ADXL345 accelerometer.

ESP32 development board connected to ADXL345 accelerometer sensor

Wiring your ADXL345 sensor to your ESP32 development board

When you look at your ADXL345 sensor, you will find several pins: GND, 3v3, CS, INT1, INT2, SDO, SDA and SCL.

In order to read accelerometer values from the ESP32 development board, we need to connect:

  • GND pin of ADXL345 to one of the GND pin on the ESP32 development board
  • 3v3 pin of ADXL345 to one of the 3v3 pin on the ESP32 development board
  • SDA pin of ADXL345 to GPIO21 on the ESP32 development board
  • SCL pin of ADXL345 to GPIO22 pin on the ESP32 development board

ADXL345 sensor with gpio wires connected to gnd 3v3 sda and scl pins

ESP32 development board with gpio wires connected to 3v3 gnd gpio21 amd gpio22 pins

Enabling ESP32 Development on Arduino IDE

Since it is easy to write code and flash programs with Arduino IDE, we can use it to get our program into our ESP32 development board.

In order to use Arduino IDE for this guide, be sure to enable ESP32 development on Arduino IDE before continuing.

Installing the required libraries from Adafruit to read from an ADXL345 accelerometer

After you had started your Arduino IDE, go to Tools -> Manage Libraries.... When you do that, the Library Manager window will appear.

First, install the Adafruit Unified Sensor Driver library. In order to do so, filter the list of libraries with adafruit unified, locate Adafruit Unified Sensor by Adafruit and click on Install:

Mac Arduino IDE 1.8.9 Library Manager with Adafruit Unified Sensor as one of the installation options

Next, install the Adafruit ADXL345 library. In order to do so, filter the list of libraries with adafruit adxl345, locate Adafruit ADXL345 by Adafruit and click on Install:

Mac Arduino IDE 1.8.9 Library Manager with Adafruit adxl345 library as one of the installation options

After you had installed these libraries, you will be able to use them to read from the ADXL345 accelerometer.

Flashing the example code from Adafruit

When you install the Adafruit ADXL345 library, you will find an example that shows you how to use the library.

So where can you find the example to use the ADXL345 accelerometer?

When you navigate to /libraries/Adafruit_ADXL345/examples/sensortest, you should find a file named sensortest.ino:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  accel.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" m/s^2");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void displayDataRate(void)
{
  Serial.print  ("Data Rate:    "); 
  
  switch(accel.getDataRate())
  {
    case ADXL345_DATARATE_3200_HZ:
      Serial.print  ("3200 "); 
      break;
    case ADXL345_DATARATE_1600_HZ:
      Serial.print  ("1600 "); 
      break;
    case ADXL345_DATARATE_800_HZ:
      Serial.print  ("800 "); 
      break;
    case ADXL345_DATARATE_400_HZ:
      Serial.print  ("400 "); 
      break;
    case ADXL345_DATARATE_200_HZ:
      Serial.print  ("200 "); 
      break;
    case ADXL345_DATARATE_100_HZ:
      Serial.print  ("100 "); 
      break;
    case ADXL345_DATARATE_50_HZ:
      Serial.print  ("50 "); 
      break;
    case ADXL345_DATARATE_25_HZ:
      Serial.print  ("25 "); 
      break;
    case ADXL345_DATARATE_12_5_HZ:
      Serial.print  ("12.5 "); 
      break;
    case ADXL345_DATARATE_6_25HZ:
      Serial.print  ("6.25 "); 
      break;
    case ADXL345_DATARATE_3_13_HZ:
      Serial.print  ("3.13 "); 
      break;
    case ADXL345_DATARATE_1_56_HZ:
      Serial.print  ("1.56 "); 
      break;
    case ADXL345_DATARATE_0_78_HZ:
      Serial.print  ("0.78 "); 
      break;
    case ADXL345_DATARATE_0_39_HZ:
      Serial.print  ("0.39 "); 
      break;
    case ADXL345_DATARATE_0_20_HZ:
      Serial.print  ("0.20 "); 
      break;
    case ADXL345_DATARATE_0_10_HZ:
      Serial.print  ("0.10 "); 
      break;
    default:
      Serial.print  ("???? "); 
      break;
  }  
  Serial.println(" Hz");  
}

void displayRange(void)
{
  Serial.print  ("Range:         +/- "); 
  
  switch(accel.getRange())
  {
    case ADXL345_RANGE_16_G:
      Serial.print  ("16 "); 
      break;
    case ADXL345_RANGE_8_G:
      Serial.print  ("8 "); 
      break;
    case ADXL345_RANGE_4_G:
      Serial.print  ("4 "); 
      break;
    case ADXL345_RANGE_2_G:
      Serial.print  ("2 "); 
      break;
    default:
      Serial.print  ("?? "); 
      break;
  }  
  Serial.println(" g");  
}

void setup(void) 
{
#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
  Serial.begin(9600);
  Serial.println("Accelerometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while(1);
  }

  /* Set the range to whatever is appropriate for your project */
  accel.setRange(ADXL345_RANGE_16_G);
  // accel.setRange(ADXL345_RANGE_8_G);
  // accel.setRange(ADXL345_RANGE_4_G);
  // accel.setRange(ADXL345_RANGE_2_G);
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
  
  /* Display additional settings (outside the scope of sensor_t) */
  displayDataRate();
  displayRange();
  Serial.println("");
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  accel.getEvent(&event);
 
  /* Display the results (acceleration is measured in m/s^2) */
  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);
}

Open that file with your Arduino IDE and upload it to your ESP32 development board.

After you upload it you should see similar output in the serial monitor:

Mac Arduino IDE 1.8.9 serial monitor output for Adafruit_ADXL345 example

Writing our own mini example program to read from the ADXL345 accelerometer

Since we have an example from Adafruit, it is easy to write our own program to read from the ADXL345 accelerometer.

Given that in mind, the following is an example sketch to read from the ADXL345 accelerometer:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
 
/*Initialize an instance of Adafruit_ADXL345_Unified with a unique id*/
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {

  Serial.begin(9600);
  Serial.println("");
  Serial.println("Small example to read from ADXL345 accelerometer");
  
}

void loop() {
  
  /*Read from ADXL345 accelerometer*/
  sensors_event_t event; 
  accel.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  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 ");
  /*Take a one second break*/
  delay(1000);

}

When you upload this program to your ESP32 development board, output similar to the following will appear on your Arduino IDE serial monitor:

Mac Arduino 1.8.9 Serial Monitor showing output from own ESP32 reading from ADXL345 sensor

How to use a ESP32 development board to read from an ADXL345 accelerometer

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.