I want to classify my data from 3 sensors with arduino so that every time i enter the specific date or hour, or also it can be a period of time like 10 days ago, it shows me the exact data. In other word i want to classify my data so that whenever i want every detail of data, i would be able to get it by entering the information of year,date,time. I also didn't want my data to be in separate files and time stamps. I want to be able to do that within one file and search inside that file...

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

RTC_DS1307 rtc;
const int chipSelect = 10;

File dataFile;

String print_time(DateTime timestamp) {
  char message[120];

  int Year = timestamp.year();
  int Month = timestamp.month();
  int Day = timestamp.day();
  int Hour = timestamp.hour();    
int Minute = timestamp.minute();
      int Second= timestamp.second();

      sprintf(message, "%d-%d-%d %02d:%02d:%02d", Month,Day,Year,Hour,Minute,Second);

      return message;

    void setup(){

      pinMode(chipSelect, OUTPUT);
      if (!SD.begin(chipSelect)){
        Serial.println("Error: SD card would not initiate.");

      if (!rtc.isrunning()){
        Serial.println("Clock is not running");

      dataFile = SD.open("log0.csv", FILE_WRITE);
      if (!dataFile){
        Serial.println("Could not open file.");


    void loop(){
      Datetime now = rtc.now();

  • 1
    Does this answer your question? sending specific date's data from data logger shield to serial port – Juraj Dec 9 '19 at 10:29
  • so this ^ is the new automatic comment for "possible duplicate" – Juraj Dec 9 '19 at 17:24
  • @Juraj the question you suggested is different from this one. What i mean here is for example a file with 1000 saved data. lets say you want the data from december 2, 10AM untill now. this is the main question... the ability to classify the data and ask the time of saved data. Here i should write a code so that my program understands the TIME i ask it... – 201 Dec 10 '19 at 8:54

If you plan to continuously take data at a fixed rate, I suggest you don't timestamp the individual records. Instead, store the initial timestamp and the data sampling period in a binary header, followed by fixed-size binary records. Then, when you want to retrieve the record for time t, you can fseek() right to

uint32_t offset = header_size
                + record_size * (t - initial_time) / sampling_period;

If there is a slight chance that you may fail to record some data points, you may want to have a boolean in each record that tells whether the data is valid. You can then fill the missing data with invalid records.

If your Arduino is likely to be turned on and off unpredictably, then timestamping every record may be a better option, but finding a specific record will likely be very slow.

  • Upvoted your answer, indeed if the data comes continuously and at a fixed rate, your solution is better. My solution I assumed data from the sensors could come randomly and possibly unevenly spread. – Michel Keijzers Dec 9 '19 at 11:01
  1. Create a single file (SD logger?)
  2. When data is received from a sensor, store it in the file, and store this information:
    1. Date/Time stamp
    2. Which sensor
    3. Sensor data
  3. When a request for data is received (probably containing start and end time stamps, parse through the file, and start returning information for those date/time stamps within the requested start/end time stamps.

If step 3 takes too long (and results in missing new sensor measurements), you have to do the read in smaller steps between the reading of sensors.

  • That's the part i don't understand. How should i write the code... sorry I'm new with arduino... your theory for writing the code is completely what i was thinking but i have trouble writing it... – 201 Dec 10 '19 at 8:59
  • Split your problem up into smaller pieces (like I showed above. Maybe it's even better to first try the file part on a PC where you have easy debug facilities without using an MCU. Thus try to write a file, add bytes to it pretendin they come from sensors. When you have that working add the MCU specific parts (reading sensor data and use the SD library from the Arduino instead). To write all the code is a bit out of scope for this forum (too broad too). – Michel Keijzers Dec 10 '19 at 9:46
  • I wrote a code with timestamp and data. Now can you help me with the next part? How can i ask the arduino to send me the timestamp i want? I uploaded my code. Now i ask the arduino to send me the last 10 timestamps.... any suggestions? – 201 Dec 10 '19 at 12:53
  • You should add the code to the question, not as answer. How you want to 'ask it to the Arduino' depends on what you want … you want to add a keypad, or a set of switches, or a keyboard of some type to the Arduino, or the serial port or another way (WIFI, USB)? I don't know your requirements. – Michel Keijzers Dec 10 '19 at 13:19
  • Thank you for the guide. I added the code to my question... I want to ask the arduino through bluetooth module but i don't know the related code for this purpose... What particular code reads the timestamp for me? – 201 Dec 10 '19 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.