Arduino UNO R4 - Micro SD Kaart

In deze gids leren we hoe u een Micro SD Kaart gebruikt met de Arduino UNO R4. We behandelen de volgende onderwerpen:

Arduino UNO R4 Micro SD Card

Over de Micro SD Kaart Module

De Micro SD Kaart Module verbindt met de Arduino UNO R4 en houdt een Micro SD Kaart vast. Dit betekent dat de Micro SD Kaart Module fungeert als een schakel tussen de Arduino UNO R4 en de Micro SD Kaart.

Pinout

Micro SD Card Module Pinout

De Micro SD Kaart Module heeft 6 pinnen:

  • Verbind de VCC-pin met de 5V-pin op de Arduino UNO R4.
  • Verbind de GND-pin met de GND op de Arduino UNO R4.
  • Verbind de MISO-pin met de MISO-pin op de Arduino UNO R4.
  • Verbind de MOSI-pin met de MOSI-pin op de Arduino UNO R4.
  • Verbind de SCK-pin met de SCK-pin op de Arduino UNO R4.
  • Verbind de SS-pin met de pin die in de Arduino UNO R4 code als SS-pin wordt geïdentificeerd.

Voorbereiding

  • Steek de Micro SD Kaart in uw computer met behulp van een USB 3.0 SD Kaart Reader.
  • Controleer dat de Micro SD Kaart geformatteerd is als FAT16 of FAT32. U kunt online zoeken om uit te vinden hoe u dit kunt doen.

Bedradingsdiagram

Arduino UNO R4 Micro SD Card Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Zie De beste manier om Arduino Uno R4 en andere componenten van stroom te voorzien.

※ Notiz:

Als u een Ethernet shield of een andere shield met een Micro SD Kaart Houder heeft, hoeft u de Micro SD Kaart Module niet te gebruiken. Plaats gewoon de Micro SD Kaart in de Micro SD Kaart Houder op de shield.

Arduino UNO R4 - Hoe een bestand op Micro SD Kaart openen en aanmaken indien niet bestaat

Arduino UNO R4 Code

Snelle Stappen

Volg deze instructies stap voor stap:

  • Als dit de eerste keer is dat u de Arduino Uno R4 WiFi/Minima gebruikt, raadpleeg dan de tutorial over het instellen van de omgeving voor Arduino Uno R4 WiFi/Minima in de Arduino IDE.
  • Plaats de Micro SD Kaart in de Micro SD Kaart module
  • Volg het bedradingsdiagram om de Micro SD Kaart module te verbinden met de Arduino UNO R4
  • Verbind het Arduino Uno R4 board met uw computer via een USB-kabel.
  • Start de Arduino IDE op uw computer.
  • Selecteer het juiste Arduino Uno R4 board (bijvoorbeeld Arduino Uno R4 WiFi) en COM-poort.
  • Open de Serial Monitor in de Arduino IDE
  • Kopieer de verstrekte code en plak deze in de Arduino IDE
/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); if (!SD.exists("arduino.txt")) { Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("arduino.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("arduino.txt")) Serial.println(F("arduino.txt exists on SD Card.")); else Serial.println(F("arduino.txt doesn't exist on SD Card.")); } void loop() { }
  • Druk op de Upload knop in de Arduino IDE om de code over te dragen naar de Arduino UNO R4.
  • Controleer de Serial Monitor voor het resultaat na de eerste upload.
COM6
Send
SD Card is ready arduino.txt doesn't exist. Creating arduino.txt file... arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Het resultaat weergegeven op de Serial Monitor voor volgende pogingen
COM6
Send
SD Card is ready arduino.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Notiz:

U ziet mogelijk geen output op de Serial Monitor als u deze opent na de eerste upload.

  • Verwijder de Micro SD Kaart uit de module
  • Plaats de Micro SD Kaart in een USB SD Kaart reader
  • Sluit de USB SD Kaart reader aan op uw computer
  • Controleer of het bestand er is of niet

Arduino UNO R4 - Hoe data schrijven naar/lezen van een bestand op Micro SD Kaart

Deze code voert de volgende acties uit:

  • Informatie opslaan in een bestand
  • Het bestand laden en elk karakter één voor één weergeven op de Serial Monitor
/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
  • De Serial Monitor toonde de inhoud van het bestand.
COM6
Send
Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Notiz:

Standaard wordt de data toegevoegd aan het einde van het bestand. Als u de Arduino UNO R4 herstart met de gegeven code, wordt de tekst opnieuw toegevoegd aan het bestand. Dit betekent dat u aanvullende regels zoals deze zult zien op de Serial Monitor:

COM6
Send
Created by newbiely.com Learn Arduino UNO R4 and SD Card Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

U kunt ook de Micro SD Kaart uit de module verwijderen en een USB SD Kaart reader gebruiken om de bestanden op uw computer te bekijken.

Arduino UNO R4 - Hoe een bestand op Micro SD Kaart regel-voor-regel lezen

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { int line_count = 0; while (myFile.available()) { char line[100]; // maximum is 100 characters, change it if needed int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // print the character to Serial Monitor // \n character is escaped by readBytesUntil function Serial.write('\n'); // print a new line charactor } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
  • Het resultaat weergegeven op de Serial Monitor
COM6
Send
SD Card is ready Line 1: Created by newbiely.com Line 2: Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ Notiz:

Als u de bestandsinhoud niet eerder heeft verwijderd, ziet u mogelijk aanvullende regels op de Serial Monitor.

Arduino UNO R4 - Hoe een bestand op Micro SD Kaart overschrijven

Normaal gesproken wordt de inhoud toegevoegd aan het einde van het bestand. Om een bestand te vervangen, verwijdert u eerst het oude bestand en maakt u vervolgens een nieuw bestand aan met dezelfde naam.

/* * Deze Arduino UNO R4 code is ontwikkeld door newbiely.nl * Deze Arduino UNO R4 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-uno-r4/arduino-uno-r4-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 // The Arduino UNO R4 pin connected to the CS pin of SDcard module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); SD.remove("arduino.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to Arduino myFile.println("Learn Arduino and SD Card"); // write another line to Arduino myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
  • De output op de Serial Monitor
COM6
Send
SD Card is ready Created by newbiely.com Learn Arduino UNO R4 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Herstart Arduino UNO R4
  • Controleer of de inhoud van het bestand in Serial Monitor is toegevoegd.

U kunt ook de Micro SD Kaart uit de eenheid verwijderen en een USB SD Kaart reader gebruiken om de inhoud ervan op uw computer te bekijken.

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

Opmerkingen

Gerelateerde Tutorials

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!