top of page

Arduino SD Card Tutorial: Data Logging and File Handling

Introduction:

SD (Secure Digital) cards are commonly used with Arduino for data logging, storing sensor data, and creating standalone data acquisition systems. In this tutorial, we'll guide you through the process of using an SD card module with Arduino to read and write data to an SD card.

Materials You'll Need:

  1. Arduino board (e.g., Arduino Uno)

  2. SD card module (e.g., Micro SD card module with SPI interface)

  3. Micro SD card

  4. Jumper wires

  5. Breadboard (optional)

Step 1: Wiring Connections

Connect the SD card module to the Arduino as follows:

  • SD Module VCC to Arduino 5V

  • SD Module GND to Arduino GND

  • SD Module MISO to Arduino MISO (pin 12)

  • SD Module MOSI to Arduino MOSI (pin 11)

  • SD Module SCK to Arduino SCK (pin 13)

  • SD Module CS to a digital pin on the Arduino (e.g., pin 10)

Step 2: Install the SD Library

To work with SD cards, you need the "SD" library. Open the Arduino IDE, navigate to "Sketch" > "Include Library" > "Manage Libraries..." and search for "SD." Click "Install" to add the library to your Arduino IDE.


Step 3: Write the Arduino Code

Here's a basic example of Arduino code for reading and writing data to an SD card. In this example, we'll create a simple data logging system that records values to a text file on the SD card.



#include <SD.h>


File dataFile;

const int chipSelect = 10; // Define the CS pin


void setup() {

// Open a serial connection

Serial.begin(9600);

// Initialize the SD card

if (SD.begin(chipSelect)) {

Serial.println("SD card is ready to use.");

} else {

Serial.println("SD card initialization failed.");

return;

}

// Create a new file on the SD card

dataFile = SD.open("datalog.txt", FILE_WRITE);

if (dataFile) {

Serial.println("File created successfully.");

dataFile.close();

} else {

Serial.println("Error creating file.");

}

}


void loop() {

// Read sensor data (replace with your own sensor data)

float sensorValue = analogRead(A0) * 0.00488; // Example: Reading analog data

// Open the file in append mode

dataFile = SD.open("datalog.txt", FILE_WRITE);

if (dataFile) {

// Write sensor data to the file

dataFile.print(sensorValue);

dataFile.println();

dataFile.close();

// Print the data to the serial monitor

Serial.print("Data recorded: ");

Serial.println(sensorValue);

delay(1000); // Delay for demonstration purposes

} else {

Serial.println("Error opening file.");

}

}


Step 4: Customize the Code

  • Replace the sensor reading code with your own sensor data.

  • You can modify the file name ("datalog.txt") or file format as needed.

  • Customize the data logging frequency using the delay() function.

Step 5: Upload and Run the Code

Upload the code to your Arduino and open the Serial Monitor (Tools > Serial Monitor). You should see the sensor data being logged to the SD card and displayed in the Serial Monitor.


Step 6: Access the Data

After recording data, you can remove the SD card, insert it into a card reader, and access the data on your computer.


Conclusion:

You've successfully learned how to use an SD card with Arduino for data logging and file handling. This opens up possibilities for various data recording and storage applications, such as environmental monitoring, weather stations, or even creating your own standalone data logger. Experiment further to integrate other sensors or enhance the functionality of your data logging system.


90 views0 comments

Recent Posts

See All

Commentaires


bottom of page