Arduino Yún Yún Datalogger

Store sensor information on a SD card.

This example shows how to log data from three analog sensors to an SD card or to a USB Flash memory stick using the Bridge library. The memory is not connected to the microcontroller, but the AR9331, which is why Bridge must be used.

Prepare your memory by creating an empty folder in the root directory named "arduino". When OpenWrt-Yun finds this folder on an attached storage device, it creates a link to the memory to the "/mnt/sd" path.

You can remove the Flash memory while Linux and the sketch are running but be careful not to remove it while data is writing to the card.

Hardware Required

  • Yún board or shield

  • micro-SD card or USB Flash memory stick

  • analog sensors attached to analog input pins 0, 1, and 2

Circuit

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

Include the FileIO header, for communicating with the SD card.

#include <FileIO.h>

in

setup()
, initialize Bridge, Serial communication, and FileSystem (for communicating with the OpenWrt-Yun file system). Wait for an active serial connection before starting the remainder of the sketch.

1void setup() {
2
3 Bridge.begin();
4
5 Serial.begin(9600);
6
7 FileSystem.begin();
8
9 while(!Serial);
10
11 Serial.println("Filesystem datalogger\n");
12}

In

loop()
, create a string that starts with a timestamp to organize the data to be logged. You'll create the
getTimeStamp()
function below.

1void loop () {
2
3 String dataString;
4
5 dataString += getTimeStamp();
6
7 dataString += " = ";

Read the data from the sensors and append them to the string, separating the values with a comma :

1for (int analogPin = 0; analogPin < 3; analogPin++) {
2
3 int sensor = analogRead(analogPin);
4
5 dataString += String(sensor);
6
7 if (analogPin < 2) {
8
9 dataString += ",";
10
11 }
12
13 }

Open the file you'll be writing the data to using a

File
object and
FileSystem.open()
. With the modifier
FILE_APPEND
, you can write information to the end of the file. If the file doesn't already exist, it will be created. In this case, you'll be creating and writing to a file at the root directory of the SD card named "datalog.txt".

1File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);

If the file opens successfully, write the string to it, close the file, and print the information to the Serial monitor.

1if (dataFile) {
2
3 dataFile.println(dataString);
4
5 dataFile.close();
6
7 Serial.println(dataString);
8
9 }

If there is a problem opening the file, send an error to the Serial monitor :

1else {
2
3 Serial.println("error opening datalog.txt");
4
5 }
6
7 delay(15000);
8}

Last, you'll write the function

getTimeStamp()
to retrieve the time the information was read. It will be returning a string. First, create a string to hold the current time. You'll also create an instance of Process called "time". start the process and call the "date" application. "date" is a command line utility that returns the current date and the time. Using
time.addParameter()
, you'll specify the parameters D and T, which will return the date (mm/dd/yy), and the current time (hh:mm:ss). Run the process and read the result into the string.

1String getTimeStamp() {
2
3 String result;
4
5 Process time;
6
7 time.begin("date");
8
9 time.addParameter("+%D-%T");
10
11 time.run();
12
13 while(time.available()>0) {
14
15 char c = time.read();
16
17 if(c != '\n')
18
19 result += c;
20
21 }
22
23 return result;
24}

The complete sketch is below :

1/*
2
3 SD card datalogger
4
5 This example shows how to log data from three analog sensors
6
7 to an SD card mounted on the YunShield/Yún using the Bridge library.
8
9 The circuit:
10
11 * analog sensors on analog pins 0, 1 and 2
12
13 * SD card attached to SD card slot of the YunShield/Yún
14
15 Prepare your SD card creating an empty folder in the SD root
16
17 named "arduino". This will ensure that the Yún will create a link
18
19 to the SD to the "/mnt/sd" path.
20
21 You can remove the SD card while the Linux and the
22
23 sketch are running but be careful not to remove it while
24
25 the system is writing to it.
26
27 created 24 Nov 2010
28
29 modified 9 Apr 2012
30
31 by Tom Igoe
32
33 adapted to the Yún Bridge library 20 Jun 2013
34
35 by Federico Vanzati
36
37 modified 21 Jun 2013
38
39 by Tom Igoe
40
41 This example code is in the public domain.
42
43 http://www.arduino.cc/en/Tutorial/YunDatalogger
44
45 */
46
47#include <FileIO.h>
48
49void setup() {
50
51 // Initialize the Bridge and the Serial
52
53 Bridge.begin();
54
55 Serial.begin(9600);
56
57 FileSystem.begin();
58
59 while (!SerialUSB); // wait for Serial port to connect.
60
61 SerialUSB.println("Filesystem datalogger\n");
62}
63
64void loop() {
65
66 // make a string that start with a timestamp for assembling the data to log:
67
68 String dataString;
69
70 dataString += getTimeStamp();
71
72 dataString += " = ";
73
74 // read three sensors and append to the string:
75
76 for (int analogPin = 0; analogPin < 3; analogPin++) {
77
78 int sensor = analogRead(analogPin);
79
80 dataString += String(sensor);
81
82 if (analogPin < 2) {
83
84 dataString += ","; // separate the values with a comma
85
86 }
87
88 }
89
90 // open the file. note that only one file can be open at a time,
91
92 // so you have to close this one before opening another.
93
94 // The FileSystem card is mounted at the following "/mnt/FileSystema1"
95
96 File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
97
98 // if the file is available, write to it:
99
100 if (dataFile) {
101
102 dataFile.println(dataString);
103
104 dataFile.close();
105
106 // print to the serial port too:
107
108 SerialUSB.println(dataString);
109
110 }
111
112 // if the file isn't open, pop up an error:
113
114 else {
115
116 SerialUSB.println("error opening datalog.txt");
117
118 }
119
120 delay(15000);
121
122}
123
124// This function return a string with the time stamp
125
126String getTimeStamp() {
127
128 String result;
129
130 Process time;
131
132 // date is a command line utility to get the date and the time
133
134 // in different formats depending on the additional parameter
135
136 time.begin("date");
137
138 time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
139
140 // T for the time hh:mm:ss
141
142 time.run(); // run the command
143
144 // read the output of the command
145
146 while (time.available() > 0) {
147
148 char c = time.read();
149
150 if (c != '\n') {
151
152 result += c;
153
154 }
155
156 }
157
158 return result;
159}

Last revision 2016/05/25 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.