Introduction: Simple Arduino 3-Axis Accelerometer Datalogger
Hello! This is a simple Arduino project that takes raw acceleration values, including the effect of gravity, and writes them in unit g’s to a micro SD card. The tutorial will cover the procedure from hardware setup to guidance on processing the accelerometer data. I have included the steps to first-time Arduino setup and troubleshooting common issues, which can be skipped if desired, as well as links to helpful resources.
Supplies
Here are the components needed and a suggested specific product for each.
Arduino Microcontroller : Arduino Nano
3-axis Accelerometer : MPU6050 3-axis Accelerometer/Gyroscope (highly recommended)
Micro SD Card Breakout Board : Adafruit Micro SD Card Breakout Board
Jumper Wires : Jumper Wires
Step 1: Wiring the Arduino Board
First you will need to solder pin heads onto the accelerometer, micro SD card breakout board, and Arduino microcontroller. This allows you to attach and remove female-to-female jumper wires easily.
Then, wire the accelerometer and breakout board to the Arduino as follows.
MPU6050 Accelerometer to Arduino
Connect 5V [IMU MPU-6050] to VCC [ARDUINO]
Connect SDA [IMU MPU-6050] to Analog IN (A4) [ARDUINO]
Connect SCL [IMU MPU-6050] to Analog IN (A5) [ARDUINO]
Connect GND [IMU MPU-6050] to GND [ARDUINO]
Connect INTPIN [IMU MPU-6050] to Pin 2 (Digital PWM pin) [ARDUINO]
Micro SD Card Breakout Board to Aruino
Connect 5V [SD CARD BREAKOUT BOARD] to the 5V pin [ARDUINO]
Connect GND [SD CARD BREAKOUT BOARD] to the GND pin [ARDUINO]
Connect CLK [SD CARD BREAKOUT BOARD] to pin 13 or 52 [ARDUINO]
Connect DO [SD CARD BREAKOUT BOARD] to pin 12 or 50 [ARDUINO]
Connect DI [SD CARD BREAKOUT BOARD] to pin 11 or 51 [ARDUINO]
Connect CS [SD CARD BREAKOUT BOARD] to pin 10 or 53 [ARDUINO]
The pins on the Arduino vary slightly depending on the model (Uno, Nano, Mega, etc.). See here for pinout diagrams of many different ones.
Note: many of these pinout diagrams do not include the ICSP pins (those are the pins on the front top of the Arduino). If you need an extra ground or VCC pin, such as in this project where both the devices need 5v connections, use the ICSP pins. You can do a quick Google Search for “Arduino ICSP pinout [my Arduino model]” to find the right diagram.
Now for the SD card. It is strongly recommended to reformat a new SD card when you buy it. For best results, use the official reformatter from the SD association. Make sure you select FAT16 or FAT32 because these are the formats supported by the Arduino SD Library.
Step 2: Setting Up Arduino Board
The open-source Arduino Software IDE (Integrated Development Environment) can be downloaded for free at the Arduino website.
To get started using the IDE with your Arduino board, plug the board into your computer with the MicroUSB cable. Go to the menu Tools >> Board and select your Arduino model from the drop-down list. Then go to Tools >> Port and select your USB port from the drop-down list (it is named in the format “/dev/cu.usbserial-####”).
You should start by running some simple code to make sure the microcontroller is working. In the Arduino IDE, go to Files >> Examples >> 01.Basics and select Blink. The Blink program is a very simple sketch - it’s like the “Hello World” of Arduino. Compile and download it, then go to Serial Monitor in the Arduino IPE to see the output.
Note: One error message you may run into looks like,
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00...
This is one of the most common error messages and can have a number of causes. One thing to try is changing your processor: go to Tools => Processor and select “ATmega328P (Old Version)”. This fix is may work if you have purchased a third-party microcontroller (not made by the Arduino company) that is not updated to be compatible with the new version.
Step 3: Setting Up MPU6050 + SD Card Board
Next we will download the two Arduino libraries needed for the accelerometer. Here are the links to download:
The SD library is already included in the Arduino IPE so that is all the libraries you will need.
Navigate to the Arduino folder on your desktop and drop the libraries in the Libraries folder. If you have a Mac, there is a different process to get to the Arduino folder: right-click the Arduino app in Finder and choose Show Package Contents >> Java >> libraries.
Now let’s run some more simple code to make sure the accelerometer and SD card will work correctly with the microcontroller. Use the sketch “MPU6050-raw” to test the accelerometer and “Card Info”, as well as “ReadWrite” to test the SD card.
'
TROUBLESHOOTING GUIDELINES:
- Have you connected the right pins? Wire and test one of the components at a time
- Set baud rate in Serial Monitor to match the serial communication # in the sketch?
- Try changing the processor from ATmega328P to ATmega328P (Old Version)?
- Does the SD card work outside of Arduino?
- Is the SD card formatted in an Arduino-compatible format (FAT16 or FAT32)?
Step 4: Programming in Arduino
Provided here is an Arduino sketch that provides the basic functionality and can be copy-pasted directly into the IPE and run. It reads raw acceleration measurements from the MPU6050 accelerometer in x/y/z axes, converts to units of g-force, and writes the data in three columns to an SD card while also outputting to Serial Monitor. When held flat and motionless, the accelerometer will register approximately 0 g’s in the x and y axes and approximately 1 g in the z-axis. The data goes to a txt file defined in the sketch.
<p>// Accelerometer/SD Datalogger<br>/* Gets raw g-force measurements from MPU6050 accelerometer in x/y/z axes and writes to an SD card </p><p>Credit to: Source available at: Jeff Rowberg for MPU6050_raw data example sketch <a href="https://github.com/jrowberg/i2cdevlib"> www.HowToMechatronics.com </a> Dejan Nedelkovski for Arduino SD card tutorial <a href="http://www.HowToMechatronics.com"> www.HowToMechatronics.com </a> */</p><p>#include <sd.h> #include <spi.h> #include "I2Cdev.h" #include "MPU6050.h"</spi.h></sd.h></p><p>#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h" #endif</p><p>File myFile;</p><p>// class default I2C address is 0x68 // specific I2C addresses may be passed as a parameter here // AD0 low = 0x68 (default for InvenSense evaluation board) // AD0 high = 0x69 MPU6050 accelgyro; //MPU6050 accelgyro(0x69); // <-- use for AD0 high</p><p>int pinCS = 10; //CS pin for SD card int scale = 16384; //divide values by MPU6050 sensitivity scale to get readings in g's (m/s^2 / 9.8) //use scale = 16384 for the default I2Cdevlib sensitivity setting of +/-2</p><p>int16_t ax, ay, az;</p><p>#define LED_PIN 13 bool blinkState = false;</p><p>void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif</p><p> // initialize serial communication // serial value can be up to you depending on project Serial.begin(9600);</p><p> // initialize SD card if (SD.begin()) { Serial.println("SD card is ready to use."); } else { Serial.println("SD card initialization failed"); return; }</p><p> // initialize accel/gyro Serial.println("Initializing accelerometer..."); accelgyro.initialize(); accelgyro.setSleepEnabled(false);</p><p> // verify connection Serial.println("Testing accelerometer connection..."); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");</p><p> // configure Arduino LED for pinMode(LED_PIN, OUTPUT); }</p><p>void loop() { // read raw accelerometer measurements from device accelgyro.getAcceleration(&ax, &ay, &az);</p><p> // display tab-separated accel x/y/z values in serial monitor Serial.print("acceleration:\t"); Serial.print((float) ax / scale); Serial.print("\t"); Serial.print((float) ay / scale); Serial.print("\t"); Serial.println((float) az / scale); // write the accelerometer values to SD card myFile = SD.open("test2.txt", FILE_WRITE); if (myFile) { myFile.print("acceleration:\t"); myFile.print((float) ax / scale); myFile.print("\t"); myFile.print((float) ay / scale); myFile.print("\t"); myFile.println((float) az / scale); myFile.close(); } // blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState); }</p>
'
Using this sketch should allow you to start collecting data with the accelerometer right away. For example code with more complex additions to the accelerometer functionality, refer to the Arduino example sketch MPU6050_DMP6 by Jeff Rowberg (available in the Examples tab of Arduino IPE), which has several options for combined accelerometer/gyroscope units.
Step 5: Some Final Suggestions
Here are some suggestions for best use of this project.
- To make the package portable, you just need a battery to plug into the USB cable. A normal phone battery pack works for this purpose.
- A hard tap on the accelerometer (preferably while having a protective case over the chip itself) creates a sharp spike in the acceleration reading. This can be a helpful way to tell when an activity you are trying to measure starts and stops.
- For a project involving collecting and analyzing accelerometer values, consider assembling and testing with two separate Arduino/accelerometer modules. This allows you to get data on relative accelerations between different locations (e.g., a person’s dominant vs. nondominant hand for a physical activity-related project), which may be more meaningful data than acceleration from just one module.
Step 6: Additional Resources
Tutorial by Adafruit on interfacing the SD Card Breakout Board with Arduino
Tutorial by Arvind Sanjeev on interfacing the MPU6050 accelerometer with Arduino
Jeff Rowberg’s I2C Library + Documentation on GitHub