Jan 16

Print this Post

Data logging with Arduino

Yesterday I gave free rein to the imagination and knowledge of Arduino and I mounted my castle shield to make a data logger with Arduino

data logging shield

The project itself is not complex less than familiar with some concepts such as I2C and SPI, In fact, I decided to use a shield SD Card to write my data to an SD card 64GB, more than sufficient for some line of log in a text file, and an RTC to maintain the time of acquisition of the log.

Proceed in order starting from the material needed for the project data logging with Arduino:

  • 1 Arduino Uno
  • 1 RTC Shield or another based on RTC DS1307
  • n.1 SD Card shield
  • # 5 buttons n.a. ( normally open )
  • # 5 330ohm resistors
  • n.1 breadboard
  • for some cable connections

Defined the material must bear in mind that the RTC used as the communication protocol I2C Bus ie pin SDA ( pin A4 ) and SCL ( pin A5 ) Arduino, Shield some have the possibility to use the 2 additional pins SDA and SCl present on the Arduino Uno R3, however, these pins are connected, level arduino, to the same pin A4 and A5, for which the corresponding Analog inputs are still usable.

The scield SD card using the SPI bus consists of the pins:


in addition to 3.3v and Gnd, some SD card used as pin CS pin 4, is the case of Ethernet Shield Journal Arduino that in addition to the pins 13,12,11 use the pin 4 come CS.

The circuit data logging with Arduino

To connect the Arduino shield, you should not use it if it, as I did, the shield stacked to make your data logging, the only connections to concern 5 buttons that are connected as usual through a pull-down ( to Gnd ) da 330ohm.

The first 4 buttons, use them to simulate the change of data on inputs A0, A1, A2 and A3 and the fifth button will use it to indicate that you want to reload the sketch and read the file on the SD Card, I call it READ button, here is a picture of the buttons

data logging

and one of the connections to the analog inputs

data logging valori analogici

while the fifth button ( Read ) you must connect to pin 9 data logging with arduino

data logging read

Operation of the data logging

The data logging with Arduino is an experiment, so I decided to use the 4 buttons to simulate the change of values ​​detected by the ADC ( analog to digital converter ) Arduino, considers that each value converted by the ADC sull'Arduino One can vary from 0 a 1023 ( 10bit ) and will be 0 when the button is not pressed through a 1023 when you press one of the buttons.

The data logging simultaneously write to the log file and the monitor serial data collected from 4 analog pin in addition to the date and time at which the data was collected.

You can replace each of the 4 buttons with the sensor you see fit, I deliberately used the analog inputs to allow you to be able to detect 1024 signal levels from each pin, if you used the pin digital I could only detect the transition from LOW to HIGH state is and vice versa.


You can not use pins A4 and A5 as they are used for the I2C Bus RTC shield.

Download sketch del data logging

You can download the sketch and the RTC library that I used for this project directly from GitHub.

The sketch of the data logging

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <SD.h>

#define readButton 9
#define CS 10

char buffer[40];

File myFile;

void setup () {
    Serial.print("Initializing SD card...");
    pinMode(CS, OUTPUT);
    pinMode(readButton, INPUT);

    if (!SD.begin(CS)) {
      Serial.println("initialization failed!");
    Serial.println("initialization done.");

    RTC.sqw(1);		//0 Led off - 1 Freq 1Hz - 2 Freq 4096kHz - 3 Freq 8192kHz - 4 Freq 32768kHz
    if (! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
      // following line sets the RTC to the date & time this sketch was compiled
      RTC.adjust(DateTime(__DATE__, __TIME__));

void loop () {
    DateTime now = RTC.now();

    sprintf(buffer,  "%02d/d/%d d:%02d:%02d %d %d %d %d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second(), analogRead( A0 ), analogRead( A1 ), analogRead( A2 ), analogRead( A3 ) );
    Serial.println( buffer );

    myFile = SD.open("dati.log", FILE_WRITE);
    if (myFile) {
    } else {
      Serial.println("error opening dati.log");

    if ( digitalRead( readButton ) == HIGH ) {
        Serial.println(" ");
        Serial.println("Log Reading Saved ----------- ");
        Serial.println(" ");
        myFile = SD.open("dati.log");
        if (myFile) {
          while (myFile.available()) {
        delay( 5000 );


the sketch of the data logging includes both classes needed to communicate with the RTC and those to use the SD Card shield:

lines 03-04: Wire include the class and the class to communicate with the RTC DS1307 through the I2C protocol;

line 05: include the class to read and write to the SD Card with SD SPI protocol;

lines 07-08: define in which pins you have connected the button you will use to re-read the log data logging and pin CS that your SD Card uses;

line 10: initializes the RTC;

line 11: define a buffer in which to memorize the time and the detected values. To calculate the length of the buffer can count the maximum number of characters that may contain, you know that the date is made 10 characters ( gg / mm / aaaa ) and time to 8 characters ( hh:mm:ss ) and that between the two there is a space, then you know that each value is separated by a space to allow you to easily read and the highest value of each pin is 1023 ( 4 characters ) for which the calculation is: 10 + 1 + 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 +4 = 39 for which you can define the buffer 40 ( considering a margin of 1 value )

lines 16-17: sets the communication with the serial monitor 57600 baud and write on the serial monitor the string “Inizializing SD card …”

lines 18-19: sets the operation mode to the CS pin and the pin connected to the button reading ( pin 9 );

lines 21-24: initializes the object with the SD method begin and passing the value of the pin CS, if it fails to initialize the SD Card writes the serial monitor “Initialization failed” and exits the setup function using the command return;

lines 27-34: Wire and initializes the RTC, tax 1 according to the flashing frequency of the SQW LEDs present on the RTC Shield and if time is not configured sets the current time of the RTC Pc, for the procedure read the article Tutorial: RTC Shield con DS1307;

line 38: sets an instance of type DateTime as RTC.now();

line 40: compose the line buffer using the command sprintf( buffer, format, val1, val2 ….walnuts ) where buffer is the variable in which to store the entire string, the format is that of the command sprintf ( see manual ), and subsequent values ​​are the ones you want to buffer;

line 41: write the serial monitor the value of buffer;

line 43: Open the file in write mode “dati.log” on the SD Card data logging

lines 44-46: if the file pointer dati.log there, ie if you are unable to open communication with the SD card inserted in the SD Card Shield write the string buffer and close the file;

lines 47-49: in the event of failure to open a file dati.log write the string on the serial monitor “error opening dati.log”;

line 51: check that the value detected on pin buttonRead is HIGH means that the pushbutton connected to this pin has been pressed;

lines 52-54: write a blank line on the serial monitor, la stringa “Log Reading Saved ————–” and an additional blank line, only serves to separate lines written by the normal operation from those read from the SD Card of the data logging;

line 55: open the file dati.log in read mode, In fact you do not specify any parameters as the second argument of the method open;

lines 56-61: If the file has been successfully opened read line by line the log file and write the result of each line on the serial monitor;

line 62: set a delay 5 second ( 5000 milliseconds ) before returning to normal operation log;

line 65: sets a delay of 1 second between one cycle of the function loop() and the following.

The video data logging with Arduino

In order to show how the data logging you just realized you can watch the video that I made during my test with data logging:

Log good !!!


Kindly NOT paste sketch in the comments, use the info box of the blog that you find on the contact page.


This article was useful?
Support me by clicking on sponsors, I may continue to acquire material to write articles and help you with your projects.

Just one click per day.

Be Sociable, Share!

Related posts

Permanent link to this article: http://www.mauroalfieri.it / electronics / data-logging-with-arduino.html


5 pings

Skip to comment form

  1. Jay

    Great demo. How would you recommend uploading this data to an FTP site so that it could be graphed in a web page?

    1. Mauro Alfieri

      Thank you,
      i suggest you use an Ethernet connection.


  2. Henry

    Congratulations for the tutorial, reading it gave me the inspiration and I would like to create a data logging with one arduino r3 recording data (text strings) coming through serial rs232 by a GPS and an echo sounder on the sd card.
    By connecting the GPS (baudrate 9600) and sounding (baudrate 4800) to the serial port of the PC with hyperterminal see text strings.
    For now I am recording the data with a laptop and a data logging software but rather, as a matter of size and portability, use a solution with arduino.
    Do you think that using the shield Assembled Adafruit Data Logging and two Serial Interface RS232-TTL 3 to 5.5 V is able easily or will I need a different hardware configuration?


    1. Mauro Alfieri

      Hello Henry,
      thanks for the compliments.
      The shield Assembled Adafruit Data Logging has within it both the shield and the RTC SD Card, might be a good solution, I've never tried because I do not own one and I can not tell if it's easier or more difficult to achieve DataLogging.

      You know that Arduino has only one serial that already use to upload sketches, of course you can use it for the data logger when it is separated from the computer, two 2 serial-to-read, at different speeds, you can try simulating the serial library SoftwareSerial.h (see tutorial Blog ) or use a Arduino Mega that has 4 independent serial.


  3. rise

    i want generate arduino data logging based bar code scanner. do have a link?

    1. Mauro Alfieri

      No, I do not have links to suggest


  4. Paul Furley

    Neat project and great write-up, thanks for the detail. I imagine this could be quite a disruptive application as commercial data loggers don’t come cheap. Might be fun to do a radiation level logger with the Geiger counter shield :)


    1. Mauro Alfieri

      Hi Paul,
      you can try a project if you have a counter Gaiger available.


  5. Danny

    Please check line 50. Seems incorrect. Should be:

    if(digitalRead(readButton) == HIGH){

    Good tutorial. Appreciate that you are sharing with us.


    1. Mauro Alfieri

      Thanks for your report, I see no difference between line.


  6. Francis


    thanks to your information I was able to assemble the various components such as LCD, RTC, SD module and temperature sensors. This all works perfectly, but I wanted to make a change to the data logger, I would like the file with the recorded data is created every day with a different name, perhaps with the date of the day of registration (this is. 03_05_2013.txt). Can you kindly help me out?

    1. Mauro Alfieri

      Hello Francesco,
      is a modification of a bit’ challenging but not impossible.
      You can work with the shakes and set a different file name for each day.

      Remember to check that the file exists before writing values, This device prevents the passage of midnight the new file is not present so you should always check that the file exists and you want to write to create it in case there is already


  7. mrss

    Hi there,
    I would like to know your recommendation (since I am very new to Arduino), if I want to log some data, say it I sample it every 1 sec, and when it reaches 60 sec, it will average the 60 data, and log it to the SD card for every 1minute, how am I going to do that? I am very confuse now.

    I manage to log the data every sec. It means in my SD card, every sec of data has been logged. But the problem is now, I only want to log data for every 1 min after averaged all the 60 sec data. Meaning that in my SD card only has data for every 1 minute. Do you understand me? Thank you in advance for your quick response. I am very looking forward for it. Thank you!

    1. Mauro Alfieri

      hello najwa,
      i understand your project.
      You store values ​​into a variable 60 times per minute and average:

      for( int i=0;i<=60;i++) {
      int val = analogRead( your_pin );
      valTotal = val;
      delay( 1000 );
      valTotal = ( valTotal / 60 );

      then write valTotal to the SD card.


      1. mrss

        thank you mauro!
        now i got the idea! thanks!


  8. Trainee

    recently I'm using arduino. My intention was to use the RFID technology. I bought one r3 arduino, a reader ID20 of innovations and OpenLog the sparkfun. I managed to run the RFID reader with Arduino, so I can write about the serial ID read from the reader.
    Now I switched to the second block of my little project: I want to transfer this data to OpenLog in order to save them on microSD. I have read the tips sparkfun, but still can not store the data that I want.
    Since I am a beginner could you give me some help?

    1. Mauro Alfieri

      Hello Apprentice,
      Your project is interesting.
      Your name is not Apprentice, right?

      I do not know you're using the OpenLog, I usually use in my rejection of the normal SD card, However, I read the Wiki Sparkfun and it seems that work on the serial communication simulated thanks to SoftSerial.h with commands sent via Serial.println( command );

      What a mistake by you?


      1. Trainee

        Hello Mauro,
        thanks for reply. I was able to run these days OpenLog with Arduino to write to the serial command Serial.println(). Use OpenLog first in listening mode and then in command mode to write on microSD. My problem now is figure out how to send the data from the reader to OpenLog. I do not understand a thing: agrees that memorize the data leaving the reader into memory first and then pitch them on dell'Atmega OpenLog, or I can directly record to OpenLog defining the Arduino pin? Sorry if the question may seem stupid, but I am stuck here and do not know how to continue.
        Thanks for the help

        1. Mauro Alfieri

          Hello Apprentice,
          I'd love to know your real name .. it's weird to contact a passionate / a with a pseudonym.
          Your questions are not stupid at all, unfortunately I've never tried the shield OpenLog why I rely only on reading the description of the manufacturer.
          To decide which mode it is necessary to write you assess the sampling time, ie the interval between one value and the next you want to store and the time of writing on OpenLog.

          For example, if you need to write on the OpenLog 1 and second samples 100 milliseconds with an interval of 10 seconds each 100 readings you should certainly use an array to be 100 locations to store the data every 100 milliseconds and write OpenLog once in a break 10 second.


          1. Trainee

            Hello Mauro,
            I tried to record with OpenLog: There are successful, I'd rather just do one thing if I can.
            The reader sends via serial to the Arduino that reads tags; I would like to be able to backup such as an ID in RAM dell'ATmega. I've only seen Arduino tutorial on writing and reading EEPROM. The problem is that I can write one cell at a time and I would like to write the entire ID? Do you have any advice? I thought of two pointers one for the ID code and one to scroll the cells of the EEPROM: too cumbersome… You can not write on the ram?



          2. Mauro Alfieri

            Hello Paola,
            I published an article on the management of EEPROM written by a passionate and shared with all.
            It is important you know the size of the ID in terms of numerical domain, you will understand by reading the article.


        2. Trainee

          I just saw you yesterday published a tutorial, thanks :-) , I see a little bit of hacking . let you know if I have problems.

          1. Mauro Alfieri



          2. Trainee

            Hello Mauro,
            ho a problem with the EEPROM. Let me explain what I want to do.
            I saw how you store the ID on a subject EEPROM.Se No ID number,
            how can I print all? I have to use a pointer to catch block in all cells stored and printed?


          3. Mauro Alfieri

            Hello Paola,
            one, I would do a loop that reads the cells and prints it on serial monitor or other communication you is convenient.


          4. Trainee

            Hello Mauro,
            I have resolved to do everything I can now.
            Thanks for the help and the availability

  9. Jon Persson


    Interesting and inspiring project! How is it with energy consumption for this datalogger. I’m looking for a solution that can work autonomous for at least weeks without out needing a huge battery, and logg signals on minute basis.


    1. Mauro Alfieri

      Hello Jon,
      the power consumption depends on the sensor and the frequency of writing on the card, every minute is a good engagement of energy.
      You can measure the current consumed by your project after assembly and determine the type of power source required.


  10. Paul

    Hello Mauro, I can confirm that the sketch of the example works well with the MicroSD Card of’ Ethernet Shield (replacing the CS pin from 10 to the 4, however, indicate how you) e l’ RTC DS1307 … I had errors all'SdCard using file names greater than 8 characters , I confirm that this limit? … suspected to have arrived at the limit of RAM because adding or removing only the 'Serial.print’ I have the success / non-success of the writing on sdcard , sometimes also appear strange characters or the sketch restarts independently, etc.… … can be a problem of employment RAM? there is a simple way to be able to verify?

    1. Paul

      … I was able to read the RAM memory space using the command 'avr-size.exe’ , mi dà :

      Program : 22402 bytes
      Data : 1796 bytes

      If I'm not mistaken the limits of 'Atmega328 Arduino UNO are 32 di kB Flash (Program) else statoLed0 = LOW 2 kB di RAM (Data) … then considering that during the execution of the program the occupation of Ram salt, I seem to be on the verge of Ram and perhaps are explained so many different errors and occasional program …

      can actually be so? how would you rate the comando'avr-size bed. exe '?

      1. Mauro Alfieri

        Hello Paul,
        carry-over concerns that the data flash memory arduino, then that which resides on the sketch compiled.
        32K = 32768 Bytes to which you subtract the 2048 Bytes ( 2K ) get 30720 Bytes.
        These dimensions are the same as that in the IDE Arduino should read at the end of a compilation.


    2. Mauro Alfieri

      Hello Paul,
      the limit on the length of names to 8 character is due to the fact that you have formatted the SD Card in FAT, This filesystem has a limit you're talking about. Try the Fat32 that the site indicates arduino.cc as supported by the library SD.
      Removing the Serial.print from your sketch you decrease the size occupied by the sketch when completed, we speak of flash memory in Arduino one is 32K, has no influence on the development of the sketch.


      1. Paul

        … I just reformatted (in fast mode) microSdCard the da 4 GB in FAT32 con 32 kilobytes of allocation units and, using a sketch base for writing on sdcard (so as to avoid any problems of memory), writing works well until the eighth character of the filename, from the ninth character in the writing then fails … anyway thanks, for the moment rest under the 8 characters, and I can tell better about formatting

        To be fair I am giving you the link to the site from which I have drawn information about the memory of Arduino :


        I do not know the author and see if tù then delete this link … it is from this article that I am convinced of the possibility that only a few Serial.print can lead to saturate the RAM …

        Thanks dela availability …

        1. Mauro Alfieri

          Hello Paul,
          supports the FAT32 file whose name is longer than the 8 characters, could be a limit of the library SDFat.
          The link you sent me is a very good programmer and passionate arduino, Leonardo Milani, with whom I exchanged some emails a few months ago and which I have a lot of respect.

          His article, I read carefully, specifies that the command avr-size.exe takes care to give an assessment of the occupation of Flash evaluating the compiled file from the IDE Arduino.
          To get a truthful assessment of the RAM used at run-time ( ie while it's running sketch ) you should use the method suggested by Leonardo MemoryFree.


          1. Paul

            ok, the upper limit of 8 characters of the file should be a limit of the library, I currently use the SD.h

            I tried the method MemoryFree and effectively to a value of 450 (suppongo Bytes) the file is successfully opened the sdcard, Serial.print add some plain text, and the value drops to 350 continuing to successfully open the file, when instead add another pair of Serial.print and the value of MemoryFree drops below 300 I get an error when opening the file to the SD … at this point I really think are the Serial.print that I occupy the Ram up to disallow opening the file … I will be more thrifty with Serial.print and indeed will assess whether to move in flash memory …

            Thanks again Mauro

          2. Mauro Alfieri

            Great Paul, your analysis is very detailed, and the library MemoryFree seems good for these tests.


  11. Nicola

    Hi Mauro.
    Following the instructions in this guide His excellent ono able to measure the temperature and humidity values ​​and save the data to an SD.
    the upgrade I want to do is to make these measurements not every few seconds, with the wait command,
    but will automatically, for example at the top of every hour.
    Could you please suggest me how to proceed?

    1. Mauro Alfieri

      Hello Nicola,
      the idea is very cute, I invite you to read the articles on sentralina of irrigation which has similar concepts.
      Compared to your need, the irrigation control unit triggers a relay to a date now set, you will not have to do is write to the SD card instead of energize a relay.


      1. Nicola

        First of all, thank you very much for your prompt response.
        But it perplessità: the example of the control unit is great, but I see it more suitable for those activities that require some adjustment, because you can easily change service intervals.
        In my case, should establish 24 intervals, every hour of the day, am I right?
        Can not quite, with more comfort, to use a command like the wait,
        but that is not influenced by the time of reading and writing data?

  1. Arduino Blog » Blog Archive » Data-logging made simple with Arduino

    [...] translate ideas into physical artifact, as practically demonstrated by Mauro, which shows on his blog how to build a simple data-logger by properly combining different shields. By using few additional [...]

  2. Data-logging made simple with Arduino | Linux-Support.com

    [...] translate ideas into physical artifact, as practically demonstrated by Mauro, which shows on his blog how to build a simple data-logger by properly combining different shields. By using few additional [...]

  3. Press - 28 January 2013 |

    [...] complete description of a datalogger based on Arduino. It is rare to see such a stack of shields! The program, simple, allows [...]

  4. Arduino project where to start - Mauro Alfieri Electronics Robotics

    [...] and its blink you'll probably need at least one shield arduino, an example is the data logger you need to make it happen, besides arduino, la RTC shield e la SD shield. The same goes for a [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>