🛠 Logger sur carte SD via le port SDIO natif du Teensy 4.1
Objectives
The Teensy 4.1 is the only board in the family with an SD connector under the board, connected to a native 4-bit SDIO port: no SPI bus sharing, no chip-select pin to wire, and a dedicated DMA in the peripheral. This tutorial sets up a data logger capable of writing continuously without blocking the rest of the program.
Hardware
- 1 Teensy 4.1 + micro-USB data cable
- 1 microSD card formatted as FAT32 (cards of 32 GB or less are the safest)
- The sensor of your choice on A0 (Arduino pin 14)
Key points
- Initialization is a single line:
SD.begin(BUILTIN_SDCARD)— the magic constant refers to the internal SDIO connector, not a pin. - For an external SPI sensor (W5500, display...), the SPI bus remains entirely free: that is the main advantage of SDIO.
- The SD library also supports cards on standard SPI pins, but this tutorial targets the onboard connector.
- Always close the file (
close) before reopening it: the file system does not handle multiple handles well.
Complete code
#include <SD.h>
const uint8_t LED = 13;
void setup {
Serial.begin(9600);
while (!Serial && millis < 3000);
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("SD absente (FAT32, <= 32 Go)");
while (true) { digitalWrite(LED, !digitalRead(LED)); delay(200); }
}
Serial.println("SD prête sur SDIO");
}
void loop {
File log = SD.open("log.txt", FILE_WRITE);
if (log) {
log.print(millis);
log.print(" A0=");
log.println(analogRead(14));
log.close;
}
delay(1000);
}
Expected verification
SD ready on SDIO appears on the first run; after reconnecting, log.txt contains one line per second. Remove the card and reconnect the power: LED 13 blinks rapidly, indicating that SD.begin failed — your fallback program is doing its job.
Common errors
- Card formatted as exFAT or NTFS: only FAT32 is guaranteed; reformat it.
- Forgetting
log.closebefore the next open: subsequent lines are lost or corrupt the file. - Expecting UHS-II card speeds: the bus is a standard 4-bit interface — already much faster than the Teensy 3.2's SPI, but no miracles.
- Confusing it with internal flash storage: the 8 MB of QSPI can also store files via LittleFS, but the SD card remains the removable high-capacity solution.
- Neglecting EEPROM write timing: if you extend this logger with
EEPROM.update, note that each write pauses execution from flash (documented by PJRC).
Other tutorials for this board
- Premiers pas sur le Teensy 4.1 : Teensyduino, Teensy Loader et premier Blink
- GPIO et PWM sur le Teensy 4.1 : 55 broches en 3,3 V, ni 5 V tolérées ni traîne
- Entrées analogiques du Teensy 4.1 : 18 broches, 2 ADC, 0-3,3 V fixes et zéro DAC
- Serveur web embarqué : l'Ethernet natif 10/100 du Teensy 4.1 (DP83825)
- Boîte à rythmes USB MIDI : classes MIDI, MIDIx16 et port hôte USBHost_t36
- Ingénierie mémoire du Teensy 4.1 : TCM, DMAMEM, EXTMEM, flash QSPI et EEPROM
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino