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.
ESP32 Devkit V1
ADXL345 (Sensor 1)
ADXL345 (Sensor 2)
/*
* 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.