I have a problem collecting data from my accelerometer/gyroscope sensor with a constant sampling rate. Is there a way to program the arduino to collect data from the sensor and store these values in a SD card with a constant frequency of 1kHz without using delay? The sensor that I am using is a MPU6050 gy-521 sensor that uses I2C with arduino. I will be reading 6 different datas from my sensor: acceleration in x,y,z directions and gyration in x,y,z directions. Thank you all in advance for the advice/help.

so my project will be making a device that collects the values from the sensors and saves them to study the data later. First, a LCD display will print out a message without having any data collection to occur. The data collection will start when a button is pushed. After the button is pushed, the display will print out another message saying that the arduino is collecting data and the Arduino will save the 6 values (x,y,z acceleration and gyration) from the accelerometer/gyroscope sensor to the SD card. The data collection will stop after the button is clicked again and the display will print out another message saying that it has stopped collecting data. Then, this process can start again if the button is clicked once again. Thanks in advance

  • 1
    Welcome to Arduino.SE. What have you done so far? Liberally describe your setup and post any code as well. Apr 21, 2016 at 0:26
  • 2
    You may actually find if you research it in more detail that you can get the MPU6050 itself to produce regularly spaced samples, and merely poll for their availability. Apr 21, 2016 at 4:52

2 Answers 2


You have discovered delay() does not guarantee the period of your code. This is because the period of your code is the delay() plus the time for your code to execute.

What you need is a hardware clock to give your code an indication that a fixed period has expired. This is one of several Arduino timer libraries. The timer library web page describes your problem in more detail with example code using delays and using the timer library features.


A quick and dirty solution is to just the right the code that does the sampling and loop it, say 10,000 times, with a short delay. Measure the time it takes to do the 10,000 samples by recording time before and after all the samples.

The amount of time taken will be:

10,000 x (delay period -> known) + 10,000 x (actual amount of time to do the sampling)  

From this data you can determine the required delay to get your 1 kHz sampling goal.

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.