Interfacing SD Card Module with Arduino Nano
Circuit:-
Code:-
#include "SD.h"
#include "SPI.h"
#define CS_PIN 10
File file;
void setup() {
Serial.begin(9600);
initializeSD();
createFile("Hello.txt");
writeToFile("Hello World");
closeFile();
}
void loop() {
}
void initializeSD() {
Serial.println("Initializing SD Card.....Please Wait");
pinMode(CS_PIN, OUTPUT);
if(SD.begin()) {
Serial.println("SD Card ready to use !");
} else {
Serial.println("Error Initializing SD Card !");
return;
}
}
int createFile(char filename[]) {
file = SD.open(filename, FILE_WRITE);
if(file) {
Serial.println("File Created Successfully");
return 1;
} else {
Serial.println("Error Creating File !");
return 0;
}
}
int writeToFile(char text[]) {
if(file) {
file.println(text);
Serial.println("Text Append Successful");
return 1;
} else {
Serial.println("Error Appending Text !");
return 0;
}
}
void closeFile() {
if(file) {
file.close();
Serial.println("File Closed !");
}
}
Demo:-
Comments
Post a Comment