SD Card: losing data every 103 samples

I’m working on a system of acquisition and ECG datalogging (Holter ECG device) with the Arduino, and I’m having the following problem:
I’m losing data every 103 points (just 103). No matter the sampling frequency to use, every 103 points, some points are lost, damaging the signal sampling. It could be the recording time on the SD card, but even when the SD.close () command is run only once, the data is lost. Here is a simple test with a 100 Hz sine wave at the entrance.

hat could it be? Speed analogRead ()? Problem on the SD card?

Here’s the code I’m using to record the signal, it’s a simple test code:

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

int CS = 4;
int ECG_PIN = 1;
int voltage;

int index = 0;
int RECORD_TIME = 20000;
File logFile;

void setup(){
  pinMode(CS, OUTPUT);

  logFile ="ECG_log.txt", O_CREAT | O_WRITE);


void loop(){
  while(millis() <= RECORD_TIME){

      voltage = analogRead(ECG_PIN);


       delay(0); // tested with many different values 

Sorry if the question is very basic, I am beginner, in a way, the Arduino platform. Any help will be very welcome.


512 / 103 is almost 5. If your samples are 3 digit values, plus a carriage return and a line feed, most of your println() calls write to the buffer. That takes next to no time.

The 103rd println() call fills the buffer, and causes the buffer to be written to the card. That takes time.

Is this what is causing your issues?

Hello, Paul. Thanks for the answer.

Yes, I think it's exactly what is causing the issue. The problem is I need to take continuous data from a signal conditioning circuit, that is already ok. But some points are lost while the buffer is recording in SD card. Is there any way to avoid this data loss? I was thinking in using two 512 bytes buffers working alternately, but the Uno board doesn't have enough memory. Now I'm trying to implement this using a Mega, but still no sucess :confused: . Any idea? Thanks again.

Look at this.

The latest version is here.