ASSIGNMENTS/DELIVERABLES
(REPORT DUE TUESDAY, 11/14/2023 AT 1:30 PM ON LAULIMA)
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? Summarize the measured distance, steps, and distance/step in a table.
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 steps estimation, distance traveled estimation, estimation error in distance, time, and speed of travel in a table.
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 understand 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.
Useful Links
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)
Objectives
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 main purpose of Lab 4 is to familiarize ourselves with using an Arduino to measure and record data while it is not connected to a computer. Standalone Arduino operation comes in handy for class projects that require mobile data logging.
For Lab 4 we use the Arduino and our ADXL accelerometer to make a simple pedometer (A pedometer is a device that counts each step a person takes by detecting the motion of the person's hips. Because the distance of each person's step varies, an informal calibration, performed by the user, is required if presentation of the distance covered in a unit of length (such as in kilometres or miles) is desired.)
To do this we'll connect the ADXL sensor to the analog inputs of the Arduino and have the Arduino record (log) data as we walk/run around.
Data Logging with Arduino
Equipment
Following equipment are required for data collection
Arduino Uno board
USB Cable
Power supply (Computer USB port, 9V battery or 4xAA battery pack) plus connection cable
Accelerometer ADXL 335
MicroSD card and adapter
Breadboard
Jumper wires
Computer with following software
Arduino IDE
ArduinoScope
Note: The jumper wires and the boards can be locked in place using tapes. This is useful idea to maintain the connections while recording the data for a mobile set up.Â
Arduino Uno
Accelerometer ADXL335Â
MicroSD card adapterÂ
Hardware Setup
Insert the MicroSD card into the MicroSD adapter and connect the ADXL accelerometer (5 pins with ST pin unused) and the MicroSD adapter (6 pins) to the Arduino pins as follows:
1. Accelerometer ADXL335 to Arduino
X: A0
Y: A1
Z: A2
3.3V: 3v3 (3.3V)
GND: GND
2. 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 4 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 for ADXL335
Connections for the SD card adapter
Software Set Up
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).
Copy and paste the following code to the Arduino IDE and save it as a sketch:
#include <SPI.h>
#include <SD.h>
File dataFile;
void setup() {
    pinMode(13,OUTPUT);
    // Add delay here in msec if you don't want to start recording right away when powered on
    // delay(10000);
    // Open serial communications and wait for port to open
    Serial.begin(9600);
    Serial.println("Initializing SD card...");
    // see if the card is present and can be initialized:
    if (!SD.begin(4)) {
        Serial.println("Card failed, or not present");
        return;
    }
    Serial.println("card initialized.");
    // Open the data file, incrementing number if necessary
    String dataFname = "Log1.txt";
    char buf[16];
    int cnt = 1;
    // Find a unique file name
    dataFname.toCharArray(buf,16);
    while (SD.exists(buf)){
        Serial.println("File <<"+dataFname+">> exists!");
        cnt += 1;
        dataFname = "Log"+String(cnt)+".txt";
        dataFname.toCharArray(buf,16);
    }
    Serial.println("Opening new logfile: "+dataFname);
    dataFile = SD.open(buf, FILE_WRITE);
    Serial.println("Start logging...");
    // Turn on LED to show it is logging
    // digitalWrite(13, HIGH);
}
void loop() {
    // make a strxing for assembling the data to log:
    String dataString = String(String(millis()/1000.0,3) + " " + analogRead(A0) + " "Â
+ analogRead(A1) + " " + analogRead(A2));
    dataFile.println(dataString);
    dataFile.flush();
    Serial.println(dataString);
    /*
    Serial.print(millis()/1000.0,3);
    Serial.print(" ");
    Serial.print(analogRead(A0));
    Serial.print(" ");
    Serial.print(analogRead(A1));
    Serial.print(" ");
    Serial.println(analogRead(A2));
    */
}
Several useful SD card sample programs are also available under File > Examples > SD:
CardInfo
DumpFile
listfiles
...
Verify Operation
Press the arrow button to compile, upload, and run your code on the Arduino. If there are no errors, you can see the accelerometer signals using the "Serial Monitor" and "Serial Plotter" tools. Shake the accelerometer and confirm that the three signals respond appropriately. If you turn over the accelerometer you should see a change in the Z-axis signal as the direction of gravity flips with respect to the sensor.
The data that is printed to the serial port is also saved to a file on the microSD card, starting with "Lab4_log1.txt" and incrementing each time the program is restarted. Check this by running the "listfiles" or "CardInfo" programs from the File > Examples > SD.
Confirm that the data is being written to the microSD card by using an SD card reader (disconnect the USB cable first), or by using the "DumpFile" program:
Change the filename in SD.open("datalog.txt") to "Lab4_logN.txt" where N is the number of the logfile you want to transfer
Run the program
Copy-paste the data printed into the Serial Monitor into an array in MATLAB
Plot the data from the MATLAB array using plot(data(:,1),data(:,2:4))
Note: The Arduino code above samples the accelerometer at about 10 Hz. The sample rate can be increased to about 20 Hz by changing Serial.begin(9600) to Serial.begin(115200), or by commenting out the Serial.println statement in the loop() function.
Cautions!
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, especially when you are powering it up from the computer.
Make sure that the Arduino is tied properly to the volunteer's body to avoid possible drop and damage.
Let's go mobile!
When the Arduino is disconnected from the USB cable and plugged into battery power, the data logging program will also record measured data to the microSD card. This way the Arduino can operate independently from a computer.
Note: Once you power on the Arduino the program will start. The logging program saves the data in sequential log files called "LogN.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 "Log1.txt" and "Log2.txt" are already on the card when the program starts it will save the new data to "Log3.txt". To restart the logger, and restart a new log file, you can simply disconnect the power and then reconnect the power or use the Reset button on the Arduino. Then transfer the data to your computer either using a microSD card reader or by reconnecting to the computer with the USB cable and using the "DumpFile" program as described previously.
Power ON the Arduino with the 9V or 4xAA batteries (the USB should NOT be connected). Use the cable that connects the battery to the barrel connector on the Arduino. Batteries, wires, pins, and circuit boards should be secured with tape and/or rubber bands.
Repeat the validation experiment above. This should be recorded as "LogN.txt".
Power OFF the Arduino - remove the battery barrel plug.
Power ON the Arduino - this should start the next "LogN.txt" log file.
Carefully shake the data logger, play a gentle game of catch or do something else to generate an 'interesting' acceleration signal.
Power OFF the Arduino.
Transfer the log data to MATLAB with an SD card reader and >> load('LogN.txt') and >> data0 = LogN, or with "DumpFile" and Copy-Paste from the Serial Monitor to a MATLAB array called "data0" or whatever name you choose.
Use MATLAB to display the results. The below MATLAB sample code plots the data in a figure. Copy-paste your code to a MATLAB script file and save it.
% Example of importing data from Arduino SD data logger
% fname = 'Log1.txt';
% data0 = importdata(fname,',');
% Now plot the results
figure(1)
plot(data0(:,1),data0(:,2:4))
xlabel('Time [seconds]')
ylabel('Amplitude [counts]')
legend('A0','A1','A2')
title('logN.txt')
Convert the results to units of g's. If we recall that the ADXL has a bias voltage (the voltage corresponding to 0 g output) of roughly 1.6 Volts and the sensitivity of the accelerometer is roughly 300 mV/g, we can convert the measured voltage into an acceleration measurement in g's. The analog voltage input range of the Arduino is 0-5V at 10 bits (1024 counts). Modify the code above to output g's. Here are some hints on how to do it:
xdata = data0(:,2); % import accelerometer data of one axis (in this case Analog0)
xvolt = xdata/1024*5; % determine measured voltage (1024bits from 0-5V)
S = 0.300; % define sensitivity of sensor in V/G
xaccel = (xvolt-1.6)/S; % determine acceleration, consider bias voltage of 1.6V
Pedometer Calibration
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 9 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 volunteer's 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 9 slow, deliberate steps and then stop.
Wait a few more (2-3) seconds while the volunteer is stationary before powering off the data logger.
Measure how far the volunteer walked in 9 steps and record the distance per step.
Remove the SD card from the data logger, connect it to your computer and plot the data. Transfer your data from the microSD card into MATLAB either by using an SD reader or using the "DumpFile" program as described previously.
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?
Measuring Distance and Speed
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.
Measure the actual value of this unknown distance to calculate the estimation error in distance.
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).
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
Alternate Approach (Using EEPROM of the Arduino)
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.