How to use a MicroSD card on ESP32S3 with MicroPython

Ats
4 min readJun 22, 2024

--

This is a note about what I did to implement to save images in a MicroSD card on ESP32S3 with MicroPython

Photo by Fredy Jacob on Unsplash

First of all

I used ESP32S3 provided by Seed Studio. The pin map would be different from the ones provided by others.

Background

I needed to take photos with ESP32S3 last week for my private project. However, there are very limited memories on it. So I decided to save the data on an SD card. I thought it would be straightforward but it wasn’t that straightforward. Then I thought I would make a note about it for future me.

What I did

First of all, I needed to install the firmware which includes a camera driver for ESP32S3. Once I googled it, I found the following repository.

I think this seems to be the first option for taking photos for now. I also checked the docs by Seeed Studio and they also provided the firmware.

I checked their sample code and they seemed to use the driver inside because the interface and functions were similar to the driver. So I decided to go with the firmware provided by Seeed Studio this time. I uploaded the firmware with the following code.

esptool.py --chip esp32s3 --port /dev/tty.usbmodem2101 write_flash -z 0 firmware/ESP32_GENERIC_S3-20240602-v1.23.0.bin

Then I googled anything for a good starting point and found the sample code. So I used the code as my reference.

I got the error when I executed the code which I just copied and pasted from the reference.

D (19074) spi: SPI3 use gpio matrix.
D (19074) sdspi_host: sdspi_host_init_device: SPI3 cs=13 cd=-1 wp=-1
D (19074) spi_hal: eff: 400, limit: 80000k(/0), 0 dummy, -1 delay
D (19074) spi_master: SPI3: New device added to CS5, effective clock: 400kHz
I (19074) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
D (19074) spi_master: device5 locked the bus
D (19074) spi_master: device5 release bus
D (19074) spi_master: device5 locked the bus
D (19074) spi_master: device5 release bus
D (19094) spi_master: device5 locked the bus
D (19094) spi_master: device5 release bus
D (19114) spi_master: device5 locked the bus
D (19114) spi_master: device5 release bus
D (19114) sdmmc_cmd: sdmmc_send_cmd_send_if_cond: received=0x0 expected=0xaa
E (19114) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108
D (19114) sdmmc_init: sdmmc_card_init: sdmmc_init_sd_if_cond returned 0x108
Error ocurred: 16

It looked like something was wrong with the SPI connection with the SD. So I checked the pinout of ESP32S3 provided by Seeed Studio and the current settings.

Then as I thought, the pin numbers weren’t correct, I changed them to the right ones. One note here is the pin number isn’t the No. in the sheet. it is the number from GPIOxx . For example, the pin number for GPIO10 is 10.

One thing I’m not sure is the ss pin. There isn’t a pin labeled for ss . So I used the pin 44 for it. But it worked well with other pins as well… (I need to investigate further)

Afterward, the error had gone. However, I got another one like below.

E (10694) esp-sha: Failed to allocate buf memory
Error ocurred: (-30592, 'MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE')

It looked like the memory was used fully. So I needed to release the memory manually. It sounded very C++ even though I used MicroPython and reminded me of what I studied in university. I quickly searched for how to do it in MicroPython and found some conversations.

Based on the conversations, I did garbage collection by myself after saving a photo in the SD cord. This is my final output.

That’s it!

--

--

Ats
Ats

Written by Ats

I like building something tangible like touch, gesture, and voice. Ruby on Rails / React Native / Yocto / Raspberry Pi / Interaction Design / CIID IDP alumni

No responses yet