Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Dual ADXL345 Accelerometer Data Logger with I2C Communication

Image of ESP32-Based Dual ADXL345 Accelerometer Data Logger with I2C Communication

Circuit Documentation

Summary

This circuit involves an ESP32 Devkit V1 microcontroller interfaced with two ADXL345 accelerometer sensors. The ESP32 reads acceleration data from both sensors using I2C communication and prints the data to the Serial Monitor. The circuit is designed to read data from both sensors simultaneously.

Component List

  1. ESP32 Devkit V1

    • Description: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
    • Pins: 3V3, GND, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, VIN
  2. ADXL345 (Sensor 1)

    • Description: A 3-axis accelerometer sensor.
    • Pins: GND, VCC, CS, INT1, INT2, SDO, SDA, SCL
  3. ADXL345 (Sensor 2)

    • Description: A 3-axis accelerometer sensor.
    • Pins: GND, VCC, CS, INT1, INT2, SDO, SDA, SCL

Wiring Details

ESP32 Devkit V1

  • 3V3: Connected to VCC of both ADXL345 sensors.
  • GND: Connected to GND of both ADXL345 sensors.
  • D15: Connected to SDO of ADXL345 (Sensor 2).
  • D18: Connected to SCL of ADXL345 (Sensor 2).
  • D21: Connected to SDA of ADXL345 (Sensor 1).
  • D22: Connected to SCL of ADXL345 (Sensor 1).
  • D23: Connected to SDA of ADXL345 (Sensor 2).

ADXL345 (Sensor 1)

  • VCC: Connected to 3V3 of ESP32.
  • GND: Connected to GND of ESP32.
  • SDA: Connected to D21 of ESP32.
  • SCL: Connected to D22 of ESP32.

ADXL345 (Sensor 2)

  • VCC: Connected to 3V3 of ESP32.
  • GND: Connected to GND of ESP32.
  • SDO: Connected to D15 of ESP32.
  • SDA: Connected to D23 of ESP32.
  • SCL: Connected to D18 of ESP32.

Code Documentation

/*
 * This Arduino Sketch reads acceleration data from two ADXL345 sensors
 * connected to an ESP32 Devkit V1 using the SparkFun_ADXL345 library.
 * The data from both sensors is read simultaneously and printed to the
 * Serial Monitor.
 */

#include <Wire.h>
#include <SparkFun_ADXL345.h>

// Create TwoWire instances for each I2C bus
TwoWire I2C_1 = TwoWire(0);
TwoWire I2C_2 = TwoWire(1);

ADXL345 adxl1 = ADXL345(); // Create an instance for the first ADXL345
ADXL345 adxl2 = ADXL345(); // Create an instance for the second ADXL345

// ESP32 I2C pins
#define SDA_1 21
#define SCL_1 22
#define SDA_2 23
#define SCL_2 18

//Tingkat akurasi sensor (2G,4G,8G,16G)
int range = 2;

void setup() {
  Serial.begin(115200);
  I2C_1.begin(SDA_1, SCL_1,100000);
  I2C_2.begin(SDA_2, SCL_2,100000);
  pinMode(15,OUTPUT);//untuk menyeting alamat address jadi 0x1D sesuai datasheet adxl345
  digitalWrite(15,HIGH);//untuk menyeting alamat address jadi 0x1D sesuai datasheet adxl345
  bool status1 = adxl1.begin(0x53,&I2C_1);
  if (!status1){
    Serial.println("Could not find a valid ADXL345 sensor, check wiring");
    while(1);
  }
  bool status2 = adxl2.begin(0x1D,&I2C_2);
  if (!status2){
    Serial.println("Could not find a valid ADXL345 sensor, check wiring");
    while(1);
  }
  Serial.println();
  adxl1.powerOn();
  adxl2.powerOn();
  adxl1.setRangeSetting(range);
  adxl2.setRangeSetting(range);
}

void loop() {
  int x1, y1, z1, x2, y2, z2;
  adxl1.readAccel(&x1, &y1, &z1);
  adxl2.readAccel(&x2, &y2, &z2);
  //Kalibrasi ADXL345 1
  x1 = x1*3.9;
  y1 = y1*3.9;
  z1 = z1*3.9;
  //Kalibrasi ADXL345 2
  x2 = x2*3.9;
  y2 = y2*3.9;
  z2 = z2*3.9;
  Serial.print("Sensor 1: ");
  Serial.print("X: "); Serial.print(x1);
  Serial.print(" Y: "); Serial.print(y1);
  Serial.print(" Z: "); Serial.println(z1);
  Serial.print("Sensor 2: ");
  Serial.print("X: "); Serial.print(x2);
  Serial.print(" Y: "); Serial.print(y2);
  Serial.print(" Z: "); Serial.println(z2);
  delay(10);
}

This code initializes the ESP32 and the two ADXL345 sensors, sets up the I2C communication, and continuously reads and prints the acceleration data from both sensors. The data is calibrated by multiplying the raw values by 3.9 to convert them to a more meaningful unit.