Rocket inertial unit : reading on MPU6050 using DMP and writing on a SDcard.

Hi guys ! I'm Antoine, a beginner in Arduino. I have some smattering in Python and a basic college level in electronic.

My project is to save acceleration and rotation datas from a rocket flight with a embedded MPU6050 (on GY-521 chip), my board is an Arduino Nano.

I will prefer to use DMP algorythms to reduce errors and i need a polling rate of 200 Hz.

My question is :

I can write on my SDcard and print YPR datas with DMP and Raw datas from the MPU6050 but how to modify the example program MPU6050_DMP6 to export the datas on the SDcard and to add the acceleration datas (not obtained with DMP ?) ?

I've seen this post :

The guy's problem is similar to mine but he's not using the same board so I don't know how to do.

Every share is welcome !

You might want to check here for code to read the MPU6050 and if you search this site you will find code for writing the SD card. Given space and weight is probably an issue as it is on every rocket you might want to use an Arduino ProMini as you will have the same functionality in a much smaller package and it operates at 3.3 V which is handy for communicating with the MPU6050 and the SD card.

You should start with code that reads the MPU6050 alone and debug it, then do the same for writing the SD card, once they are both working as you would like integrate them together. I find it easier to read code and debug for each piece separately when feasible and then integrate. Think crawl, walk and then run.

Hope this helps,

Thank you wade !

So I will first try to make the SD shield work. All the examples to read MPU6050 are working fine but I don't know how I would be able to push the frequency to 100hz ?

For the SD shield, i have SCK (CLK) on pin 13, MISO on 12, MOSI on 11 and CS on D4 but all the test programs i found didn't worked.

How can I know if my SD shield is working ?

I'm sorry I have not enouth time remaining on this project to buy another Arduino and everything is perfectly fitting in the rocket.


Hi everyone !

Ok so i worked on it and my program is very close to be fully working, at least i guess …
I modified the MPU6050 DMP6 example and added small parts to write on my SDcard.

Everything is working ! For 10 seconds … 10 seconds after a reset, the Sd shield stops writing. I get a txt file with around 80 datas. Why ?

Thank you by advance ! Antoine

Here is the .ino file, code is too long …

CV2r.ino (8.92 KB)

Maybe someone with an MPU6050 can verify your problem; I haven't looked at your code, but I have run in to similar situations before. Do you have anything in your code that will free up memory? Many times, I've found that if I've run out of memory for variable storage, my project will lock up.

I'm pretty sure you're right ! I need to clean the memory in the loop, is there a part of code to clean the right part ?

I'll take a look at your code this evening; I can't really answer that reliably, or even a suggestion how to make it cleaner.

One way you could figure out where the problem is happening would be to determine how many bytes 80 data points actually is, create a sketch with just the SD portion of your code and Serial and then throw 90 points worth of data at it through a serial port and have it dump whatever it receives to the card. I use PuTTY for that kind of test.

Basically, build up your code in blocks until it breaks again, the same way you would a lab in college.

You are opening the sd file for each loop, why?

You wait for an interrupt (or a complete packet), then you open the file, why not the other way around?

If the file was opened unconditionally, it should be closed unconditionally also.

Hi Whandall

I'm opening the file for each loop cause i don't know how to close it after the loop, and it has to be closed before being read.

In fact i'm a beginner so I have no real reason to do it like this, can you tell me how would you improve the code ?

Thank you chazzy !

Have a look at this thread Seeking Higher Sampling Rates, the open close problem is discussed there in depth.

I think your instability results from the unmatch open and close that is possible with your code,
but this is only a guess. I don’t have any experience with the MPU stuff.

I've looked the thread, used the library SdFat and it solved my problem. Yet I think that my program isn't efficient as it could be.

I still haven't had a chance to look at your code, but Whandall is on the right track.

You only have 2k of RAM available to the microcontroller. Every time you open the file on the SD, you're setting up a buffer (a portion of RAM dedicated to that file.) If you don't close the file, at least some of that RAM, be it data bytes or just the variables associated with the name and mode, remains in use. 2k can get chewed up very rapidly that way.

What I would suggest would be to just focus on working with the SD file and dumping data to it through Serial until you've got it stable. The thread Whandall is pointing to has some very good information in it.

I will look at this thread soon.

I have another problem. I have a led and a buzzer on the same Arduino and I want in a cycle of 3000ms.

-500ms led on
-500ms buzzer on
-2000ms delay

I Will need a counter because I cannot put a delay in the data recording loop.

How to do this ?

const byte buzzerPin = 2;
const unsigned int durations[3] = { 500, 500, 2000 };
byte blinkBuzz;
unsigned long lastBlinkBuzz;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

void loop() {
  unsigned long topLoop = millis();
  if (topLoop - lastBlinkBuzz >= durations[blinkBuzz]) {
    lastBlinkBuzz = topLoop;
    switch (blinkBuzz++) {
      case 0:
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(buzzerPin, HIGH);
      case 1:
        digitalWrite(buzzerPin, LOW);
      case 2:
        digitalWrite(LED_BUILTIN, HIGH);
        blinkBuzz = 0;

Your code worked perfectly. Thanks for all !!!

The electronic part of the rocket is finished, i need to launch it quickly because i have a report to do with the datas for wednesday.

I will share some pics of the electronic part.