In my previous blog, I explained usage of Shift register using Node MCU and shown how to extend the additional digital input and output pins using 74HC595 and CD4021B shift register. In this blog we will use 74HC595 along with SD card and Node MCU.
74HC595: This is serial to parallel shift out register. You can use 8 digital output.This works in synchronous serial communication.
SD Card: SD Card will be used to store the data.
![]() |
SD Card and 74HC595 Schematic |
PIN Connections:
SD Card | Node MCU | 74HC595N |
MISO | D6 (GPIO12) | |
SCK | D5 (GPIO14) | SCK/SRCLK/SH_CP (PIN: 11) CLOCK PIN |
D3 (GPIO0) | RCK/SH_CP/RCKL (PIN: 12) LATCH | |
MOSI | D7 (GPIO13) | SER/DS (PIN: 14) DATA |
CS/SS | D8 (GPIO15) | |
GND | GND | G/OE |
VCC (3.3V) | SCL/MR/SRCLR | |
VCC | VU (5V) |
Once connection is made, you can use Arduino IDE to program the device. First thing is to initialise the Output pins as below,
pinMode(LATCHOUT_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
SD Card Initialisation can be done as below,
pinMode(CHIPSELECT_PIN, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(CHIPSELECT_PIN)) {
// don't do anything more:
Serial.println("Card failed, or not present");
return false;
} else {
if (!isBaseDirAvailable()) {
createDirectory();
}
return true;
}
Serial.println("card initialized.");
Once initialisation is complete, then call the write and read method of the SD card to read or write on SD card device. Also in parallel you can set the digital output pin as per requirement.
Reading the file:
File readFile = SD.open(ROOT_DIRECTORY);
readFile.rewindDirectory();
dataReader(readFile, 0);
readFile.flush();
readFile.close();
Here if you are reading the files in loop then make sure that you rewind directory.
Writing the file:
if (!checkSpace(MINIMUM_SDCARD_SIZE)) {
deleteFile();
}
//SdFile::dateTimeCallback(TimeRecorder::dateTime);
File dataFile = SD.open(filename, FILE_WRITE);
uint32_t fileSize = dataFile.size();
fileSize /= 1024;
// if the file is available, write to it:
if (dataFile) {
dataFile.println(output);
// print to the serial port too:
//Serial.println(output);
} else {
Serial.println("Error in reading/opening file " + filename);
//dataFile.clearWriteError();
//Serial.print("Write Error: ");
}
dataFile.flush();
dataFile.close();
In above code before writting multiple files, we are checking the space. Also, if you are using Real Time Clock then you may want current timestamp while writting the file. This can be acheived as below,
SdFile::dateTimeCallback(TimeRecorder::dateTime); //TimeRecorder is your RTC setup class
dateTime callback method can be as below,
static void dateTime(uint16_t* date, uint16_t* time) {
static RTC_DS1307 RTCCall;
DateTime now = RTCCall.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
Please find complete program as below,