On some systems, such as 3D printers, memory cards are used to save print files. Therefore, it is very important to check the memory card connection at the beginning and during the printing process.

Therefore, in case of any connection or card failure, the system must be able to detect a malfunction and inform the user on the system LCD screen.

In addition to the 3D printer, this method can be used for any system or device that uses a memory card.

Now, for constructing this project, you'll need:

Therefore, we present a circuit to test the solution, as shown in Figure 1.


Figure 1 - Electronic Schematic of the Project.

Project Development

The logic of building the code is very simple. We need to check at the beginning (void setup function) and during code execution (inside the loop function) if the card is connected.

If the card is not detected, a message must be entered on the LCD screen to inform the user, according to is presented in Figure 2.


Figure 2 - Message of Failed or SD Card disconnected.

In this way, the user will insert the card and the system will re-operate again and present the message "Card Connected!", as is shown in Figure 3.


Figure 3 - Message of Card Connected.

After the system verifies the status of the SD Card, the system will wait for the user to press the button and initiate the storage processing of 10 values of ADC in the SD Card. 

At this moment, it will show the message as is presented in Figure 4.


Figure 4 - Message for the user to press the button to initiate the storage process.

After the user press the button, the system will storage 10 units of ADC values in the SD Card and present the screen with the message: "Storing data..." and "Finishing Successfully", to inform the end of the storage process. 

These messages are presented below.



After all these processes, the system back to the beginning of the loop and initiate all logic again.

Hereafter, we will present and discuss the code developed to solve the problem.

Programming Logic

According to the code below, the libraries of the used elements were inserted: LCD display, SD Card and declared all variables of the code.

        #include <SD.h>
#include <SPI.h>
#include <LiquidCrystal.h>  File myFile;  const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);  #define AnalogPin A0  int pinoSS = 10; // Pin 53 para Mega / Pin 10 para UNO
int DigitalValue = 0;
byte samples = 0;
bool SDCardTest = 0, ControlState = 0, LCDControl = 0;    

After this code block, we will present the void loop function below. As is possible to see, the Display LCD and Serial was initialized. 

After, was performed the first test to verify if our SD Card it is connected or it is failing.

        void setup()
{    Serial.begin(9600); // Define BaundRate   lcd.begin(16, 2);   pinMode(pinoSS, OUTPUT); // Declara pinoSS como saída     delay(500);   lcd.clear();       do   {     if (SD.begin())      { // Inicializa o SD Card       lcd.setCursor(6,0);       lcd.print("Card");       lcd.setCursor(3,1);       lcd.print("Connected!");       delay(2000);       SDCardTest = 1;     }        else      {       lcd.clear();       Serial.println("imprimindo segunda mensagem de erro.");       lcd.setCursor(1,0);       lcd.print("Failed or Card");       lcd.setCursor(2,1);       lcd.print("disconnected");       SDCardTest = 0;     }   }while(SDCardTest == 0);          lcd.clear();     lcd.setCursor(0,0);     lcd.print("Press the button");     lcd.setCursor(1,1);     lcd.print("To store data");

There is a do-while loop to verify the SD Card. In this process, the system does an initialization of the SD Card. If the initialization process occurs normally, so the SD Card it not with the problem. 

But, a case occurs anyone problem, the system will initialize the SD card.

This way will be presented the message "Failed or Card disconnected" in the Display...

Read more »