ESP32 with SD card modules — The master guide
Using SD cards with an ESP32 is simple, right? In reality you might find some shortcomings; this article explores common drawbacks and provides an in-depth view of the SD library.
Connecting to the SD card module
Most SD card modules are like the one in the picture above. They connect through a protocol known as Serial Peripheral Interface (SPI) and accept either 5V or 3.3V (note most modules won’t work with 3.3V).
For testing, you can connect the module to the ESP32 as indicated in the diagram above. For reference, the SPI pins of the ESP32 are: MOSI GPIO23, MISO GPIO 19, SCK GPIO 18 and CS GPIO 5. Note the Clock Select (CS) pin is not fixed as the other SPI pins. You can practically use any pin as the CS pin.
Now, plug in a microSD card and try the code below. You can ignore #include <Arduino.h>, if you’re running this code in the Arduino IDE; though it should not cause any problems.