ArduinoMicrocontroller Software Development

The microcontroller (MCU) used throughout this project is the Arduino Mega, more precisely, the ATmega1280. It has several very attractive features, such as 54 digital IO pins and 16 analog pins. The MCU runs at 16 MHz, and has an ICSP header used to interface to externaldevices, such as the micro-SD shield by Libelium.

Summary

Microcontroller:Arduino Mega (ATmega1280)

Operating Voltage:5V

Input Voltage (recommended):7-12V

Input Voltage (limits):6-20V

Digital I/O Pins:54 (14 provide PWM output)

Analog Input Pins:16 (10-bit accuracy)

DC Current per I/O Pin:40 mA

DC Current for 3.3V Pin:50 mA

Flash Memory:128 KB (4 KB used by boot loader)

SRAM:8 KB

EEPROM:4 KB

Clock Speed:16 MHz

The MCU is designed so that the 16analog inputs are divided across two ADCs, each with eight channels. This makes for a total of 16 A/D channels; two of which can be sampled simultaneously, which can increase the sample rate for a particular application.

Data Storage

Local storage was an optimal solution to achieve portability of design, and the technology of choice was micro SD. A micro-SD shield developed by Libelium was used. The micro-SD shield interfaces with the MCU directly via the ICSP header, using the I2C interface.

Several libraries exist to support SD storage, some of which are:

microfat

SD_FAT16lib

SD_FAT32lib

SD_GCC_V2.2_M32

SdFat

Each of these libraries were tried; although most of them were advertised as load and play, none of them worked. Most were very poorly documented. The biggest problem was that these libraries had issues initializing the card. Some of them required that a previously, manually created file be alreadyon the card, with a specified size and ending characters. It was obvious that this setup would not work wellwhen trying to log data into a CSV file, since it would have a different format and irregularly variable sizes.

The library that performed best was SdFat, which needed a few tweaks in the main source files: Sd2Card.cppand Sd2Card.h. Afterward, the card initialization was successful and the MCU could then be programmed to store data on the micro SD card. Thelogging is very robust, as the card does notneed any special treatment. It may be pulled out and put back into the socket, and logging will resume creating a new file.

The only requirement for this process to work is that the card needs to be pre-formatted (by a computer) as FAT (FAT16)before use.

The main Arduino source file is: SdFatAnalogLogger.pde. This file logs the ADC values on the specified channels (0-15) and stores them in a comma-separated values (CSV) file on the micro-SD card. The CSV file is plain-text, which is readable by spreadsheet programs such as Excel, as well as any standard text editor. This makes it easy to analyze the data using programming languages such as MATLAB or other software.

The first line of the CSV file is the header line, which contains the titles for each of the columns. Each subsequent line contains the data. On each data line, the first value is the timestamp, which is the number of milliseconds that have passed since the Arduino was powered on. The subsequent values are the digitized values (stored in decimal) of the analog input on that channel. In order to analyze the data, it is converted back into voltages using the C++ program decimal_to_voltage.

How to Use This Software

First, setup the Arduino Mega MCU and Micro-SD shield shown in Figure 1 and Figure 2.

Figure 1. Arduino Mega MCU
Figure 2. Arduino Mega MCU with Libelium Micro-SD Shield attached. Note the short between pin SS of the micro-SD shield and pin 53 of the Arduino! Also, it is important to note that the voltage jumper on the microSD shield must be set to 3.3V

Figure 2 shows the Arduino Mega connected to the micro-SD shield via the ICSP header. Note that the correct orientation is such that the micro-SD slot is covering the lower 8 ADC channels (unfortunately). For correct operation, a jumper wire (short) must connect pin SS of the micro-SD shield with pin 53 of the Arduino Mega. Also, the power selection jumper on the micro-SD shield must be set to 3.3V. The default of 5V will not work.

Next, download the latest version of the Arduino IDE, found on the Arduino website:

Connect the Arduino Mega to a computer. It may ask for drivers; if so, just point to the Arduino IDE folder -- C:\arduino-0018\drivers.

Next, obtain the latest copy of the SDAnalogLogger code off SVN/EDGE for project P10010. This should be located as follows:

Copy the entire SdFat folder into the Arduino IDE libraries folder. For example, let the Arduino IDE be located: C:\arduino-0018. The libraries folder is: C:\arduino-0018\libraries. The SdFat library should be located as follows: C:\arduino-0018\libraries\SdFat.

Next, open the SdFatAnalogLogger.pde using the Arduino IDE. There are several variables that can be changed according to the application; the key ones follow. The sample rate is defined by the LOG_INTERVAL, which specifies the number of milliseconds between samples. The SYNC_INTERVAL specifies the number of milliseconds between SD card synchronize events. A synchronize event essentially flushes the write buffers and forces a write to the card. The SYNC_INTERVAL should not be set too fast, otherwise it will adversely affect write performance.

At approximately line 34, the Serial Monitor's baud rate may be specified. By default, this value is set to 115200 to maximize throughput. When using the serial monitor, be sure to set the same baud rate in the Arduino IDE as specified on . The next key thing to note is around line 116. Here, one may specify the starting and stopping ADC channels for sampling.

Before compiling and loading the program on the Arduino, one must specify the correct Arduino board and port in the Arduino IDE. Specify the Arduino Mega for the board by selecting Tools --> Board --> Arduino Mega from the menu. Next, specify the port by selecting Tools --> Serial Port --> COMXX. It may not be obvious at first which port is the Arduino. Trial and error may be the best method for determining the correct port.

Compile and load the project by clicking the icon that shows an arrow pointing to the right. Alternatively, select File --> Upload to I/O Board....

Once completed, click on the Serial Monitor button (far right on the tool bar). Ensure the correct baud rate is set (see line ~34 in SdFatAnalogLogger.pde) . The serial monitor will then open a serial connection to the Arduino. The code that is loaded on the MCU will trigger on this event and will wait for the user to press any key to start logging. Logging is stopped when the Serial Monitor is closed. Logging may be resumed again (which creates a new file) by re-opening the Serial Monitor and repeating the process.

The data may then be moved to a computer by manually disconnecting the microSD card and inserting it into a computer's media card reader (a micro-SD to full-size SD adapter may be required). The data logged will be contained in files labeled LOGXXX.CSV. The CSV files are plain-text and may be edited by any number of programs, including Excel.

To convert the digital values back into voltages, the decimal_to_voltage.exe may be necessary. This program is also located on EDGE/SVN:

This program is run by calling its executable under the Release folder.

Usage: decimal_to_voltage.exe <input_file.csv> [output_file.csv]

<input_file.csv> comma separated value file to be converted to VOLTAGE

[output_file.csv] optional output file -- if not specified: data_out.csv

An example input file, test_in.csv is provided for a reference.