ADXL345 Data Rate seems to depend more upon Baud rate than the actual Data Rate that I set I have an Arduino UNO that is connected to an ADXL345. The problem is I need to collect data at 50Hz at least. The ADXL345 according to its Data sheet can go up to 3200Hz without any problem. I am using the Adafruit library using I2C interface but the accelerometer does not go above 10-12Hz as reported by the getDataRate() function of Adafruit library.

Im trying to set the data rate like this:

accel.setDataRate(ADXL345_DATARATE_100_HZ);


The strange thing is that the data rate is depended more upon increasing the baud rate at the beginning of my setup function. If I set this to 38400 I get around 90-92 readings every second. I have setup my code in a way that every measurement from the accelerometer is recorded with the timestamp down to seconds. This is a snapshot of the output from the serial monitor.

Data Rate:    9 Hz
------------------------------------
Sensor:       ADXL345
Driver Ver:   1
Unique ID:    12345
Max Value:    -156.91 m/s^2
Min Value:    156.91 m/s^2
Resolution:   0.04 m/s^2
------------------------------------

Range:         +/- 8  g


84.00,18:40:4,15/7/2018,0.59,9.14,2.82
85.00,18:40:4,15/7/2018,0.59,9.14,2.86
86.00,18:40:4,15/7/2018,0.63,9.10,2.86
87.00,18:40:4,15/7/2018,0.55,9.10,2.82
88.00,18:40:4,15/7/2018,0.55,9.10,2.82
89.00,18:40:4,15/7/2018,0.59,9.10,2.82
90.00,18:40:4,15/7/2018,0.59,9.10,2.82
91.00,18:40:4,15/7/2018,0.55,9.10,2.86
92.00,18:40:4,15/7/2018,0.55,9.10,2.86
93.00,18:40:4,15/7/2018,0.63,9.10,2.90
94.00,18:40:4,15/7/2018,0.63,9.10,2.90
95.00,18:40:4,15/7/2018,0.63,9.06,2.82
96.00,18:40:4,15/7/2018,0.63,9.06,2.82
97.00,18:40:4,15/7/2018,0.59,9.06,2.82
98.00,18:40:4,15/7/2018,0.59,9.06,2.82
99.00,18:40:4,15/7/2018,0.59,9.10,2.82


Is it a problem with the code, or the adxl345?
You do know that sending data through serial takes time, don't you?

You are sending 41 bytes per reading. The serial port has a 64 byte buffer. By the second reading the buffer is full and all printing blocks until there is room in the buffer.

At 38400 baud that's sending 3840 bytes per second. With 41 bytes per reading that's 93.66 readings per second (not including the time taken to do the actual reading) that you can squeeze down the serial port.

Either run at a faster speed, or reduce the quantity of data you're sending per reading - or do both.