I am using ADXL345 accelerometer with Adafruit_ADXL345 library for vibration measurement.

I am reading acceleration at X, Y, Z-axis of a sensor placed on a vibrating platform.And serially sending it to PC at a baud rate of 115200.

When vibration is low sensor shows vibrations but as i increase the vibrations serial transmission seems to be halted. As shown in figure:(x-axis=Samples y-axis=Values )Serial Plot Data

I thought it may be due to loose connections so I soldered its terminals but it behaved the same way at higher vibrations. Here is the code that I’m using.

#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(12);
int i=0;
struct measure
    int timestamp;
    float x;  // ACC in x direction
    float y;  //ACC in y direction
    float z;  //ACC in z direction
sensors_event_t event; 
struct measure measurements;

int timestamp = 0;

void setup(){

  Serial.println("Accelerometer Test"); Serial.println("");

  /* Initialise the sensor */
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
     pinMode(LED_BUILTIN, OUTPUT);

void loop(){

   measurements.timestamp = timestamp++;
   measurements.x  = event.acceleration.x;
   measurements.y  = event.acceleration.y;
   measurements.z  = event.acceleration.z;

  digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(LED_BUILTIN, LOW);  

  /* Sending the array with data through serial*/ 


Where does problem lie? How to resolve this issue?

Circuit Connections: Circuit

  • After a quick look at the Adafruit code I'd expect it to return zero or junk even if the ADXL stopped working. Is the Arduino subject to vibration as well? I'd try removing the accelerometer code and just transmit 'ABC' or something and see if it still stops due to a hardware problem.
    – PeterJ
    Aug 19, 2017 at 10:17
  • If i remove the accelerometer code and transmit anything, it prints perfectly without any problem (with or without subjecting arduino to vibration) Aug 19, 2017 at 11:46
  • I smell internal accelerometer problem. Internally they do a lots of approximations to produce the result - if acceleration changes too quickly probably they stop to work.
    – PeterJ_01
    Aug 19, 2017 at 21:19
  • 1
    Maybe the vibration is shaking your wires loose and breaking communication...
    – Majenko
    Aug 22, 2017 at 18:14
  • 1
    @MasoodSalik - (a) From a quick read of the Arduino & Adafruit I2C code, there is no "hardening" to cope with I2C errors. (b) I see no evidence that the micro-controller is hanging - the code could be in a loop with no timeout (for example). It's important not to blame the MCU prematurely. I recommend you view the I2C signals, ideally using a 'scope, otherwise with a logic analyser(LA), both before and after the problem. Even better if your scope or LA can trigger on the start of the problem. (c) "x-axis=Samples y-axis=Samples" I doubt that - perhaps x-axis = samples & y-axis = values?
    – SamGibson
    Aug 23, 2017 at 0:03

1 Answer 1


There is a known issue with the default "Wire" library, where if a certain transition is missed, the code goes into an infinite loop (rather than timing out). You possibly are reaching that situation. A possible reason is your printing of numbers at quite a high baud rate. The printing could be taking up so much time (itself causing serial interrupts as the serial buffer empties) that the I2C code doesn't execute in time the I2C interrupt.

You could try dropping the printing baud rate (something I wouldn't normally recommend) which means the serial interrupts would occur less frequently.

There is a library by DSS Circuits that has a time-out for I2C which may work around this issue to an extent:


Another thing you could try would be to summarize the data (eg. sample for 500 ms and then send the average), which would reduce the amount of serial comms.

Also you might try lower values for the pull-up resistors, like 4.7k.

So, my recommendations are:

  • Drop the baud rate (eg. to 9600 baud) and see if the hanging goes away
  • Summarize data rather than sending each reading
  • Try 4.7k pull-up resistors

If you have a logic analyzer you could look at the I2C signals and see if you can work out if the data is being transmitted as expected when the vibrations occur.

Saleae sell logic analyzers, the cheapest being $US 109. I use their (more expensive) analyzers extensively for debugging this sort of thing.

Your Answer

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

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