Esp32-c3 set micro sd card help

Hello I have a xiao esp32-c3 and a micro sd card with a module for. I use the Arduino IDE software and for the micro SD the SD library with the SD_Test example but the problem is that it doesn’t work someone can help me please

How do you have it wired… which SD card Module?

Name: TF Micro SD Card Module Mini SD Card Module Memory Module for Arduino ARM AVR

Int sck = D8;
Int miso = D9;
Int mosi = D10;
Int cs = D7;

https://www.amazon.com/Micro-Card-Module-Memory-Arduino/dp/B01N7B8UTX

What code are you using?

`/*

  • pin 1 - not used | Micro SD card |
  • pin 2 - CS (SS) | /
  • pin 3 - DI (MOSI) | |__
  • pin 4 - VDD (3.3V) | |
  • pin 5 - SCK (SCLK) | 8 7 6 5 4 3 2 1 /
  • pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
  • pin 7 - DO (MISO) | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
  • pin 8 - not used |_________________|
  •                         ║ ║ ║ ║ ║ ║ ║ ║
    
  •                 ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
    
  •                 ║         ║ ║ ║ ║ ║ ╚══════╗    ║
    
  •                 ║   ╔═════╝ ║ ║ ║ ╚═════╗  ║    ║
    
  • Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
  • full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
  • SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
  • Pin name | - DO VSS SCK VDD VSS DI CS - |
  • SD pin number | 8 7 6 5 4 3 2 1 9 /
  •              |                                  █/
    
  •              |__▍___▊___█___█___█___█___█___█___/
    
  • Note: The SPI pins can be manually configured by using SPI.begin(sck, miso, mosi, cs).
  •    Alternatively, you can change the CS pin and use the other default settings by using `SD.begin(cs)`.
    
  • ±-------------±--------±------±---------±---------±---------±---------±---------+
  • | SPI Pin Name | ESP8266 | ESP32 | ESP32‑S2 | ESP32‑S3 | ESP32‑C3 | ESP32‑C6 | ESP32‑H2 |
  • +==============+=========+=======+==========+==========+==========+==========+==========+
  • | CS (SS) | GPIO15 | GPIO5 | GPIO34 | GPIO10 | GPIO7 | GPIO18 | GPIO0 |
  • ±-------------±--------±------±---------±---------±---------±---------±---------+
  • | DI (MOSI) | GPIO13 | GPIO23| GPIO35 | GPIO11 | GPIO6 | GPIO19 | GPIO25 |
  • ±-------------±--------±------±---------±---------±---------±---------±---------+
  • | DO (MISO) | GPIO12 | GPIO19| GPIO37 | GPIO13 | GPIO5 | GPIO20 | GPIO11 |
  • ±-------------±--------±------±---------±---------±---------±---------±---------+
  • | SCK (SCLK) | GPIO14 | GPIO18| GPIO36 | GPIO12 | GPIO4 | GPIO21 | GPIO10 |
  • ±-------------±--------±------±---------±---------±---------±---------±---------+
  • For more info see file README.md in this library or on URL:
  • arduino-esp32/libraries/SD at master · espressif/arduino-esp32 · GitHub
    */

#include “FS.h”
#include “SD.h”
#include “SPI.h”

/*
Uncomment and set up if you want to use custom pins for the SPI communication
#define REASSIGN_PINS
int sck = -1;
int miso = -1;
int mosi = -1;
int cs = -1;
*/

void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.printf(“Listing directory: %s\n”, dirname);

File root = fs.open(dirname);
if (!root) {
Serial.println(“Failed to open directory”);
return;
}
if (!root.isDirectory()) {
Serial.println(“Not a directory”);
return;
}

File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : “);
Serial.println(file.name());
if (levels) {
listDir(fs, file.path(), levels - 1);
}
} else {
Serial.print(” FILE: “);
Serial.print(file.name());
Serial.print(” SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}

void createDir(fs::FS &fs, const char *path) {
Serial.printf(“Creating Dir: %s\n”, path);
if (fs.mkdir(path)) {
Serial.println(“Dir created”);
} else {
Serial.println(“mkdir failed”);
}
}

void removeDir(fs::FS &fs, const char *path) {
Serial.printf(“Removing Dir: %s\n”, path);
if (fs.rmdir(path)) {
Serial.println(“Dir removed”);
} else {
Serial.println(“rmdir failed”);
}
}

void readFile(fs::FS &fs, const char *path) {
Serial.printf(“Reading file: %s\n”, path);

File file = fs.open(path);
if (!file) {
Serial.println(“Failed to open file for reading”);
return;
}

Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}

void writeFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf(“Writing file: %s\n”, path);

File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println(“Failed to open file for writing”);
return;
}
if (file.print(message)) {
Serial.println(“File written”);
} else {
Serial.println(“Write failed”);
}
file.close();
}

void appendFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf(“Appending to file: %s\n”, path);

File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println(“Failed to open file for appending”);
return;
}
if (file.print(message)) {
Serial.println(“Message appended”);
} else {
Serial.println(“Append failed”);
}
file.close();
}

void renameFile(fs::FS &fs, const char *path1, const char *path2) {
Serial.printf(“Renaming file %s to %s\n”, path1, path2);
if (fs.rename(path1, path2)) {
Serial.println(“File renamed”);
} else {
Serial.println(“Rename failed”);
}
}

void deleteFile(fs::FS &fs, const char *path) {
Serial.printf(“Deleting file: %s\n”, path);
if (fs.remove(path)) {
Serial.println(“File deleted”);
} else {
Serial.println(“Delete failed”);
}
}

void testFileIO(fs::FS &fs, const char *path) {
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
if (file) {
len = file.size();
size_t flen = len;
start = millis();
while (len) {
size_t toRead = len;
if (toRead > 512) {
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = millis() - start;
Serial.printf("%u bytes read for %lu ms\n", flen, end);
file.close();
} else {
Serial.println(“Failed to open file for reading”);
}

file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println(“Failed to open file for writing”);
return;
}

size_t i;
start = millis();
for (i = 0; i < 2048; i++) {
file.write(buf, 512);
}
end = millis() - start;
Serial.printf("%u bytes written for %lu ms\n", 2048 * 512, end);
file.close();
}

void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}

#ifdef REASSIGN_PINS
SPI.begin(sck, miso, mosi, cs);
if (!SD.begin(cs)) {
#else
if (!SD.begin()) {
#endif
Serial.println(“Card Mount Failed”);
return;
}
uint8_t cardType = SD.cardType();

if (cardType == CARD_NONE) {
Serial.println(“No SD card attached”);
return;
}

Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println(“MMC”);
} else if (cardType == CARD_SD) {
Serial.println(“SDSC”);
} else if (cardType == CARD_SDHC) {
Serial.println(“SDHC”);
} else {
Serial.println(“UNKNOWN”);
}

uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf(“SD Card Size: %lluMB\n”, cardSize);

listDir(SD, “/”, 0);
createDir(SD, “/mydir”);
listDir(SD, “/”, 0);
removeDir(SD, “/mydir”);
listDir(SD, “/”, 2);
writeFile(SD, “/hello.txt”, "Hello ");
appendFile(SD, “/hello.txt”, “World!\n”);
readFile(SD, “/hello.txt”);
deleteFile(SD, “/foo.txt”);
renameFile(SD, “/hello.txt”, “/foo.txt”);
readFile(SD, “/foo.txt”);
testFileIO(SD, “/test.txt”);
Serial.printf(“Total space: %lluMB\n”, SD.totalBytes() / (1024 * 1024));
Serial.printf(“Used space: %lluMB\n”, SD.usedBytes() / (1024 * 1024));
}

void loop() {}`

Set up your pins here

I’ve already tried with this but it still didn’t work, for the micro SD card I have 32GB and it was formatted in FAT32

set up your pins and post the correct code… what da? this is no game man dont waste my time no need to post code you are not using

#include <SPI.h>
#include <SD.h>

int sck = D8;
int miso = D9;
int mosi = D10;
int cs = D7;

void setup() {
Serial.begin(115200);

// Initialisation de la carte SD
SPI.begin(sck, miso, mosi, cs);
if (!SD.begin(cs)) {
Serial.println(“Card mount failed”);
return;
}
Serial.println(“Card SD initialised”);

// Création d’un fichier et écriture de données
File dataFile = SD.open("/test.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(“Hello, ESP32-C3!”);
dataFile.close();
Serial.println(“Writing in test.txt successful”);
} else {
Serial.println(“Error writing in test.txt”);
}

// Lecture du fichier
dataFile = SD.open("/test.txt");
if (dataFile) {
Serial.println(“Contained of test.txt:”);
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
} else {
Serial.println(“Error opening test.txt for reading”);
}
}

void loop() {
}

This is my script that I did to test if my micro sd card works or not to do more even if it will not be more than launching a file with another script

Did you select XIAO ESP32C3 as the board?

SD Text

cs=20 FOR PIN 7… DONT USE D IN FRONT

Its the difference between the Physical Pin and the Logical Pin… which is very Illogical

I will have sent photos to show you how I did it, but it doesn’t work to send them.

I made the code but it doesn’t work too, serial monitor « Card Mount Failled»

No it’s good it works I re uploaded the program is that to work

good… yes… sometimes the C3 will act like it has uploaded but has not… also sometimes you have to press the reset button or power off and on again!

PS I like your Avatar

Pour mon avatar, regardez-le et retournez-le, il y a autre chose :sweat_smile::wink:

1 Like