please help me because my adxl345 sensor is not working properly

Maybe someone has experienced a problem like me, the adxl345 sensor can’t read properly.

This is my sketch

#include <Wire.h>

#define DEVICE (0x53) //inisialisasi ADXL345
#define TO_READ (6)

byte buff[TO_READ] ;
char str[512];

void setup()
{
Wire.begin(); // inisialisai komunikasi I2C
Serial.begin(9600); // memulai serial keluaran

//mengaktifkan ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
}

void loop()
{
int regAddress = 0x32; //intruksi pembacaan
int z;

readFrom(DEVICE, regAddress, TO_READ, buff); //membaca nilai akselerasi dari ADXL345

//membaca akselerasi pada sumbu z

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

//mengirim nilai z sebagai string ke port serial
sprintf(str, “%d”, z);
Serial.print(str);
// Serial.print(10, BYTE);
Serial.println();

//memberikan delay 50 ms
delay(50);
}

//---------------- Functions
//mengirim nilai ke alamat register pada device
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //memulai pengiriman ke device
Wire.write(address); // mengirim alamt register
Wire.write(val); // mengirim nilai
Wire.endTransmission(); //mengakhiri pengiriman
}

//membaca jumlah bytes dimulai dari alamat register pada device dalam array
void readFrom(int device, byte address, int num, byte buff) {
Wire.beginTransmission(device); //memulai pengiriman ke device
Wire.write(address); //nds address to read from Wire.endTransmission(); //mengakhiri pengiriman
Wire.beginTransmission(device); //memulai pengiriman pada device
Wire.requestFrom(device, num);

int i = 0;
while(Wire.available())
{
buff = Wire.read(); // menerima byte

YOSIHERVINDO:
//mengaktifkan ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);

Very cool code :wink: Please read "How to use the forum" and edit your post accordingly.