Saving to a SD card#
Requirements
An Arduino (we used an UNO)
A SD card shield
A SD card
Code#
Most of the code for running an Inator already includes code to connect to a SD card.
This intentional, we feel that all Inators should be self describing, having that description in a SD card is generally the best option An example can be found here, which uses the ArduinoJson library
The SD library data logger example that can be installed in the Arduino IDE is a good starting point
#include <SPI.h> //This is a library to use the Serial Peripheral Interface
#include <SD.h> //The library to connect to the SD card
const int chipSelect = 4; //There are several types of SD chip sets available
//This might need changing
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
The key command is the SD.begin. This attempts to communicate with the SD shield, if it fails then it prints to the Serial port that it has failed. Otherwise the programme will continue
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
The code here now opens a file into which it can write. If it is present then it writes dataString to the SD card then closes the file.
The SD card can then be removed and loaded into a PC for analysis.