Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
The topic of Tarjeta SD, or SD card in English, is important to Arduino users as it allows for data storage and retrieval in projects. SD cards provide a convenient and portable solution for storing large amounts of data, such as sensor readings, log files, or configuration settings. In this article, we will explore how to use an SD card with Arduino, including the necessary adjustments to work with the Arduino environment.
Project: SD Card Data Logger
The project we will create as an example is a simple data logger that records temperature readings from a sensor and stores them on an SD card. The objectives of this project are to demonstrate how to interface an SD card with Arduino and to showcase the capabilities of data logging using external storage.
Components List:
Examples:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
void setup() { Serial.begin(9600);
if (!SD.begin(chipSelect)) { Serial.println("SD card initialization failed!"); return; }
Serial.println("SD card initialized successfully."); }
void loop() { // Main program logic goes here }
In this example, we include the necessary libraries for SD card and SPI communication. We define the chip select pin for the SD card module and then initialize the SD card using the `SD.begin()` function. If the initialization fails, an error message is printed to the serial monitor.
2. Writing Data to SD Card
const int chipSelect = 10; File dataFile;
void setup() { Serial.begin(9600);
if (!SD.begin(chipSelect)) { Serial.println("SD card initialization failed!"); return; }
dataFile = SD.open("data.txt", FILE_WRITE); if (!dataFile) { Serial.println("Error opening data.txt!"); return; }
dataFile.println("Temperature,Time"); dataFile.close();
Serial.println("Data file created successfully."); }
void loop() { // Main program logic goes here }
In this example, we open a file named "data.txt" on the SD card for writing using the `SD.open()` function. If the file opening fails, an error message is printed. We then write a header line to the file and close it. This demonstrates how to create a data file on the SD card.
3. Reading Data from SD Card
const int chipSelect = 10; File dataFile;
void setup() { Serial.begin(9600);
if (!SD.begin(chipSelect)) { Serial.println("SD card initialization failed!"); return; }
dataFile = SD.open("data.txt"); if (!dataFile) { Serial.println("Error opening data.txt!"); return; }
while (dataFile.available()) { Serial.write(dataFile.read()); }
dataFile.close(); }
void loop() { // Main program logic goes here }
In this example, we open the "data.txt" file on the SD card for reading. If the file opening fails, an error message is printed. We then read and print the contents of the file to the serial monitor using the `dataFile.read()` function. Finally, we close the file. This demonstrates how to read data from an existing file on the SD card.