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:


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


Is it a problem with the code, or the adxl345?

1 Answer 1


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.

  • Sure I increased the baud rate now to 115200 without changing the data rate of ADXL345, now I get around 280-290 readings on my Serial Monitor per second which means that this bottleneck has been removed?, but getDataRate still shows 9Hz when it is actually set to 100Hz.
    – m.umar
    Jul 24, 2018 at 10:00
  • Also why am I getting 280-290 readings when they should be somewhere around 100 readings per second?
    – m.umar
    Jul 24, 2018 at 10:01
  • How often you read has no bearing on how often the ADXL samples. You just get multiple readings the same.
    – Majenko
    Jul 24, 2018 at 10:02
  • 1
    The value returned by getDataRate isn't a Hz value - it's a number that equates to one of the ADXL_DATARATE_x_HZ enumerations. github.com/adafruit/Adafruit_ADXL345/blob/master/…
    – Majenko
    Jul 24, 2018 at 10:04
  • Oh I got it, thanks :) Just one last query though, I need to collect data at 50Hz for an activity tracking machine learning algorithm, which from my understanding meant for every second 50 values in the x,y,z axes have to be collected. If I adjust baud rate so that I get 'around' 50 values every second in the serial monitor, does that mean the values line up with the actual Data Rate of the accelerometer?
    – m.umar
    Jul 24, 2018 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.