Espressif ESP32C3 SD implementation

I’ve been trying to get the SD card library to set the file creation date and timestamp when a file is newly created.

The standard SD Arduino library (SD.h) includes the files which then include the FAT structure - this makes it possible to add a callback such that when a new file is created - the date and timestamps are placed in the files FAT entry.

E.g. look at the SD.h and the include files in the standard arduino libraries.

The ESP32 XIAO implementation uses the Espressif ESP32 board definitions and it includes its own SD library - which omits the includes and definitions for the files FAT entry. E.g look at Arduinoxx, packates, the ESP32 directory, hardware, then 3.0.x, then the source for SD.cpp and the SD.h – when you look at the files they include - the FAT_DATE and FAT_TIME macros for the structure definition are not there.

Has anyone else run into the same problem, it looks incomplete.

FWIW, the standard SD card libraries with the time code will compile on a XIAO SAMD21, but not the XIAO ESP32C3, and I doubt that they would compile and operate correctly on any of the ESP32 designs using the Espressif library. Any suggestions?

Hi there,
I would look at the Round display Examples, or the WEB cam demo for the ESP32s3 sense the portion where saving the image to SD card may shed some light.
GL :slight_smile: PJ

1 Like

Can’t do, this is a custom board that I created. No display,

I can open & mount an SD card so long as I don’t care if the time stamp says 1980 or the year 2047 or what ever previously was in the structure that is written to the FAT.

The result is that when an SD card is pulled out and mounted in another device - it looks pretty unprofessional.

The issue is that the ESP32 library provided by Espressif (3.0.1) includes the SD library that lacks the structure files and macro needed.

E.g. if you create a function for callback similar to:

You can’t compile the sketch, the macros FAT_TIME and FAT_DATE do not exist.

If you look at the IDE board manger only the Arduino Nano ESP32 has an entry - unless you select the Espressif ESP32 combination - which then removes the definitions need.

I’m really looking for someone else to look at the libraries (standard Arduino SD & version provided by Espressif to confirm that I’m not imagining things) – I could be mistaken!

Hi there,
How do you format the SD card?
What format are you using?
What is your TIME source?
GL :slight_smile: PJ

Formatted FAT32, file is created, about 46000+ lines of valid data written, multiple files with different names - but the file creation date is a default - caused by the lack of the correct structure in the Espressif ESP32 library.

Please look at the Standard Arduino library on the arduino git hub, then look at the Espressif ESP32 git hub, SD library files - I’m pretty sure that the issue will be spotted by someone else besides myself.

If I’m right it would also explain why some XIAO users say that they are having problems with various sketches on the seeed ESP32 product line - the libraries don’t implement the same generic Arduino libraries used by Arduino, Adafruit, Sparkfun or others. There are functionality changes.

Here is a stripped down example you can try using a XIAO SAMD21, then try compiling it for a XIAO ESP32C3

Here is the link to the Espressif ESP32 Arduino github for the SD library - look at the source files (.cpp & .h), follow the links to the other Espressif ESP32 libraries that are included in the library.

Here is the link to the standard Arduino SD library, in this library (which works with XIAO SAM21), you’d notice the links in the SD.h to include file utility items (different directory under SD), the Utility directory includes the FAT structure and definitions.

It looks like Espressif only implemented some of the file system library functions. I suspect that has been the same with some of their other library functions in the Espressif github.

Hi there,
ok , Fat32 … what’s the time source your giving it :face_with_peeking_eye:
Why can’t you answer simple question? :face_with_hand_over_mouth: how big is the SD card
I won’t do your work for you, answer the questions if you want some help, start there.
GL :slight_smile: PJ
even better post the code.

I posted a link to a prior response that was posted on this forum that shows how to set the time & date for the file creation. It’s in my responses.;

Even that simple sample will fail with the ESP32 because of the incomplete library definitions in the Espressif ESP32 board configuration — which included their own version of the library.

It’s not wiring, its not SD card type, it’s not FAT, I’m pretty sure that the is a library problem and it could be seen if people look at the two github links posted and search for the FAT structure… the ESP32 version misses the FAT_TIME and FAT_DATE macro and the ability to add a datetimeCallback to set the time on the file entry when it is created.

Check the code from Espressif and Arduino

I did answer simple questions - I need someone to simply look at the source for the SD libraries and confirm that they also see that the data structure definitions are missing in the ESP32 library for the SD card that is provided by Espressif.

Links to THEIR GITHUB code and the ARDUINO standard GITHUB for the two different versions of the SD libraries have been posted in this thread.

I’ve even posted a link to a prior thread from this forum which showed example of how to set the time & date — it won’t compile with the ESP32 selected — because the library lacks the FAT-TIME and FAT_DATE macrco.

Please look at the actual githubs to see verify the problem.

I don’t need people to do the code work for me. I have nearly 50 years experience, thousands, perhaps tens of thousands of lines on various arduino devices. Lots on the Xiao.

What I do need is for someone to actually look at the Espressif SD library and confirm that it’s missing the the FAT macros and structure. Then I will put in trouble tickets to Seeed, Espressif, Adafruit, and all the vendors that use the ESP32 library as named in the board manager - and let them work with Espressif - if indeed I am correct.

I’m looking for someone to help verify the problem (missing structure in their library) so a ticket can be added to the Espressif ESP32 gitub.

No coding is required, just open up the ESP32 SD library and look for the FAT structure definitions.

Hi there.
Ok , now it makes more sense, So which library do you want to use? and It does work on the Xiao ESP32S3 with the camera app. and it works with the Xiao Nrf52840 if you set the system date and time so?
I’ll look and see what your talking about, I also recall a Micro python “touch” kinda util to set date and time for SD files. I would look there as well.
GL :slight_smile: PJ

I really need a SD card library that works with the ESP32 AND supports the FAT macro definitions for time.

If someone can independently confirm that they are missing in the Espressif library - I can then put in tickets on the github for the library and work work with Espressif to correct the library problem.

The only other native ESP32 support is for the Arduino Nano, it looks like nearly all the other vendors are bundled under the ESP32 board library provided by Espressif.

If the library is the source of the problem - the best solution for all is to help explain to Espressif what’s missing and help them update the library.

That should also help clear up lots of others that have posted comments about a sketch compiles correctly for board xyz but not on the ESP32… again - I need verification that’s the problem.

Nordic chip doesn’t use the ESP32 library from Espressif, I have some Xiao Nrf52840’s but haven’t had time to chase down what happens in the cases that it is used.

i’m on the road until next week. When I’m back around - I may try compiling the testjig that I’ve created for a project that uses a Xiao SAMD21 and see how the Nordic reacts. The jib uses the XIAO expansion board with the display, RTC and SD card. That’s a test jib - not the project I’m working on now.

Ideally, you’d want any person to drop onto a Xiao expansion board uses all its features with as few changes to the code as possible. (perhaps some #defines and #if for code changes).

E.g. the XIAO footprint makes the modules more like a commodity chip with a wide feature choice.

If the modules become more like a commodity chip, it means the projects created with them will last longer and be more useable by many. The end-of-life for a module becomes less of an issue.

1 Like

i would think you would have to get the internal ESP32 clock set before trying any of this stuff
PS I never use expressive libraries in Arduino IDE

did you try this library?

Hi there,
So I did , of course it works as it should. I’m using the Xiao Expansion Board too. “SS D2”
I think the issue is his Time source, For whatever reason he doesn’t post the code he’s working with so I didn’t either. He’s got 50 years experience , should be able to get it working. We just verified it.
I set it manually or with time server and I see no Issues, Well maybe one between the KEYBOARD and the FLOOR, LOL :smile: :face_with_hand_over_mouth:
Here is the output and I added the same to the SD tester code I used in the drag and drop testing with Uf2 files and it works there also. reports the correct time and date with the files

Power ON 
 Test program compiled on Jun 12 2024 at 01:16:58
Processor came out of reset.
.WiFi connected
IP address:
Contacting Time Server

Now is : 2024-06-12 07:11:16

SD Card Type: SDHC
SD Card Size: 29827MB
Listing directory: /
  DIR : System Volume Information  LAST WRITE: 2024-03-13 00:03:24
  FILE: test.txt  SIZE: 1048576  LAST WRITE: 1980-01-01 00:22:04
  DIR : mydir  LAST WRITE: 2024-06-12 07:11:56
  FILE: hello.txt  SIZE: 13  LAST WRITE: 2024-06-12 07:11:56
  FILE: Spagettio.txt  SIZE: 13  LAST WRITE: 2024-06-12 07:11:22
Removing Dir: /mydir
Dir removed
Creating Dir: /mydir
Dir created
Deleting file: /Spagettio.txt
File deleted
Writing file: /hello.txt
File written
Appending to file: /hello.txt
Message appended
Renaming file /hello.txt to /Spagettio.txt
File renamed
Reading file: /Spagettio.txt
Read from file: Hello World!
Listing directory: /
  DIR : System Volume Information  LAST WRITE: 2024-03-13 00:03:24
  FILE: test.txt  SIZE: 1048576  LAST WRITE: 1980-01-01 00:22:04
  DIR : mydir  LAST WRITE: 2024-06-12 07:17:16
  FILE: Spagettio.txt  SIZE: 13  LAST WRITE: 2024-06-12 07:17:16

FYI test.txt was already on the SD card I used, Idunno about its date and time the others are accurate.
Reading file: /Spagettio.txt :point_left: :laughing: I used to love those…
Works fine…
GL :slight_smile: PJ

throw a bone,

FQBN: esp32:esp32:XIAO_ESP32C3
Using board 'XIAO_ESP32C3' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.1
Using core 'esp32' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.1
---Brev Edit---
Sketch uses 942308 bytes (71%) of program storage space. Maximum is 1310720 bytes.
Global variables use 32980 bytes (10%) of dynamic memory, leaving 294700 bytes for local variables. Maximum is 327680 bytes.
"C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\4.6/esptool.exe" --chip esp32c3 --port "COM6" --baud 921600  --before default_reset --after hard_reset write_flash  -z --flash_mode keep --flash_freq keep --flash_size keep 0x0 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\5C2DCB185E3B22F23B052F92402C91EE/SD_time.ino.bootloader.bin" 0x8000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\5C2DCB185E3B22F23B052F92402C91EE/SD_time.ino.partitions.bin" 0xe000 "C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.1/tools/partitions/boot_app0.bin" 0x10000 "C:\Users\Dude\AppData\Local\Temp\arduino\sketches\5C2DCB185E3B22F23B052F92402C91EE/SD_time.ino.bin" v4.6
Serial port COM6
Chip is ESP32-C3 (revision v0.3)
Features: WiFi, BLE
Crystal is 40MHz
MAC: a0:76:4e:3f:a3:88
Uploading stub...
Running stub...

Its needed to add the normal Date/Time stamp to the SD CARD files my project creates, to ensure my final product will impress customers as being professional


1 Like

I love it when a plan comes together!

I like when we can help each other, because the internet is such a huge place, no one person could have seen it all… and some golden nuggets can be found scraping the bottom of the river!

I wonder what happens on a XIAO SAMD? i never tried setting up time on that one, other than reading the RTC

… time is a many splentered thing…

1 Like

Hi there,
Idunno , I never had any XIAO SAMD21 too weak IMO. I don’t get why it’s even used.
lowest SYSTEM clock speed of all,No rf Radio, least amount of ram and Flash. Good for blinking some LED’s and that’s about it IMO. It’s the price maybe , but now ESP eats it’s lunch.
GL :slight_smile: PJ

yeah i ordered 3 of the NRF and they dont have wifi, so i guess i just did it so i would have one incase i needed one

1 Like