Вы находитесь на странице: 1из 12

Application Note

Communication between arduino and IMU Software capturing the data

ECE 480 Team 8


Chenli Yuan Presentation Prep
Date: April 8, 2013

Executive Summary
In summary, this application note is about how to have the arduino communicate with IMUs. In
order to achieve that goal, the effort is put into two major parts, the hardware connection and
software programming. This application note is focused on the software part. It will guide users
through how to correctly choose the libraries corresponding to the hardware components used by
design team, how to write the code which will tell arduino to read the data from the sensors, the
way to have the software display captured data in the serial monitor in an intuitive form, and the
method of time stamping the data. This application note is based on the teams experience of
using arduino software provided with hardware from Sparkfun.
Keywords
Controller & Sensors communication, Arduino Software, Library, Code Functions, Time
Stamping, Serial Monitor
Introduction
Team 08 is tasked to design a product that captures the motion of a runner, and then analyzes the
captured data with the data of an elite runner, which is stored as a baseline in the comparison
software. At last, the product send feedback to the runner in order to have the runner corrects his
running posture immediately. This task can be divided in to three major parts: data capturing,
post processing and feedback. For the first part, which is data capturing, it is important to have
correct data from sensors and smooth communication between IMUs and Arduino, otherwise, it
will be impossible to go to the following two major parts. In order to correctly program the
Arduino, programming software named arduino-1.0.3 is chosen to be the solution of
programming the Arduino because this software is provided from the same company that
fabricates the Arduino. The advantages of using this software are: ensured compatibility between
software and hardware, intuitive user interface, plenty of build-in examples and library for team
to consult and test.
Three main libraries included in the teams coding:
Wire.h Arduino Wire library, required if I2Cdev I2CDEV_ARDUINO_WIRE
implementation is used in I2Cdev.h.
I2Cdev.h I2C specialized pins function library, required if I2C buses is used for data
transferring.
MPU6050.h MPU6050 IMU library, modified to be compatible with IMU-9150, which is the
sensors solution of design team.
Some main functions used in the teams arduino programming are the followings:
Serial.begin (baudrate) initialize serial communication.

Accelgyron.initialize() initialize device (IMUs).


Accelgyro.GetMotion9 build-in function of MPU6050.h, read 9 axis measurements from
device.
String prints the data points into a string
Serial.print print the text or data string to the serial monitor
Micros() useful when doing time-stamping, give time in microseconds.
These codes will be discussed in details, and relative examples of how to use these code swill be
provided in following section.
Objectives
1) To have the Arduino continuously read data collected by IMUs. Besides hardware connection
and setup, correct software programming is required. Furthermore, write code which can
indicate that the Arduino and sensors are collecting data, i.e. using on-board LED.
2) The arduino software should be able to print the data collected by Arduino. Use serial
monitor to verify whether the collected data is correct or not. Use serial monitor of arduino
software for testing IMUs and debugging purpose.
3) Time-stamp the collected data, which is important when using multiple sensors.
4) Transfer data points from IMUs to a string, so that the data can be used for post processing.
5) Modify the display format of collected data to give an intuitive visual result.
Preparation & Arduino software set-up

The figure above shows a hardware connection of the IMU and Arduino. This application note
will not go deeper into the hardware specification of the design. However, one thing needs to be
mentioned before programming is that in order to upload the software programming into the
board, the Arduino needs to be connected with computer via an usb cable. Besides powering the
board, all the data communication including program upload is done using usb connection, and
the usb connector is located in the top right corner of the Arduino. Furthermore, the on-board
memory has a size of 32Kb, which means the file size of our programming can not exceed that
limitation. If it does reach the maximum allowed size, simplification for codes and modification
for libraries will be necessary to control the actual size of the uploaded program file to ensure the
program stored in Arduino is in complete form.
Software Set-up
Arduino-1.0.3 is provided with the hardware board on the Sparkfuns website and is free to
download at http://arduino.cc/en/main/software. For stability purpose, the team chooses version
of 1.0.3, which is not the latest version but it is recommend for stability and compatibility
purpose.
After correctly install the software and launch it, one will get the following user interface:

On the bottom right corner of the software interface, it indicates the number of serial port which
the Arduino is connecting to. However, for first time users, the detection might be wrong because
there is another software issue with Arduino installation of driver.
To install the driver, do the following steps:

1) Disconnect the Arduino from computer and plug in the board again; Windows will begin its
drivers installation process automatically, wait until the automatic driver installation fails.
2) Click on the Start Menu, and open up the Control Panel. While in the Control Panel, navigate
to System and Security, and then click on System. Once the System window is up, open the
Device Manager.
3) Look under Ports (COM & LPT), one should see an open port named Arduino UNO
(COMxx) (xx is the number of serial port, which varies depending on different computers).

4) Right click on the Arduino UNO (COMxx) port and choose the Update Driver software
option. Next, choose the Browse my computer for Driver software option.
5) Finally, navigate to and select the Arduino Unos driver file, named ArduinoUNO.inf,
located in the Drivers folder of the Arduino Software download.
6) Wait until windows finish up the driver installation from there.
Now the Arduino should be recognized by windows and be ready to use. Users can check if the
Arduino is working in device manager, similarly, the number of serial port the Arduino is
connecting to can be found there. The tutorial above is the guide of installing drivers for the
Arduino Uno with Windows OS. More information about installing drivers for MAC OS or
Linux can be found at http://arduino.cc/en/Guide/HomePage.
Once the users get to this step, go back to arduino software and finish the software set-up and
configuration. To choose the right Arduino board, from the menu bar, select Tools > Board >
Arduino UNO.

In order to let the arduino software upload program to Arduino board, configure the serial port
number. From the menu bar, select Tools > Serial Port > COMxx.

Add libraries
Once the software set up is done, user can proceed to next step adding libraries. Libraries are a
collection of code that makes it easy for users to use the functions of an Arduino. Arduino

software comes with some build-in libraries like SPI, WiFi and SD.
Adding additional libraries can extend the ability of Arduino. It is also required if user want to
connect additional parts like sensor, display and modules to Arduino. In the teams design project,
two important libraries added are MPU6050 and I2Cdev.
MPU6050 library is used for MPU6050 sensor, which is an earlier version of MPU9150. The
difference between them is MPU6050 is a 6-axis motion capture sensor composed of a 3-axis
accelerometer and a 3-axis gyroscope. MPU9150 is a newer version which is capable of doing
9-axis measurement with an additional magnetometer. Because of the similarity shared by the
two sensors, an MPU6050 library is easily modified to be compatible with MPU9150.
In order to add the necessary library, firstly, download the MPU6050 libraries from
http://www.i2cdevlib.com, the libraries distributed are ZIP files. Uncompress the ZIP file, and
copy the desired libraries folder into the softwares libraries folder:
.

Libraries in red box are essential for the design project; make sure they are added to the correct
path.
Arduino Programming & Examples
The team develops a code that allows the Arduino continuously read data from IMU including all

9-axis measurements from accelerometer, gyroscope and magnetometer. Then convert the data to
string format so that it can be used for post processing. Furthermore, for debugging purpose, the
team adds code that have the arduino software display collected data in a serial monitor. Key
functions are explained and relative examples are shown below.
Set-up code

#include xxx.h add libraries to program (xxx is the name of librairy)


As mentioned before, the three main libraries are one build-in library named Wire.h and
two additional libraries added by team named I2Cdev.h and MPU6050.h.
Because the communication chosen in the design project is I2C, corresponding libraries need
to be added in advance to active the communication. Between the two libraries related to
I2C communication, Wire.h is used to support the functioning of I2Cdev.h.
As for the MPU6050.h, it is code library specifically used for IMUs. Build-in functions of
this library allows the user to get data of 9-axis measurement with one single command,
which makes the programming much easier.
Example of adding library:

Serial.begin (baudrate) the speed of transferring data, replace baudrate with number of
desired speed.
Wire.begin() active the I2C bus.
Both of the two codes are used for setup purpose. The I2C communication does not
automatically active, thus use this build-in code from library trigger on the data transition of
I2C bus.
The Arduino support several baud rates, i.e. 9600, 38400 and so on. Higher baud rate allows
more data to be transmitted in a unit time. For accuracy purpose, a high baud rate is chosen
in the design to ensure that plenty of data is collected. However, faster data collection also
means more power consumption, thus the tradeoff of durability and accuracy needs to be
taken into consideration.
Example of setting baud rate:

Data related code (data capturing, transform, display and time-stamping)

Accelgyro.initialize() build-in function of MPU6050.h, used to initialize the sensors.


Accelgyron.GetMotion9 build-in function of MPU6050.h, used to commend Arduino to
read dara from sensors.
Both of the two codes above are built into the IMU library. For the second code, the captured
data is in the order of x, y, z axis of accelerometer; x, y, z axis of gyroscope; x, y, z axis of
magnetometer respectively. By assigning variables names to these captured data, it will be
easier to use or transmit those data in other code.
Example of having Arduino read data from sensors & name the captured data:

Define the name of IMU sensor and xyz axis measurement variables for accelerometer,
gyroscope and magnetometer respectively.

The first code prints text message in serial monitor when IMU is initializing.
The second code reads the data from IMU and assigns the captured data to the pre-defined
variables.

String transfer the data into string format


The reason the team wants to put the data into sting format are the followings:
1) Data in string format can be directly transmitted to other via Xbee.
2) MATLAB can read data of string format.

3) String formatted data can be easily change back to number.


Example of printing the data points into a string:

The code above firstly changes all numerical value to strings and then combines them into a
single one.

Serial.print prints data or text to the serial monitor


In order to understand the data collected by the sensors, the team writes the code that prints
data to serial monitor. Even though it is not necessary for final product, it is helpful during
the test phase. By monitoring the continuous updated data, the team can know if the Arduino
and sensors are working as expected.
Example of prints data and text to the serial monitor:

The first picture shows a text message in the serial monitor when Arduino is testing the
connection with IMUs. Once the test passes, the program proceeds to the second main
function looping the codes in the second picture. In that manner, the data is being
continuously displayed.

Micros () & Millis() Give user time in microseconds or milliseconds


These two are useful codes for time-stamping. To use these two codes, first define time
variables, and then assign the variable with one of the codes. At last, use serial.print to
display timing information in serial monitor.

Arduino working status related code


In final design, we will not have the chance to monitor the serial port, so the team needs to come
up with something else to verify if the Arduino and IMUs are working or not. The way is to
utilize the on-board LED indicator. The idea is to have LED keep blinking if it is reading data.
Example of LED coding:

Define the variables relates to LED and LED statues.


Name pin 13 as LED_PIN, and set the initial state of LED be off.

Add this code in the setup part. Because all the analog and digital pins on Arduino can be either
an output pin or input pin, the mode needs to be chosen before using them. Because the team
wants to send control signal to LED, the pin is used as output pin.

Add this code in the loop. Every time the Arduino capture data from sensors, the on/off statues of
LED changes. PIN 13 is a digital pin, so use digitalWrite to change the output signal of that pin.
Results
This is a result of correct programming. The IMU is not moving at the beginning, thus the
measurement of 9-axis almost stay constant. For the first three values, they stand for acceleration
of xyz axis. Only one of them is large, which makes sense because while holding the IMU one
side up, only y axis experience the earth gravity and have a large acceleration. The other two
axes have a relatively much smaller acceleration.
The gx, gy and gz are the angular speed of three axes; they are close to 0 under a very large
resolution, which is also correct.
As for the last three values, they record the strength of magnetic field on different direction. The
magnetic field changes under different situation with different surroundings. However, these
values are in the range of measurement, and they did change when IMU is moved. It is proved
that the data capturing is working and the programming is correct.

Serial Monitor results:

One thing needs to be noted, while in the serial monitor, choose the same baudrate used in the
code, otherwise, no reasonable results will be shown because it is monitoring other baudrate.
Conclusions and Recommendations
After getting familiar with the arduino software and get all preparation done, the team
successfully does a programming with arduino software. By following all the steps listed above,
the team is able to write a code to have the Arduino read data from sensor, change the format of
data, and display the result in a serial port intuitively. Furthermore, a LED indicator will indicate
the working statue of device.
One aspect of the programming that can be modified is to make the data easier to understand.
Currently, the data is kept as it was; conversion is done in the post processing. If the user does
not understand the meaning of this value, it will be difficult to debug during the programming
development phase. There are two solutions, first, using a newer IMU library which is able to
directly give speed and angle information instead of binary value (or corresponding decimal
value). Second, write those conversion programs directly in the program.
However, there is some disadvantage with this recommendation. While the library is having
more build-in functions, the size of library increases. If the whole program exceeds the 32 Kb
limitations, user cannot upload the whole program to Auduino. To sum up, if the size of program
can be limited under certain value, the team can consider using a more complex library to have
the Arduino deal with more data processing.

Вам также может понравиться