SD card is a very helpful in projects where you store a lot images or data for display. the built tin flash memory of MCUs run out easily and not suitable to store anything bigger or useful for your applications where it involved multiple graphical images.
Introduction
You can use Wokwi Arduino Simulator to learn Arduino programming You can use several peripherals such as sensors (temperature, pressure, accelerometer, gyro. etc), displays (LEDs, OLEDs, TFT, Character LCD, FastLED matrix and more). In this article you will learn how an SD card be used to store images or even executable code.. or to store MP3 and video files to play on a TFT display.
Live Arduino simulation link
Link: https://wokwi.com/arduino/projects/310542489623724609
Details
SD card simulation has just been enabled on the Wokwi Arduino Simulator. It means, it has a lot of scope to become more resourceful. Presently there is a GitHub ticket where you can track all the details. We will be glad to hear from you any suggestions, which you would like to share. Together, we can make the future SD card projects.
Connection diagram
Code
here is the code. I would rather suggest you to visit the Arduino simulation page given above for the complete working as well as latest code.
#include <SD.h>
#define CS_PIN 10
File root;
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// Example of reading file from the card:
File textFile = SD.open("wokwi.txt");
if (textFile) {
Serial.print("wokwi.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("error opening wokwi.txt!");
}
}
void loop() {
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
Feedback and suggestions
YOU are always welcome to share your suggestions and feedback to make the Simulator more helpful to you in your coming projects. Please hop on to the Wokwi Discord server to share your details. Share your interesting projects and browse through several curious projects from fellow developers and makers on Facebook Wokwi Group!
Comments
Please log in or sign up to comment.