Ordinato entro le 14:00 nei giorni lavorativi = spedito in giornata 30 giorni per il recesso Garanzia di 1 anno
← Torna alla panoramica

Schema di collegamento ed esempio di codice

Ecco come collegare il Lettore di schede Micro SD per Arduino a un Arduino UNO o ESP32, con uno sketch di esempio che puoi caricare subito.

Leggere e scrivere con il lettore di schede Micro-SD

Questo progetto mostra come collegare un lettore di schede Micro-SD 5V a un Arduino Uno utilizzando la comunicazione SPI. Lo sketch inizializza la scheda SD, scrive una riga di testo in un file chiamato test.txt e legge il file nel Monitor Seriale. Apri il Monitor Seriale a 115200 baud per visualizzare l'output.

Schema di collegamento: Lettore di schede Micro SD per Arduino su un Arduino UNO
Schema di collegamento · Clicca per ingrandire
Tabella di collegamento: Lettore di schede Micro SD per Arduino su un Arduino UNO
Tabella di collegamento · Clicca per ingrandire
Read_and_Write_with_Micro_SD_Card_Reader.ino · compilato e testato
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : Read and Write with Micro-SD Card Reader
 *  Board     : Arduino UNO (arduino:avr:uno)
 *  Parts     : Micro-SD Card Reader 5V
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // Wait for serial port to connect
  }

  Serial.println(F("Initializing SD card..."));

  if (!SD.begin(chipSelect)) {
    Serial.println(F("Initialization failed! Check wiring and FAT16/FAT32 format."));
    return;
  }
  Serial.println(F("Initialization done."));

  // Write data to the card
  File myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    Serial.print(F("Writing to test.txt..."));
    myFile.println(F("Hello from Arduino UNO!"));
    myFile.close();
    Serial.println(F(" done."));
  } else {
    Serial.println(F("Error opening test.txt for writing."));
  }

  // Read data back from the card
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println(F("--- Content of test.txt ---"));
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
    Serial.println(F("--- End of file ---"));
  } else {
    Serial.println(F("Error opening test.txt for reading."));
  }
}

void loop() {
  // Demo runs once in setup
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online

Demo di lettura e scrittura della scheda MicroSD ESP32

Collega il modulo scheda MicroSD 5V all'ESP32 usando un convertitore di livello logico a 4 canali per garantire una comunicazione SPI sicura a 3.3V. Lo sketch inizializza la scheda SD sul bus VSPI, crea e scrive un file di test e ne legge il contenuto tramite il Serial Monitor a 115200 baud.

Schema di collegamento: Lettore di schede Micro SD per Arduino su un ESP32 DevKit V1
Schema di collegamento · Clicca per ingrandire
Tabella di collegamento: Lettore di schede Micro SD per Arduino su un ESP32 DevKit V1
Tabella di collegamento · Clicca per ingrandire
ESP32_MicroSD_Card_Read_and_Write_Demo.ino · compilato e testato
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : ESP32 MicroSD Card Read and Write Demo
 *  Board     : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
 *  Parts     : Micro-SD Card Reader 5V
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

#include "FS.h"
#include "SD.h"
#include "SPI.h"

const int chipSelect = 5;

void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(10); }

  Serial.println("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card Mount Failed! Check wiring and ensure a FAT32 card is inserted.");
    return;
  }

  uint8_t cardType = SD.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached.");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) Serial.println("MMC");
  else if (cardType == CARD_SD) Serial.println("SDSC");
  else if (cardType == CARD_SDHC) Serial.println("SDHC");
  else Serial.println("UNKNOWN");

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);

  // Write to a test file
  Serial.println("Writing to test.txt...");
  File file = SD.open("/test.txt", FILE_WRITE);
  if (file) {
    file.println("Hello from ESP32 and MicroSD Card!");
    file.close();
    Serial.println("Write complete.");
  } else {
    Serial.println("Error opening file for writing.");
  }

  // Read the test file back
  Serial.println("Reading test.txt:");
  file = SD.open("/test.txt");
  if (file) {
    while (file.available()) {
      Serial.write(file.read());
    }
    file.close();
  } else {
    Serial.println("Error opening file for reading.");
  }
}

void loop() {
  // Idle
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online

Visti di recente

Chassis con 4 ruote e 4 motori TT e portabatterie

Chassis con 4 ruote e 4 motori TT e portabatterie

€ 14,99
Modulo MP3 seriale UART TTL con slot Micro SD (alternativa YX5300)

Modulo MP3 seriale UART TTL con slot Micro SD (alternativa YX5300)

€ 5,80
Portabatterie per 6 batterie AA (9V)

Portabatterie per 6 batterie AA (9V)

€ 2,10
Robot Linking Creative Keyboard Kit (Clone di Makey Makey in lingua inglese))

Robot Linking Creative Keyboard Kit (Clone di Makey Makey in lingua inglese))

€ 16,65
Modulo di evitamento ostacoli a infrarossi HW-201

Modulo di evitamento ostacoli a infrarossi HW-201

€ 1,25
Amplificatore mono da 18 Watt TDA2030

Amplificatore mono da 18 Watt TDA2030

€ 2,50
Kit assortimento oscillatori al cristallo - 9 tipi

Kit assortimento oscillatori al cristallo - 9 tipi

€ 4,70
Punta di saldatura Velleman per VTSS4 e VTSS5

Punta di saldatura Velleman per VTSS4 e VTSS5

€ 2,95
Vedi
Cavo USB-A a Micro-USB 1 metro (Per ESP8266, ESP32 e Wemos)

Cavo USB-A a Micro-USB 1 metro (Per ESP8266, ESP32 e Wemos)

€ 2,00