ESP32 MicroPython Read Config from SD Card

This guide shows you how to load settings from a configuration file stored on a Micro SD Card using ESP32 and MicroPython. Here is what we will cover:

With this approach, you can manage your project settings through a simple text file on the Micro SD Card, supporting various data types including integers, floats, and strings.

ESP32 MicroPython Micro SD Card Config File

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×Optionally, MicroSD to SD Memory Card Adapter
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Micro SD Card Module

New to the Micro SD Card Module? Find out everything about its pins, how it works, and how to program it in the ESP32 MicroPython - Micro SD Card tutorial.

How It Works

The configuration file on the Micro SD Card follows a simple structure:

  • Each setting occupies its own line — key-value pairs are separated by newline characters.
  • Within each line, the key and value are separated by the = character.

The MicroPython code scans the file for a specific key, extracts the matching value, and assigns it to a variable. The value can be converted to int, float, or string as needed.

Wiring Diagram

The wiring diagram between ESP32 MicroPython Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

The wiring table below shows how to connect the Micro SD Card Module to the ESP32:

Micro SD Card Module ESP32
VCC 5V
GND GND
MISO GPIO19
MOSI GPIO23
SCK GPIO18
CS GPIO5

※ NOTE THAT:

If your shield (such as an Ethernet shield) already has a built-in Micro SD Card slot, you can skip the external module and simply insert the card directly into the shield.

How to Load Config Settings into Variables

  • Create a config.txt file on your PC using Notepad or Notepad++
  • Copy the following key-value pairs into the config.txt file:
myString_1=Hello myString_2=newbiely.com myInt_1=2 myInt_2=-105 myFloat_1=0.74 myFloat_2=-46.08
  • Plug the Micro SD Card into a USB 3.0 SD Card Reader and connect it to your PC
  • Verify that the Micro SD Card uses FAT16 or FAT32 format (search online for instructions if needed)
  • Transfer the config.txt file to the root directory of the Micro SD Card
  • Safely remove the Micro SD Card from the PC
  • Attach the Micro SD Card to the ESP32 through the Micro SD Card Module following the wiring diagram above
  • Copy the MicroPython code below
""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-read-config-from-sd-card """ import machine import os import sdcard # Define SPI pins for the Micro SD Card module SPI_SCK_PIN = 18 # The ESP32 pin GPIO18 connected to SCK SPI_MOSI_PIN = 23 # The ESP32 pin GPIO23 connected to MOSI SPI_MISO_PIN = 19 # The ESP32 pin GPIO19 connected to MISO SPI_CS_PIN = 5 # The ESP32 pin GPIO5 connected to SS (CS) FILE_NAME = "/sd/config.txt" # Variables my_int_1 = 0 my_int_2 = 0 my_float_1 = 0.0 my_float_2 = 0.0 my_string_1 = "" my_string_2 = "" def sd_find_key(key): """Search for a key in the config file and return its value as a string.""" try: f = open(FILE_NAME, "r") for line in f: line = line.strip() if "=" in line: k, v = line.split("=", 1) if k == key: f.close() return v f.close() except Exception as e: print("SD Card: Issue encountered while attempting to open the file", FILE_NAME, e) return None def sd_find_int(key): """Read a key from config file and return as int. """ value = sd_find_key(key) if value is not None: return int(value) return 0 def sd_find_float(key): """Read a key from config file and return as float.""" value = sd_find_key(key) if value is not None: return float(value) return 0.0 def sd_find_string(key): """Read a key from config file and return as string. """ value = sd_find_key(key) if value is not None: return value return "" # Initialize SD card using SoftSPI and sdcard driver spi = machine.SoftSPI(baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(SPI_SCK_PIN), mosi=machine.Pin(SPI_MOSI_PIN), miso=machine.Pin(SPI_MISO_PIN)) cs = machine.Pin(SPI_CS_PIN, machine.Pin.OUT) try: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd") print("SD Card initialized.") except Exception as e: print("SD CARD FAILED, OR NOT PRESENT!", e) raise SystemExit my_int_1 = sd_find_int("myInt_1") my_int_2 = sd_find_int("myInt_2") my_float_1 = sd_find_float("myFloat_1") my_float_2 = sd_find_float("myFloat_2") my_string_1 = sd_find_string("myString_1") my_string_2 = sd_find_string("myString_2") print("myInt_1 =", my_int_1) print("myInt_2 =", my_int_2) print("myFloat_1 =", my_float_1) print("myFloat_2 =", my_float_2) print("myString_1 =", my_string_1) print("myString_2 =", my_string_2) os.umount("/sd")

Here's instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search "sdcard", then find the sdcard library provided by micropython-lib.
  • Click on sdcard, then click Install button to install the SD card library.
ESP32 MicroPython SD Card library
  • Paste the above MicroPython code into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot SD Card initialized. myInt_1 = 2 myInt_2 = -105 myFloat_1 = 0.74 myFloat_2 = -46.08 myString_1 = Hello myString_2 = newbiely.com
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡
  • The Shell displays the parsed values, which match exactly what was saved in the config file on the Micro SD Card.

Feel free to extend the code by adding more key-value pairs to suit your project.

※ NOTE THAT:

The code does not depend on the order of entries in the config file. It searches from the top of the file to the bottom until it finds a matching key.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!