ESP32 – Micro SD Card Interface

by PugazhM in Circuits > Wireless

25718 Views, 5 Favorites, 0 Comments

ESP32 – Micro SD Card Interface

ESP32_Circuit_V01.jpeg

The SD card module provides the micro memory card interface and it is connected to ESP32 via SPI port. This experiment, creates a “sample text file” inside the SD card, and then written with the running text. ESP32 opens the “sample text file” for reading and dump the text on debug port.

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos.
https://www.youtube.com/channel/UCY4mekPLfeFinbQHp...

ESP32 – Micro SD Card Interface implementation video

Abstract

The ESP32 is constructed with Xtensa dual-core (or single-core) 32-bit RISC architecture, operating at 160MHz or 240MHz, with up to 520KiB internal SRAM, integrated with Wi-Fi (802.11 b/g/n) and Bluetooth (v4.2 BR/EDR and BLE (shares the radio with Wi-Fi). The ESP32 provides peripheral interfaces like ADC, DAC, touch sensors, SPI, I2C, I2S, UART, CAN, PWM etc. The ESP32 supports all IEEE 802.11 standard security feature (WFA, WPA/WPA2 and WAPI), and cryptographic hardware acceleration (AES, RSA, SHA-2, ECC, RNG etc). It supports wake up from GPIO / Sensor interrupts, timers and 5uA deep sleep current consumption.

Secure Digital Card, abbreviated as SD Card is a non-volatile memory card, commonly used to write and read large quantities of data in smart devices. These days SD cards are available with 4GB to 128GB memory size.

The SD card module provides the micro memory card interface and it is connected to ESP32 via SPI port. This experiment, creates a “sample text file” inside the SD card, and then written with the running text. ESP32 opens the “sample text file” for reading and dump the text on debug port.

Reference

“ESP32 – Getting Started MicroPython -- on Board Blink LED” Instruct-able / You-tube by PugazhM https://www.instructables.com/ESP32-Getting-Start...

MicroPython “sdcard.py” library

https://github.com/micropython/micropython/blob/m...

Components

Component.jpg

ESP32 Development Board

Micro USB Cable

SD Card Module = 1 No

8GB micro-SD card = 1 No

Schematic

ESP32_Circuit_V01.jpeg

SD Card module provides “micro-SD card socket”, for inserting the memory card and then provides SPI interface pins (MOSI = GPIO12, MISO = GPIO13, SCLK = GPIO14 and CS = GPIO27), for connecting it into ESP32 board.

SD Card module converts the +5VDC supply into 3.3VDC using AM1117, 3.3VDC regulator and converts the 5VDC logic Pins into 3.3VDC logic pins by using “74ABT125PW, Quad Buffer Tri state logic converter”.

The micro-SD card should be powered and operated with 3.3VDC. Otherwise, connecting the 5VDC logic into microSD card, can permanently damage the memory card.

This experiment uses an 8GB micro-SD card and it must be formatted (FAT32) on Windows 10 environment.

Python Program – SD Card Interface Module

TaskEvent_V01.jpeg

Open the ESP32 as drive, and then copy the library into root directory.

The timer_one is initialized and callbacks the “BlinkLED” functionality for toggling on board LED at 200mS duration. (frequency = 5)

MicroPython implements a Unix-like Virtual File System (VFS) layer. All mounted filesystems are combined into a single virtual filesystem, starting at the root /. Filesystems are mounted into directories in this structure, and at startup the working directory is changed to where the primary filesystem is mounted.

The main advantage of the FAT filesystem is that it can be accessed over USB MSC on supported boards without any additional drivers required on the host PC.

Download and store the MicroPython “sdcard.py” library into ESP32.

The file open functionality open(), can take following arguments for write, read and append a file.

w -- Open a file for writing the ASCII text. If a file is already existing, then it rewrites the file.

r -- Open a file for reading the ASCII text.

a -- Open a file for appending the ASCII text at the end.

wb -- Open a file for writing binary data.

rb -- Open a file for reading binary data.

MicroPython SPl, sdcard and os libraries are used for constructing the experiment.

MicroPython ESP32 debug port is used for printing the debug statements.

During startup, the SPI bus and SD card is initialized.

Initially a “sample.txt” file is created / opened with FILE_WRITE option.

If file open is success, then the program writes running numbers (0 to19), into “sample.txt”, and then closes.

The program once again opens the “sample.txt” with FILE_APPEND option, and appends a text at the end.

Then the program once again opens the “sample.txt” with FILE_READ option, reads the “sample.txt” and send data to the debug port, which prints text on connected debug terminal.


'''
 # Demonstrates ESP32 interface to MicroSD Card Adapter
 # Create a text file and write running numbers.
 # Open text file, read and print the content on debug port
   
 * The ESP32 pin connections for MicroSD Card Adapter SPI
 
 # MicroSD Card Adapter Power Pins
 * MicroSD VCC pin to ESP32 +5V
 * MicroSD GND pin to ESP32 GND
 
 # MicroSD SPI Pins
 * MicroSD MISO pin to ESP32 GPIO13
 * MicroSD MOSI pin to ESP32 GPIO12
 * MicroSD SCK pin to ESP32 GPIO14
 * MicroSD CS pin to ESP32 GPIO27
 
 Name:- M.Pugazhendi
 Date:-  20thOct2021
 Version:- V0.1
 e-mail:- muthuswamy.pugazhendi@gmail.com
'''
import machine
from machine import Pin, SPI, SoftSPI
import sdcard
import os
 
toggle = 0
 
#Initialize the onboard LED as output
led = machine.Pin(2,machine.Pin.OUT)

# Toggle LED functionality
def BlinkLED(timer_one):
    global toggle
    if toggle == 1:
        led.value(0)
        toggle = 0
    else:
        led.value(1)
        toggle = 1

# Initialize the SD card
spi=SoftSPI(1,sck=Pin(14),mosi=Pin(12),miso=Pin(13))
sd=sdcard.SDCard(spi,Pin(27))

# Create a instance of MicroPython Unix-like Virtual File System (VFS),
vfs=os.VfsFat(sd)
 
# Mount the SD card
os.mount(sd,'/sd')

# Debug print SD card directory and files
print(os.listdir('/sd'))

# Create / Open a file in write mode.
# Write mode creates a new file.
# If  already file exists. Then, it overwrites the file.
file = open("/sd/sample.txt","w")

# Write sample text
for i in range(20):
    file.write("Sample text = %s\r\n" % i)
    
# Close the file
file.close()

# Again, open the file in "append mode" for appending a line
file = open("/sd/sample.txt","a")
file.write("Appended Sample Text at the END \n")
file.close()

# Open the file in "read mode". 
# Read the file and print the text on debug port.
file = open("/sd/sample.txt", "r")
if file != 0:
    print("Reading from SD card")
    read_data = file.read()
    print (read_data)
file.close()

# Initialize timer_one. Used for toggling the on board LED
timer_one = machine.Timer(0)

# Timer one initialization for on board blinking LED at 200mS interval
timer_one.init(freq=5, mode=machine.Timer.PERIODIC, callback=BlinkLED)

Downloads

Conclusion

C1.jpg
C2.jpg

The experiment is successfully executed with ESP32, SD Card Module, and 8GB micro-SD card

A “sample.txt” file is created, written with running numbers and then readback / printed the text via debug port.

Results Video and Links

Result_V01.jpg

Please Visit You Tube for the Videos:,

Visit the "VESZLE - The Art Of Electronics" you tube channel for further information and videos. https://www.youtube.com/channel/UCY4mekPLfeFinbQH...

ESP32 -- Micro SD Card Implementation video

https://youtu.be/08MXeM0Qb8Y