Lab 4 due 12/4
In this lab we will further improve our Arduino skills by making a pedometer out of it. This is an interesting lab with fun results to look out for! Focus on the possibilities of how to use these Arduino and MATLAB skills you can use in your final project. Be careful with the equipment and have fun!!
The lab report should include the following:
Describe your experimental setup. Which axis of the accelerometer did you use? Was the orientation positive-up or positive-down? Where and how did you attach the device to the person? Are your answers to these questions consistent with what you observe in the experimental data?
Present the results of your calibration experiment. Include a graph of the acceleration measurements in units of g's. What was the distance traveled per step?
Present the results of your distance and speed measurement for the unknown distance test. Include a graph of the acceleration measurement in units of g's. Describe how you estimated the number of steps taken during the experiment. If you automated this using MATLAB, include a brief outline of the processing steps in your program. A bulleted list works well for describing "psuedo-code".
Report your estimate of the distance traveled and speed of travel.
This report can be brief; it does not need to be as long as the previous lab reports, but it should be properly formatted and consistent with the course guidelines. There is no 'model' for this lab report. Your goal in writing this lab report is to convey to the reader that you have been able to successfully build and undertand the operation of the Arduino-based hardware. This will better prepare you for writing the final project report, using what you have learned so far.
MPU-6050: 6-DOF 3-Axis Accelerometer Gyroscope Sensor Module GY-521 for Arduino
MPU 6000 Series (Datasheet, Register map)
** You have to install this library on your computer before you proceed with the lab. Go to "Arduino IDE -> Tools -> Manage Libraries" and then search and install the latest version of this library o be able to use it in your program.
Interfacing Micro SD Card Module with Arduino (Courtesy: lastminuteengineers.com)
** Check if the card is connected properly. Go to "Arduino IDE -> File -> Examples -> SD -> ..." and check the relevant sketches. Play around with these example sketches in general to improve your understanding and build confidence!
Arduino playground (Useful for projects)
Arduino Uno
Acceleromter Gyroscope GY521
MicroSD card adapter
Following equipment are required for data collection
An Arduino uno board
USB Cable
Power supply (Computer USB port, Powerbank or 9V battery + connection cable)
Accelerometer Gyroscope GY521 breakout board
MicroSD card and adapter
Breadboard
Jumper wires
Computer with following software
Arduino IDE
ArduinoScope
Make sure the connections are correct and proper before you power the Arduino
Remember to take measurements of the calibration and unknown distance.
Be careful while walking around with the mobile set up, specially when you are powering it up from the laptop.
Make sure that the Arduino is tied properly to the volunteer's body to avoid possible drop and damage.
Connections for GY521
Connections for the SD card adapter
Accelerometer Gyroscope GY521
Vcc to 5V pin on the Arduino
GND to the GND of Arduino
SCL to pin A5 of Arduino
SDA to pin A4 of Arduino
MicroSD card adapter
Vcc to 5V pin on the Arduino
GND to the GND of Arduino
MISO (Master In Slave Out) to pin 12 of Arduino Uno
* It is the SPI output from the Micro SD Card Module
MOSI (Master Out Slave In) to pin 11 of Arduino Uno
* It is the SPI input to the Micro SD Card Module.
SCK (Serial Clock) to pin 13 of Arduino Uno
* This pin accepts clock pulses which synchronize data transmission generated by Arduino.
CS (Chip Select) to pin 10 of Arduino Uno
* This pin is used by Arduino (Master) to enable and disable specific devices on SPI bus.
Note: The connections are different for different versions of Arduino (Reference).
Connections from alternate angle
Note that the jumper wires and the boards have been locked in place using tape. This is useful idea to maintain the connections while recording the data for a mobile set up.
Refer to the Lab 3 webpage for following topics.
Installing the Arduino IDE and ArduinoScope
Verifying the Operation with ArduinoScope
While the Arduino is connected to the computer, you can upload the sketch on it and check the changing accelerations along X-, Y- and Z- axes by shaking the setup and looking into serial monitor, or serial plotter. Based on following table (Register map for MPU 6050, page 29) you can set the acceleration scale range of the system. You can use these numbers for conversion of the measured acceleration values into g's, by dividing the observed values by the corresponding LSB sensitivity.
Note: Format the SD card before you take the measurements. If there is an error due to the formatting using Windows (forum discussion), you can use this software (download webpage link).
First, let's upload a sketch that enables mobile data logging on a SD card. Copy and paste the code below into the Arduino IDE and save it as "ArduinoLog402.ino". Follow the intructions to compile and upload the code to the Arduino. The "ArduinoLog402.ino" sketch performs the same operations as "arduinoscope402.ino" but it also saves the data to a text file on the SD card, therefore, the SD Shield must be installed for this sketch to operate.
#include<Wire.h>
#include<MPU6050.h> // Install this header as mentioned in lab instructions
#include<SD.h>
MPU6050 mpu1;
File myFile;
const int MPU=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
const int chipSelect = 10;
// Full scale acceleration range options (use as required)
#define MPU6050_ACCEL_FS_2 0x00
#define MPU6050_ACCEL_FS_4 0x01
#define MPU6050_ACCEL_FS_8 0x02
#define MPU6050_ACCEL_FS_16 0x03
// Pin 13 has an LED connected on most Arduino boards, give it a name:
int led = 13;
unsigned long time;
String dataString = "";
String dataFname = "run1.txt";
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// Initialize the GY-521 board
mpu1.initialize();
mpu1.setFullScaleAccelRange(MPU6050_ACCEL_FS_16); // Change this to change the desired acceleration range
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Initializing and verifying the card presence
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// Open the data file
char buf[12];
int cnt = 1;
// Find a unique file name
dataFname.toCharArray(buf,12);
while (SD.exists(buf)){
Serial.println("File <<"+dataFname+">> exists!");
cnt += 1;
dataFname = "run"+String(cnt)+".txt";
dataFname.toCharArray(buf,12);
}
// Opeining the SD card to write
Serial.println("Opening new logfile:"+dataFname);
myFile = SD.open(dataFname, FILE_WRITE);
// Confirming if the card is open to write
if (myFile){
Serial.println("Start logging...");
// Turn on LED to show it is logging
digitalWrite(led, HIGH);
// myFile.close();
}
else{
Serial.println("File not open!");
// Turn off LED to show it is not logging
digitalWrite(led, LOW);
}
myFile.close();
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,12,true);
// Extracting the Accelerometer values
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
// Extracting the Gyroscope readings
// GyX=Wire.read()<<8|Wire.read();
// GyY=Wire.read()<<8|Wire.read();
// GyZ=Wire.read()<<8|Wire.read();
// make a string for assembling the data to log:
time=millis();
dataString = String(time)+","+String(AcX)+","+String(AcY)+","+String(AcZ);
Serial.print(dataString); Serial.print(" \t ");
// Individual serial print options
// Serial.print("Accelerometer: ");
// Serial.print("X = ");
// Serial.print(AcX); Serial.print(" \t ");
// Serial.print(" | Y = ");
// Serial.print(AcY); Serial.print(" \t ");
// Serial.print(" | Z = ");
// Serial.println(AcZ);
// Serial.print("Gyroscope: ");
// Serial.print("X = "); Serial.print(GyX);
// Serial.print(" | Y = "); Serial.print(GyY);
// Serial.print(" | Z = "); Serial.println(GyZ);
// Serial.println(" ");
// if the file opened okay, write to it:
myFile = SD.open(dataFname, FILE_WRITE);
if (myFile) {
Serial.println("Writing to data file...");
myFile.println(dataString);
// Turn on LED to show it is logging
digitalWrite(led, HIGH);
//delay(500); // You can uncomment and change delay value for observation frequency
}
else {
// if the file didn't open, print an error:
Serial.println("error opening data file");
// Turn off LED to show it is logging
digitalWrite(led, LOW);
}
myFile.close();
}
After upload, switch off the Arduino (disconnect the power supply/battery and unplug the USB-cable)
Using a USB card reader attached to your PC, insert your SD card. It should show up as an external drive on your Windows PC.
Delete all the log files from the SD card, eject the card from the PC and install it on the Arduino (with the power OFF).
Note: Once you power on the Arduino the program will start. The logging program saves the data in sequential log files called "Lab4RunN.txt" where N is an integer starting with 1. It looks on the SD card to make sure it does not overwrite a log file and finds the next available name. For example, if "Lab4Run1.txt" and "Lab4Run2.txt" are already on the card when the program starts it will save the new data to "Lab4Run3.txt". To restart the logger, and restart a new log file, you can simple disconnect the power and then reconnect the power or use the Reset button on the Arduino. After you power off the Arduino you can remove the SD card and put it in the provided SD card reader to copy the files from the SD card to a folder on your PC.
Power ON the Arduino with the power supply (Battery/Power bank preferred; the USB with laptop works but be careful while walking around with that set up). Use the cable that connects the power on the Arduino (battery to the barrel connector, standard USB A/B cable with power bank or laptop). Make sure to insulate the metal surfaces (e.g. battery case) from the exposed pins of any of the the printed circuit boards.
Repeat the validation experiment above. This should be recorded as "Lab4Run1.txt".
Power OFF the Arduino by remove the power source connection.
Reconnect and power ON the Arduino - this should start "Lab4Run2.txt" log file.
Carefully shake the data logger to generate a random acceleration signal (You can play a gentle game of catch or do something else to generate an 'interesting' acceleration signal, but only if you have a 9V battery connected to it).
Power OFF the Arduino.
Remove the SD card, install it in the card reader and copy the log files.
Use MATLAB to display the results. The below MATLAB code imports the data from a log file (specify the name of your log file!) and visualizes it. Copy and paste the code to a MATLAB script and save it as "importArduinoLog.m".
% Example of importing data from Arduino SD data logger
fname = 'Lab4Run1.txt';
data0 = importdata(fname,',');
% Now plot the results
figure(1)
clf();
plot(data0(:,1),data0(:,2),'r')
hold on
plot(data0(:,1),data0(:,3),'b')
plot(data0(:,1),data0(:,4),'g')
xlabel('Time [ms]')
ylabel('??')
legend('Ax','Ay','Az')
title(sprintf('ArduinoLog Log - %s',fname))
Now that we have a portable data logger that can record a single channel of acceleration we are ready to measure a person's steps. The calibration step will serve two purposes: first you will determine a setup that gives you clean measurements for later process and second you will measure the distance your volunteer covers in 10 (ten) steps.
You will need to attach your data logger to a volunteer. It will take some trial and error to get a setup that will give you a clean acceleration measurement from which you can determine individual steps. Orient the axis of measurement vertically so that you are measuring vertical acceleration. Firmly attach the device near the volunteers hip. Zip ties and a belt loop seem to work.
With the volunteer standing still, power on the data logger and wait a few (2) seconds.
The volunteer should take 10 slow, deliberate steps and then stop.
Wait a few more seconds (e.g., 2) while the volunteer is stationary before powering off the data logger.
Measure how far the volunteer walked in 10 steps and record the distance per step.
Remove the SD card from the data logger, connect it to your PC and plot the data. It is recommended that you graph the data in engineering units of acceleration (g's) so that you can ensure your data is valid before moving on in the lab. Make sure you do the necessary conversions based on the full scale acceleration range you have used.
Make sure you can clearly see each step in the acceleration record. Do your results look like this?
Now that we have a calibrated system we will repeat the experiment, but now we'll have the volunteer walk an unknown distance. Choose a new random distance, and make sure that the unknown distance is different from the previous calibration distance, preferably longer. You may want to repeat the experiment a few different times. Make sure to view that data using MATLAB to verify that you have a clean measurements.
From the data record you should be able to estimate the distance the volunteer traveled (in units of feet) and the speed (in units of feet per second). Use the actual measured value of this unknown distance to calculate the estimation error in distance.
You can obviously count the number of steps in the record "by hand". This is a fine approach, but you might also consider automating the process with MATLAB, to test your MATLAB practice and skills developed so far.
Since this is a fairly simple MATLAB program (actually a small part of the codes we have used so far!), we will leave the MATLAB programming to you! Add the code in the appendix of your report.
An alternate method to perform this lab using EEPROM of the Arduino board instead of the SD card can be found in this link. You can use this to perform your lab, or for your own learning and practice.