Example Code for Arduino-Use SD Card with LCD

Last revision 2025/12/24

In this section, we'll explain how to use the SD card with LCD screen.

Hardware Preparation

Software Preparation

Note: This library only works on Classic Arduino IDE 1.0.x)

Sample Code

/*
/*
This code is to teach you how to use SD library.

 Created 2016-4-8
 By Andy zhou <[email protected]>
 version:V1.0
*/
#include <Arduino.h>
#include <SPI.h>
#include <MultiLCD.h>
#include <SD.h>
#include "datalogger.h"

#define STATE_SD_READY 0x1
#define STATE_OBD_READY 0x2
#define STATE_GPS_CONNECTED 0x4
#define STATE_GPS_READY 0x8
#define STATE_MEMS_READY 0x10
#define STATE_GUI_ON 0x20

LCD_R61581 lcd;
CDataLogger logger;

byte state = 0;

bool checkSD()
{
    Sd2Card card;
    SdVolume volume;
    state &= ~STATE_SD_READY;
    pinMode(SS, OUTPUT);

    lcd.setFontSize(FONT_SIZE_MEDIUM);
    if (card.init(SPI_HALF_SPEED, SD_CS_PIN)) {
        const char* type;
        switch(card.type()) {
        case SD_CARD_TYPE_SD1:
            type = "SD1";
            break;
        case SD_CARD_TYPE_SD2:
            type = "SD2";
            break;
        case SD_CARD_TYPE_SDHC:
            type = "SDHC";
            break;
        default:
            type = "SDx";
        }

        lcd.print(type);
        lcd.write(' ');
        if (!volume.init(card)) {
            lcd.print("No FAT!");
            return false;
        }

        uint32_t volumesize = volume.blocksPerCluster();
        volumesize >>= 1; // 512 bytes per block
        volumesize *= volume.clusterCount();
        volumesize >>= 10;

        lcd.print((int)volumesize);
        lcd.print("MB");
    } else {
        lcd.println("No SD Card");
        return false;
    }

    if (!SD.begin(SD_CS_PIN)) {
        lcd.println("Bad SD");
        return false;
    }

    state |= STATE_SD_READY;
    return true;
}

void setup(){
  lcd.begin();
  lcd.setFontSize(FONT_SIZE_MEDIUM);  //set font size
  lcd.setColor(RGB16_RED);  //set strings color
  lcd.println();
  lcd.println();
  lcd.println("DFRobot!!!");
  lcd.println("TELEMATICS LCD SHIELD V1.0");
  lcd.println();
  lcd.setColor(RGB16_WHITE);
  if (checkSD()) {
        uint16_t index = logger.openFile();
        lcd.println();
        if (index > 0) {
            lcd.print("File ID:");
            lcd.println(index);
        } else {
            lcd.print("No File");
        }
    }
}

void loop(){
}

Was this article helpful?

TOP