high speed data logging

i want to log my data includes temperature and humidity very fast like 50 data/sec. i am using arduino uno and a SD card module that connects to micro with spi protocol. in each loop i also measure the time of the loop and normally a loop time is 18 millisecond. but sometimes a loop last long for about 100 or 200 millisecond. and this peaks in the logging process destroy my sample rate i do not know how to fix it.
i wrote the time per loop in sd card and plot the data via matlab and u can see how it works in the attachments. my problem is those random peaks that u can see in plot and i want it to be eliminated .
the y axis is time (second) and the x axis is sample number.

What sort of humidity sensor responds that quickly?

(Did you forget to post something?)

the problem is not with the sensor. i examined the module with generating 10 random numbers in each loop and writing it to sd card and i saw these peaks again. the problem is with the module i guess

Your code is doing something, periodically, that messes up the loop time. That is about all that I can say without seeing the code.

Please read the "how to use this forum-please read" sticky to see how to post code.

How to post images.

What sort of humidity sensor responds that quickly?
I'm genuinely curious.

This is my code that generate 10 random numbers per loop and write it to sd card for 5 minutes. if u see the data written to timerdata file and plot it u would see the peaks in time per loop.

// (c) Michael Schoeffler 2016, http://www.mschoeffler.de
#include <SD.h> //Load SD library
int chipSelect = 4; //chip select pin for the MicroSD Card Adapter
int number[10] ;
int loop_counter;             //holds the count for every loop pass
long loop_timer_now;          //holds the current millis
long previous_millis;         //holds the previous millis
float loop_time;              //holds difference (loop_timer_now - previous_millis) = total execution time
int loop_test_times = 1;  //Run loop 20000 times then calculate time
int counter=0;
long t ;

File file; // file object that is used to read and write data
File timerdata;
void setup() {
  Serial.begin(9600); // start serial connection to print out debug messages and data
  pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
  if (!SD.begin(chipSelect)) { // Initialize SD card
    Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
  }
  
  if (SD.exists("file.dat")) { // if "file.dat" exists, fill will be deleted
  //  Serial.println("File exists.");
    if (SD.remove("file.dat") == true) {
 //     Serial.println("Successfully removed file.");
    } else {
  //    Serial.println("Could not removed file.");
    }
  }
   if (SD.exists("timer.dat")) { // if "file.dat" exists, fill will be deleted
  //  Serial.println("timer data exists.");
    if (SD.remove("timer.dat") == true) {
 //     Serial.println("Successfully removed timer data.");
    } else {
  //    Serial.println("Could not removed timer data.");
    }
  }
  file = SD.open("file.dat", FILE_WRITE); // open "file.dat" to write data
  timerdata = SD.open("timer.dat", FILE_WRITE); // open "timer.dat" to write data
}
void loop() {
 
  if (counter >= 100)
    counter=0;

  for (int i=0 ; i<10 ; i++){
     number[i] = counter + random(10); 
     if (file) {
        file.print(number[i]); // write number to file
        file.print("  ");
       }
   // Serial.print("time: "); // debug output: show written number in serial monitor
    //Serial.println(number);
      else {
        Serial.println("Could not open file (writing).");
       }
  }
  counter = counter+10;
  file.print("\n");

  
 /* file = SD.open("file.dat", FILE_READ); // open "file.dat" to read data
  if (file) {
    Serial.println("--- Reading start ---");
    char character;
    while ((character = file.read()) != -1) { // this while loop reads data stored in "file.dat" and prints it to serial monitor
      Serial.print(character);
    }
    file.close();
    Serial.println("--- Reading end ---");
  } else {
    Serial.println("Could not open file (reading).");
  }*/
  loop_counter++;
  if (loop_counter == loop_test_times)
  { previous_millis = loop_timer_now;
    loop_timer_now = micros();
    loop_counter = 0;
    loop_time = (loop_timer_now - previous_millis) / 1.0; 
    timerdata.println( loop_time);
    
  }
  t=millis(); 
  if ( t > 300000){
    Serial.println("job finished");
    file.close();
    timerdata.close();
    while (1){
      
    }
  }

}

AWOL:
What sort of humidity sensor responds that quickly?
I'm genuinely curious.

its a Air Management Unit (AMU) module .

Hi,
You can generate 10 random numbers as much as you like but have you looked at the spec of the sensors and how quickly they send data?

Can you post links to data/spec of the sensors you are using please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
You can generate 10 random numbers as much as you like but have you looked at the spec of the sensors and how quickly they send data?

Can you post links to data/spec of the sensors you are using please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

hi
i know that i can generate random numbers as much as i can.but my problem is the sd card adapter cant even write these random numbers that generates easily with a stable rate and the writing speed varies between 15 millisecond and 200 millisecond. and this problem makes it hard for me to use this module.