How to change/increase data rate of ADXL345?

Hello everyone, I am currently working on a project in which ADXL345 is connected with ESP32. When I select baud rate as 115200 in Arduino IDE i get around 500 samples per second, in case baud rate is 57600 i get around 250 samples. But it does not vary if i change the data rate of adxl345 with the help of code included in its library i.e samples remain same even if data rate is changed from 50Hz to 3200Hz (datasheet). Can someone identify the problem?
Also, even if i increase baud rate above 115200, the samples per second are set to 500 i.e it does not increase further.

Well we would need to see your code first.

However my guess is that you are reading the data and then printing it out when you read it. This makes a delay in the loop using it and also fills the print buffer causing more delays.

So the way to measure the sample rate would be to measure the time it takes to put say 500 samples into an array. Then you can print it out from the array to see if the samples are correct.

Hello Mike, here is the code

#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();
accel.setDataRate(ADXL345_DATARATE_3200_HZ);// not sure about this line as it was added by me similar to accel.setRange command
/
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(event.acceleration.x); Serial.print(" ");
Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print(event.acceleration.z); Serial.println(" ");

}

I am using coolterm software to read data from port for one minute and then dividing by 60 to get samples per sec. Readings look something like this
0.31 -0.04 11.02
But the problem is that when setting datarate with the help of command accel.setDataRate(ADXL345_DATARATE_3200_HZ) does not produce any changes even if datarate is changed from 50Hz to 3200Hz i.e maximum that i could achieve is 500 samples per sec

Yes like I said you are getting one sample and printing it out. So you need to fix that.

Also I can't see in that loop function where you do any actual timing. You do this by making a note of the millis() value in a variable before you enter the loop to read 500 samples and then when this loop is finished you subtract the saved millis() value from the current millis() value, to get the time for extracting three values. Then print out your results to see if they look right.

By the way, look at the code you posted. You will see that it goes into italics in one part. This is because the forum software is mistaking part of your code as formatting information. You should have got a warning about this when you posted but I guess you just clicked on a "post anyway" link.

You might want to look at this How to get the best from this from this Forum before you proceed any further. Because it tells you things like how to post code here. Best if you edit that last post and replace the code you posted. The simplest way is to simply use the "copy for forum" option in the IDE when you copy and then you can just paste it into your post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.