Arduino Yún Projects: Datalogging


Datalogging is the process of collecting data over a period of time, and saving that data for further examination. For example, this process could be very useful for measuring the conditions in a greenhouse. You could potentially have 3 sensors in a greenhouse, one for measuring light levels, one for measuring the soil moisture of a plant and one for measuring the temperature.

If you logged this data over a week and checked the results, you could see if you were watering your plants frequently enough, if they were getting enough light and if the temperature was suitable for plant growth. You could then use these results to ensure the plants are subjected to the right conditions for growth.

You can actually buy dataloggers from sites like eBay and use them instead, but if we build it ourselves we can fully customize them with as many sensors as we like!

So, yeah. I’m going to collect data from various sensors using an Arduino and log the data to an SD card, because why not? Let’s get started.

In this Tutorial

  1. What you will need
  2. Wiring
  3. Coding (line by line explanation of the official Arduino datalogging code)
  4. Conclusion

1. What You Will Need

For this project, you will need more than just your standard Arduino UNO board. You’ll need the legendary Arduino Yún (or Yún mini or Yún shield).  You can use an SD card shield but may need to tweak the code slightly.

You need some sensors as well, just 3 will do, that we can record data from.


2. Wiring

Wiring is very simple for this project, just 3 sensors on Analog Pins 0, 1 and 2.

An example of how a sensor can be wired is below.


For my datalogging project, I wired up a soil moisture sensor, temperature/humidity sensor and a photoresistor.


3. Code Explanation

This code is part of the examples section of the Arduino IDE. Go to File -> Examples -> SD -> Datalogger.

#include <SPI.h>
#include <SD.h>

We need to use two libraries for this project: the SPI library and the SD library. The SPI library allows the Arduino to communicate with one or more peripheral devices quickly over short distances. This allows us to communicate with the SD card.

The SD library allows us to write to the SD card directly from the Arduino. We are able to create a file, log data from the sensors, and close the file.

const int chipSelect = 4;

Create a variable with the location of the SD card. This is a constant, because this variable will not change.

void setup() {

In the void setup function, which will only run once, we initialize the serial monitor at the standard rate of communication, 9600. We are using the serial monitor in this project so that we can see the data being logged as it happens.

while (!Serial) {
Serial.print("Initializing SD card...");

When we have initialized the serial monitor, we then need to wait for the serial port to connect. The ‘!Serial’ means no serial connection. So while there isn’t a serial connection, the program will be halted and no code will run.

When it has connected, we tell the user by printing ‘Initializing SD card…’ to the serial monitor

if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
Serial.println("card initialized.");

If there is no connection present with the SD card, the program will stop there as it cannot continue without a connection. The program is stopped with the return function. However, if there is not a connection problem, the program continues as normal and tells the user by printing ‘card initialized’.

void loop() {
String dataString = "";

Now we’re moving into the void loop – this will run continuously and won’t stop until we tell it to. To start off with, we need to declare a string. We can then put the data into this string later in the program.

for (int analogPin = 0; analogPin < 3; analogPin++) {

We now need to iterate over the analog pins. We start off my declaring the variable ‘analogPin’ that is equal to 0. We know that this loop will continue until the analogPin is equal to 3, as we have put in the for loop after declaring the variable ‘analogPin < 3’.

So, this would run forever if the analogPin was just set to 0, but It’s the last bit that means that it doesn’t. Each time the for loop repeats itself, analogPin increases by one. The last bit ‘analogPin++’ means exactly the same as ‘analogPin = analogPin + 1’, meaning the for loop (the following bit of code) will run 3 times; while analogPin is equal to 0, 1 and 2.

int sensor = analogRead(analogPin);

A new integer variable is created to read the data from the analogPin. This means we have a way of storing whatever data is received through whichever analogPin we are reading from.

dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";

This bit utilizes the dataString variable we made earlier. It adds whatever value is being read from the sensor to the end of the variable. So, when analogPin is set to pin zero and we are reading from that, it adds that to the variable. Then, when analogPin is set to one, it adds that value to the end of the same line. Finally, when it’s set to two, it adds that to the end of the same line. This gives us one complete line with the data from all three analog sensors.

To make the logged data easier to read, it automatically adds a comma in between each set of data. It only adds a comma to the first 2 sets of data, as we don’t need a comma at the end of the line.

This bit of code will give us a line of text like the one below to the SD card.

1023, 467, 936

File dataFile ="datalog.txt", FILE_WRITE);

This will create a text document on the SD card called ‘datalog.txt’

if (dataFile) {

If the file is open and ready to be written to, write to it the line of data with the values from the sensors that we just created. Also, print this line to the serial monitor so that you can see the live data from the sensors.

else {
Serial.println("error opening datalog.txt");

If the file is not open, give an error message.

4. Conclusion

I left this program running for just over 10 minuites with a few sensors hooked up, and this  is what was written to my SD card:

01/09/17-17:35:12 = 1023,10,156
01/09/17-17:35:27 = 1022,10,160
01/09/17-17:35:42 = 1022,9,160
01/09/17-17:35:57 = 1023,10,160
01/09/17-17:36:13 = 786,10,159
01/09/17-17:36:28 = 1022,2,155
01/09/17-17:36:43 = 1022,10,160
01/09/17-17:36:58 = 1022,9,159
01/09/17-17:37:13 = 1022,9,158
01/09/17-17:37:29 = 1022,10,160
01/09/17-17:37:44 = 1022,10,160
01/09/17-17:37:59 = 1022,10,159
01/09/17-17:38:14 = 996,10,159
01/09/17-17:38:29 = 1017,74,192
01/09/17-17:38:44 = 1017,76,193
01/09/17-17:39:00 = 1017,19,164
01/09/17-17:39:15 = 1016,20,163
01/09/17-17:39:30 = 1017,18,163
01/09/17-17:39:45 = 1017,17,162
01/09/17-17:40:00 = 1017,17,161
01/09/17-17:40:15 = 1017,18,162
01/09/17-17:40:30 = 763,17,161
01/09/17-17:40:46 = 579,17,161
01/09/17-17:41:01 = 559,17,160
01/09/17-17:41:16 = 569,18,161
01/09/17-17:41:31 = 577,17,160
01/09/17-17:41:46 = 577,17,161
01/09/17-17:42:01 = 572,17,160
01/09/17-17:42:17 = 326,20,161
01/09/17-17:42:32 = 348,19,161
01/09/17-17:42:47 = 978,19,161
01/09/17-17:43:02 = 997,18,162
01/09/17-17:43:17 = 999,18,161
01/09/17-17:43:33 = 1003,19,161
01/09/17-17:43:48 = 1007,19,161
01/09/17-17:44:03 = 1006,18,161
01/09/17-17:44:18 = 852,18,161
01/09/17-17:44:33 = 839,18,161
01/09/17-17:44:48 = 809,18,160
01/09/17-17:45:03 = 771,18,160
01/09/17-17:45:19 = 723,18,160
01/09/17-17:45:34 = 668,17,160
01/09/17-17:45:49 = 647,17,160
01/09/17-17:46:04 = 616,17,159
01/09/17-17:46:19 = 603,18,160
01/09/17-17:46:34 = 608,18,160
01/09/17-17:46:49 = 604,17,160
01/09/17-17:47:05 = 604,18,160
01/09/17-17:47:20 = 606,18,160
01/09/17-17:47:35 = 652,18,160

Well, there we have it, a basic way of recording data from sensors with an Arduino board. I’m soon going to configure the Yún so that it uploads the data to Temboo and creates real-time graphs and stuff like that.

Thanks for reading this tutorial. If you have any questions or suggestions, put them in the comments section. Also, why not follow my blog by email and get alerts whenever I upload?

Further Reading

Arduino Yún first setup guide

Arduino Yún review


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s