🛠 Logger sur carte microSD en SPI (mode carte)
Objectives
Make your RP2040 board standalone: write files to a microSD card in SPI mode. The RP2040 features 2 SPI controllers (datasheet) and MicroPython includes a generic SD driver (the sdcard module from micropython-lib) that speaks the SD SPI mode (initialization at ~400 kHz followed by faster transfers). Autonomy bonus: the microSD card operates natively at 3.3 V, so no level shifter is needed.
Hardware
- 1 × generic RP2040 board
- 1 × microSD reader module with integrated regulator or 3.3 V compatible (most breakouts are)
- SPI0 wiring: GP5 = SCK, GP3 = MOSI (DI), GP4 = MISO (DO), GP2 = CS (standard SPI0 assignments on the RP2040, routable elsewhere)
- microSD card formatted as FAT32 (cards > 32 GB use exFAT: not readable, use 32 GB max or reformat to FAT32)
sdcard.pymodule copied to the board (from micropython-lib)
Complete code
# logger.py — monte la SD et journalise la température interne
import os, time, machine
from machine import SPI, Pin
import sdcard
spi = SPI(0, baudrate=400_000, polarity=0, phase=0,
sck=Pin(5), mosi=Pin(3), miso=Pin(4))
cs = Pin(2, Pin.OUT, value=1)
sd = sdcard.SDCard(spi, cs)
fs = os.VfsFat(sd) # monte le système de fichiers
os.mount(fs, '/sd')
t0 = time.ticks_ms
adc = machine.ADC(4) # canal 4 = capteur thermique interne
with open('/sd/log.csv', 'a') as f:
while True:
ms = time.ticks_diff(time.ticks_ms, t0)
mv = adc.read_u16 * 3.3 / 65535
t = 27.0 - (mv - 0.706) / 0.001721 # formule datasheet RP2040
f.write('%d,%.2f\n' % (ms, t))
f.flush # écrit réellement la carte
time.sleep_ms(1000)
Expected verification
After a few seconds, remove the card and read it on a PC: log.csv contains one line per second with the timestamp in ms and the temperature (~27–30 °C at rest; the internal sensor formula, 0.706 V and −1.721 mV/°C, comes from the "Temperature Sensor" section of the RP2040 datasheet). Rerun the script: the file continues to grow (mode "a") — persistence works.
Common errors
OSError: [Errno 22] EINVALduring mounting: card is exFAT/NTFS — reformat to FAT32.- Initialization too fast: some cards require an init clock of ~400 kHz; the driver handles this, but if it fails, test at 100 kHz.
- MISO not connected or CS on an occupied pin: the driver never receives the command token 0xFE →
sdcardinit error. - Forgetting
f.flush(or thewithcontext): data remains in the MicroPython buffer and disappears upon reset. - Removing the card during writing: FAT corruption — unmount cleanly (
os.umount('/sd')). - 5 V power supply on a reader without a regulator: SD cards are limited to ~3.6 V — this damages the card, literally.
Other tutorials for this board
- Piloter la LED RGB embarquée WS2812 d'une carte RP2040 générique
- Avoir une console série sur une carte sans pont USB (UART0)
- Jouer une mélodie avec un buzzer passif et le PWM
- Afficher sur un OLED I2C SSD1306 et déboguer le bus
- Interruptions GPIO et alarmes matérielles avec le Pico SDK
- Transformer la carte en clavier USB HID avec TinyUSB
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython