Zamówienie w dzień roboczy przed 14:00 = wysyłka tego samego dnia 30 dni na odstąpienie od umowy 1 rok gwarancji
← Wróć do przeglądu

Schemat podłączenia i przykład kodu

Tak podłączysz Czytnik kart Micro SD do Arduino do Arduino UNO lub ESP32, z przykładowym szkicem, który możesz od razu wgrać.

Odczyt i zapis za pomocą czytnika kart Micro-SD

Ten projekt pokazuje, jak podłączyć czytnik kart Micro-SD 5V do Arduino Uno za pomocą komunikacji SPI. Sketch inicjalizuje kartę SD, zapisuje linię tekstu do pliku o nazwie test.txt, a następnie odczytuje plik z powrotem w Serial Monitor. Otwórz Serial Monitor z prędkością 115200 baud, aby zobaczyć wynik.

Schemat podłączenia: Czytnik kart Micro SD do Arduino na Arduino UNO
Schemat podłączenia · Kliknij, aby powiększyć
Tabela połączeń: Czytnik kart Micro SD do Arduino na Arduino UNO
Tabela połączeń · Kliknij, aby powiększyć
Read_and_Write_with_Micro_SD_Card_Reader.ino · skompilowane i przetestowane
/*
 * ==================================================================
 *  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

Przykład odczytu i zapisu karty MicroSD dla ESP32

Podłącz moduł karty MicroSD 5V do ESP32 za pomocą 4-kanałowego konwertera poziomów logicznych, aby zapewnić bezpieczną komunikację SPI 3.3V. Sketch inicjalizuje kartę SD na magistrali VSPI, tworzy i zapisuje plik testowy, a następnie odczytuje jego zawartość w Serial Monitor przy 115200 baud.

Schemat podłączenia: Czytnik kart Micro SD do Arduino na ESP32 DevKit V1
Schemat podłączenia · Kliknij, aby powiększyć
Tabela połączeń: Czytnik kart Micro SD do Arduino na ESP32 DevKit V1
Tabela połączeń · Kliknij, aby powiększyć
ESP32_MicroSD_Card_Read_and_Write_Demo.ino · skompilowane i przetestowane
/*
 * ==================================================================
 *  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

Ostatnio oglądane

Moduł Bluetooth HC-05 6pin CSR BC417

Moduł Bluetooth HC-05 6pin CSR BC417

€ 12,95
Elektromagnes DC12V 15KG

Elektromagnes DC12V 15KG

€ 10,40
CP2102 USB do TTL Serial UART Bridge 3.3v-5v

CP2102 USB do TTL Serial UART Bridge 3.3v-5v

€ 3,65
MAX3232 RS232 do TTL moduł szeregowy

MAX3232 RS232 do TTL moduł szeregowy

€ 1,80
Elektromagnes DC12V 100KG

Elektromagnes DC12V 100KG

€ 33,90
Nano V3 Arduino Compatible CH340

Nano V3 Arduino Compatible CH340

€ 6,60
Elektromagnes DC12V 60KG

Elektromagnes DC12V 60KG

€ 18,70