SD-card access is sometimes delayed... why?

Hello everybody

I'm completely new to programming and just finished my first project. And it works. At least for an hour or so.
What it does is: After pressing a button, the Arduino accesses a .txt file on a sd-card, counts the lines, generates a random number between 1 and said number of lines, sends this line to an thermoprinter and it is printed. Like I said, this works. However, if I leave it powered on for like an hour, it stops working. (at least the way it should)
When the button is then pressed, it registers the pressed button. But then nothing happens. After quite some time (about 10 minutes), it finds a line and prints it.
What I found so far is, that the sd-card reader might be the problem. This is supported by the fact, that it registers that the button was pressed, but then doesn't access the sd-card.
I also have to add that this problem is not consistent. Sometimes it suddenly works again as supposed...

Hardware used:

  • Arduino micro pro
  • SD-card reader: got not much information on that, but the EAN is 4251266703204
  • Thermoprinter: Adafruit ADA2753
  • SD-card: Intenso High Performance microSDHC-card 4 GB, EAN 4034303016099

Wiring:

Code:

#include <SPI.h>
#include <SD.h>
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"

#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor

String Text="default";
String fileName="print.txt";
File myFile;
char cha;
int zaehlerzeile;
int zaehlersuche;
int gefunden=0;
int loop1=0;
int linescounted=0;
int ziel;
int maxzeile;
int max;


void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  digitalWrite(3,HIGH);
  Serial.begin(9600);
  mySerial.begin(9600);
  //Printer setup
  printer.begin();
  printer.setLineHeight(50);
}

void loop (){
  //Pressing button on pin 2 starts process
  if(digitalRead(2)){
    Serial.println("button pressed");
    delay(1000);
    initializeCard();
    //read lines on SD card
    countlines();   
    randomSeed(micros());
    max=zaehlerzeile+1;     
    //generate random number
    zaehlersuche=random(1, max);   
    //seek and save line
    seekline();  
    //print
    printing();   
    Text="";
  }
  
}
void initializeCard(){
  if(!SD.begin(4)){
    while(1);
  }
}

void countlines(){
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=0;
    while(myFile.available()){
        cha=myFile.read();
        if(cha=='\n'){
            zaehlerzeile++;
          }
      }
    zaehlerzeile++;
  }
  myFile.close();
  maxzeile=zaehlerzeile;
}

void seekline(){
  gefunden=0;    
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=1;
    while(myFile.available()){
      if(zaehlersuche==1){
        read();
      }
      while(gefunden==0){
        cha=myFile.read();
        if(cha=='\n'){
          zaehlerzeile++;
          if(zaehlerzeile==zaehlersuche){
            read();            
          }
        }
      }
    }
  }
  myFile.close();
}
  
void read(){
  Text=myFile.readStringUntil('\n');
  gefunden=1;
  myFile.close();
}

void printing(){
  printer.justify('C');
  printer.setSize('L');
  printer.setLineHeight(50);
  Serial.println("printing geöffnet");
  Serial.println(Text);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println(Text);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println("");
  //delay(2000);
  Serial.println("ende");
}

So, if anybody got an idea why this delay happens, please tell me.
Thanks for your help!

Please post a link to the product page for the SD-card module.

How are you handling the 5V to 3.3V logic level conversion?

If the SD module does not have 5V to 3.3V logic level shifters on board, by connecting the Pro Micro directly to it, you may end up destroying the Pro Micro, the SD module, or both.

Avoid using String types and stick with standard null terminated strings.

Do you have the same issues if you initialise the SD card in setup, rather than each time the button is pressed inside the main loop?

1 Like

The SD-reader can handle up to 5.5V. This should not be the problem.

5V power. The product page states that the module has a 3.3V regulator, but does not state that the I/O is 5V compatible.

It is safest to assume that I/O is not compatible and to use a 5V to 3.3V logical level shifter, like this one.

Edit: the product photo shows an LVC125A bus buffer chip, which is 5V compatible, so you are probably OK.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.