SD Card Data Logger With LinkitOne

by KedarP1 in Circuits > Microcontrollers

954 Views, 8 Favorites, 0 Comments

SD Card Data Logger With LinkitOne

C360_2015-12-17-15-09-48-901.jpg

In this tutorial, we will see how to use the built-in SD card functionality in the super awesome MediaTek LinkitOne!

The LinkitOne offers 10MB on board flash storage and up-to 32GB expandable storage via the microSD card slot onboard.
Here we will be using the SD card slot to store data collected using the Grove Temperature sensor.

IoT is all about connected applications. But offline data storage can be used where connectivity isn't available. Large amounts of data collected can be analyzed using R or any other analytics software of your choice.

What You'll Need?

C360_2015-12-16-23-19-52-341.jpg
5671a35850e1b62d7a0014db.jpeg
C360_2015-12-17-15-09-48-901.jpg

This is more of an intro to SD card logging. So even though I am using the temperature sensor provided in Grove here, you can make use of any analog sensor of your choice. I have included code for a generic SD card data logger later.

Components:
1. MediaTek LinkitOne Board
2. DHT 22 Temperature Sensor
3. MicroSD Card

Setup

C360_2015-12-16-23-08-24-904.jpg

The setup here is pretty easy. Just connect the temperature sensor either to the onboard Grove slots or through the Grove shield. As you can see here, I have connected it to D2 on the Grove shield.

Secondly, make sure to insert the SD card to the SD card slot on the LinkIt One.

And you're good to go!

Programming

The programming is fairly easy as well. We make use of the LStorage library for the SD card functionality.
Also include the DHT library for the sensor.

Code: 

#include "DHT.h"
#include "LTask.h"
#include "LFlash.h"
#include "LSD.h"
#include "LStorage.h"

#define Drv LSD // use SD card
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while(!Serial.available()); // input any thing to start
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
LTask.begin();
Drv.begin();
Serial.println("card initialized.");
dht.begin(); //initialize sensor library
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";
float t = 0.0;
float h = 0.0;
int temp;
if(dht.readHT(&t, &h))
{
temp = (int)t;
dataString += String(temp);
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
LFile dataFile = Drv.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");
}
delay(1000);
}

Generic Logging

Here is the code for a generic analog sensor data logging as promised

/*SD card datalogger
This example shows how to log data from three analog sensorsto an SD card using the LSD library.*/
#include "LSD.h"
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial);
// wait for serial port to connect.
Serial.print("Initializing SD card...");
LSD.begin();
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 14; analogPin < 17; analogPin++)
{
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 16)
{
dataString += ",";
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
LFile dataFile = LSD.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");
}
}