Data logger for MPU6050

Hi there,

I had developed a simple data logger that can log 2 MPU6050 imu with real time without using a RTC device.
My devices:

  1. Arduino Nano
  1. 2 MPU6050
  1. MicroSD Card shield

I bought all these modules from ebay as shown in the links.

I had modified the lowlatencylogger example from SDFat library and the code is as shown in the attached file.

The way I obtain the unixtime without using an RTC is, I used a dual USB to mini usb cable as shown in this link:

And when I upload my code to the microcontroller through one USB pin to PC, I also connect the other USB pin to a power bank at the same time. So when I unplug the USB from the PC, the micro controller is still power on and the unixtime will still be accurate.

I will start logging when I press the button on the switch and stop when I press it again.

So my question is, connecting a micro controller board to PC and power bank at the same time, will it fry my micro controller, IMUs and microSD card? So far I do not have any problem, but if I use this way in long term, will it be a problem?

Thanks for any advise.

:slight_smile:

mpudatalogger.ino (13.6 KB)

No problem for the Arduino.
If you turn off the computer, will the power bank try to power the computer ? Can the power bank 5V output be connected to a USB port of the computer like that ?

Arduino Nano clone 16MHz 5V with CH340G : okay, but the 3.3V is weak, it can not supply a lot of current. Don’t even try to power a led with the 3.3V pin.

SD module with level shifter : okay. It is very cheap, the socket for the SD memory card might have bad quality metal connection pins that might corrode.

MPU-6050 module : not perfect. It is better if you power the MPU-6050 module with the 5V pin from the Nano (5V to VCC), and let the voltage regulator on the MPU-6050 module make 3.3V out of that. The signals for I2C are outside the specifications. For a more reliable I2C bus, use a level shifter for SDA and SCL. Never connect AD0 to 5V, but use the 3.3V pin of the Nano to AD0 (to change the I2C address for one of the modules). Or perhaps the AD0 is already pulled up to 3.3V on the module, and you can connect AD0 of one of the modules to GND.

Double power 5V to the Nano : okay. The Arduino will run when 5V is connected to the USB connector. Where that 5V comes from, that does not matter.

Sketch:
The “while (!Serial) {}” is for an Arduino Leonardo. You can keep that line, since it is not problem for a Nano.
You don’t set SS as output ? The SPI bus for a ATmega328P requires that SS (pin 10) is set as output.
You could add this before or after setting switchPin as INPUT_PULLUP in setup(). You set the mode for switchPin twice by the way, in setup() and in setupData().

pinMode( SS, OUTPUT);    // set SS as output, required for ATmega328P.
digitalWrite( SS, HIGH);   // for safety, make it high (a high chip select means not active)

Why do you test for Serial.available() but never read data from the Serial port ? I don’t understand that.

Koepel: No problem for the Arduino. If you turn off the computer, will the power bank try to power the computer ? Can the power bank 5V output be connected to a USB port of the computer like that ?

Hi thanks for the reply.

I did set SS as output.

// SD chip select pin.
const uint8_t SD_CS_PIN = 8;

I think digital pin 8 is also usable for nano right?

And the Serial.available(), there is no specific reason I put this line there. It is just I did not remove from the original sketch from SDFat lowlatencylogger example because I am not very good in programming and I do not want to mess the whole code by simply removing something. :P

For MPU6050, the reason I put 3.3V is because previous user said 5v input will straight away fry the MPU6050 chips. So I did not dare to connect to 3.3v. So now you are suggesting to connect the Vcc to 5V and used a level shifter for SDA and SCL pins?

And I will remove one of the switchPin mode from setupData().

So besides the above issues, the system is good to go?

And thank you very much for your advise. Cheers.

As far as I know the 'SS' pin, that is pin 10 should be set as output for the internal SPI hardware inside the ATmega328P. But perhaps that is already done in the SPI library.

You can use any digital pin for a Chip Select for a SPI device, and pin 8 is okay of course.

The MPU-6050 can not handle 5V on any of its pins. The link you gave shows a module with a voltage regulator for 3.3V, and I think that 5V to VCC is better. A I2C level shifter is the best.

When you apply 3.3V to the module VCC, the MPU-6050 might get 3.2V, and the SDA and SCL 2k2 pullup resistors (on the module) pull it to 3.2V. The 3.2V is not enough for the Arduino Nano for a valid 'HIGH' on the I2C bus. When you apply 5V to the module VCC, the voltage for the MPU-6050 is 3.3V, which is a little better, but still not within the specifications. Only with a level shifter, you can connect a 5V I2C bus correctly to a 3.3V I2C bus, and everything is according the specifications.

And thanks for taking time to read my messy code. I apologise for the untidy code I present.

And again, many thanks to your awesome reply. I will update my connection based on your advise...

:)

Found it. Here : http://www.arduino.cc/en/Reference/SPI It says : All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.

It means that the 'SS' pin must be set as output, regardless if you use pin 10 or an other pin for Chip Select. The name 'SS' is actually the name that the Arduino gives to pin 10, that is why 'SS' can be used instead of '10'. See the code in my Reply #1.

Koepel: Found it. Here : http://www.arduino.cc/en/Reference/SPI It says : All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.

It means that the 'SS' pin must be set as output, regardless if you use pin 10 or an other pin for Chip Select. The name 'SS' is actually the name that the Arduino gives to pin 10, that is why 'SS' can be used instead of '10'. See the code in my Reply #1.

Hi,

Thank you for your advise.

I had made the adjustment based on your advise and it is running smoothly. So now that I had confirmed connecting 2 power sauce to arduino is not a problem, I think I can proceed with my project.

Again, thank you for your advise.

:)