11/*
2- * Connect the SD card to the following pins:
2+ * pin 1 - D2 | Micro SD card |
3+ * pin 2 - D3 | /
4+ * pin 3 - CMD | |__
5+ * pin 4 - VDD (3.3V) | |
6+ * pin 5 - CLK | 8 7 6 5 4 3 2 1 /
7+ * pin 6 - VSS (GND) | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄ /
8+ * pin 7 - D0 | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
9+ * pin 8 - D1 |_________________|
10+ * ║ ║ ║ ║ ║ ║ ║ ║
11+ * ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
12+ * ║ ║ ║ ║ ║ ║ ╚══════╗ ║
13+ * ║ ╔═════╝ ║ ║ ║ ╚═════╗ ║ ║
14+ * Connections for ║ ║ ╔═══╩═║═║═══╗ ║ ║ ║
15+ * full-sized ║ ║ ║ ╔═╝ ║ ║ ║ ║ ║
16+ * SD card ║ ║ ║ ║ ║ ║ ║ ║ ║
17+ * ESP32-S3 DevKit | 21 47 GND 39 3V3 GND 40 41 42 |
18+ * ESP32-S3-USB-OTG | 38 37 GND 36 3V3 GND 35 34 33 |
19+ * ESP32 | 4 2 GND 14 3V3 GND 15 13 12 |
20+ * Pin name | D1 D0 VSS CLK VDD VSS CMD D3 D2 |
21+ * SD pin number | 8 7 6 5 4 3 2 1 9 /
22+ * | █/
23+ * |__▍___▊___█___█___█___█___█___█___/
24+ * WARNING: ALL data pins must be pulled up to 3.3V with external 10k Ohm resistor!
25+ * Note to ESP32 pin 2 (D0): Add 1K Ohm pull-up resistor to 3.3V after flashing
326 *
4- * SD Card | ESP32
5- * D2 -
6- * D3 SS
7- * CMD MOSI
8- * VSS GND
9- * VDD 3.3V
10- * CLK SCK
11- * VSS GND
12- * D0 MISO
13- * D1 -
27+ * For more info see file README.md in this library or on URL:
28+ * https://github.com/espressif/arduino-esp32/tree/master/libraries/SD_MMC
1429 */
1530
1631#include " FS.h"
@@ -25,6 +40,29 @@ const char* password = "your-password";
2540long timezone = 1 ;
2641byte daysavetime = 1 ;
2742
43+ // Default pins for ESP-S3
44+ // Warning: ESP32-S3-WROOM-2 is using most of the default SD_MMC GPIOs (33-37) to interface with on-board OPI flash.
45+ // If the SD_MMC is initialized with default pins it will result in rebooting loop - please
46+ // reassign the pins elsewhere using the mentioned command `setPins`.
47+ // Note: ESP32-S3-WROOM-1 does not have GPIO 33 and 34 broken out.
48+ // Note: The board ESP32-S3-USB-OTG has predefined default pins and the following definitions with the setPins() call will not be compiled.
49+ // Note: Pins in this definition block are ordered from top down in order in which they are on the full-sized SD card
50+ // from left to right when facing the pins down (when connected to a breadboard)
51+
52+ #if defined(SOC_SDMMC_USE_GPIO_MATRIX) && not defined(BOARD_HAS_SDMMC)
53+ int d1 = 21 ; // SD pin 8 - Add a 10k Ohm pull-up resistor to 3.3V if using 4-bit mode (use_1_bit_mode = false)
54+ int d0 = 47 ; // SD pin 7 - Add a 10k Ohm pull-up resistor to 3.3V
55+ // GND pin - SD pin 6
56+ int clk = 39 ; // SD pin 5 - Add a 10k Ohm pull-up resistor to 3.3V
57+ // 3.3V pin - SD pin 4
58+ // GND pin - SD pin 3
59+ int cmd = 40 ; // SD pin 2 - Add a 10k Ohm pull-up resistor to 3.3V
60+ int d3 = 41 ; // SD pin 1 - Add a 10k Ohm pull-up resistor to 3.3V to card's pin even when using 1-bit mode
61+ int d2 = 42 ; // SD pin 9 - Add a 10k Ohm pull-up resistor to 3.3V if using 4-bit mode (use_1_bit_mode = false)
62+ #endif
63+
64+ bool use_1_bit_mode = false ; // Change the value to `true` to use 1-bit mode instead of the 4-bit mode
65+
2866void listDir (fs::FS &fs, const char * dirname, uint8_t levels){
2967 Serial.printf (" Listing directory: %s\n " , dirname);
3068
@@ -164,16 +202,39 @@ void setup(){
164202 Serial.println (" IP address: " );
165203 Serial.println (WiFi.localIP ());
166204 Serial.println (" Contacting Time Server" );
167- configTime (3600 *timezone, daysavetime*3600 , " time.nist.gov" , " 0.pool.ntp.org" , " 1.pool.ntp.org" );
168- struct tm tmstruct ;
205+ configTime (3600 *timezone, daysavetime*3600 , " time.nist.gov" , " 0.pool.ntp.org" , " 1.pool.ntp.org" );
206+ struct tm tmstruct ;
169207 delay (2000 );
170208 tmstruct.tm_year = 0 ;
171209 getLocalTime (&tmstruct, 5000 );
172- Serial.printf (" \n Now is : %d-%02d-%02d %02d:%02d:%02d\n " ,(tmstruct.tm_year )+1900 ,( tmstruct.tm_mon )+1 , tmstruct.tm_mday ,tmstruct.tm_hour , tmstruct.tm_min , tmstruct.tm_sec );
210+ Serial.printf (" \n Now is : %d-%02d-%02d %02d:%02d:%02d\n " ,(tmstruct.tm_year )+1900 ,( tmstruct.tm_mon )+1 , tmstruct.tm_mday ,tmstruct.tm_hour , tmstruct.tm_min , tmstruct.tm_sec );
173211 Serial.println (" " );
174212
175- if (!SD_MMC.begin ()){
176- Serial.println (" Card Mount Failed" );
213+ // If you are using any other ESP32-S3 board than ESP32-S3-USB-OTG which has preset default pins, you will
214+ // need to specify the pins with the following example of SD_MMC.setPins()
215+ // If you want to use only 1-bit mode, you can use the line with only one data pin (d0) begin changed.
216+ // Please note that ESP32 does not allow pin changes and will fail unless you enter the same pin config as is the hardwired.
217+ #if defined(SOC_SDMMC_USE_GPIO_MATRIX) && not defined(BOARD_HAS_SDMMC)
218+ if (use_1_bit_mode){
219+ if (! SD_MMC.setPins (clk, cmd, d0)){ // 1-bit line version
220+ Serial.println (" Pin change failed!" );
221+ return ;
222+ }
223+ } else {
224+ if (! SD_MMC.setPins (clk, cmd, d0, d1, d2, d3)){ // 4-bit line version
225+ Serial.println (" Pin change failed!" );
226+ return ;
227+ }
228+ }
229+ #endif
230+
231+ if (!SD_MMC.begin (" /sdcard" , use_1_bit_mode)){
232+ Serial.println (" Card Mount Failed." );
233+ Serial.println (" Increase log level to see more info: Tools > Core Debug Level > Verbose" );
234+ Serial.println (" Make sure that all data pins have a 10k Ohm pull-up resistor to 3.3V" );
235+ #ifdef SOC_SDMMC_USE_GPIO_MATRIX
236+ Serial.println (" Make sure that when using generic ESP32-S3 board the pins are setup using SD_MMC.setPins()" );
237+ #endif
177238 return ;
178239 }
179240 uint8_t cardType = SD_MMC.cardType ();
@@ -203,7 +264,7 @@ void setup(){
203264 deleteFile (SD_MMC, " /hello.txt" );
204265 writeFile (SD_MMC, " /hello.txt" , " Hello " );
205266 appendFile (SD_MMC, " /hello.txt" , " World!\n " );
206- listDir (SD_MMC, " /" , 0 );
267+ listDir (SD_MMC, " /" , 0 );
207268}
208269
209270void loop (){
0 commit comments