Archive | November, 2020

Reading Data From SD Card to Serial Terminal [Arduino]

3 Nov

Good day, its been years since my last post.

As i browse the net, im looking for way to download SD card data directly to my laptop without needing to unplug the sd card from arduino sd card reader.

This save me a lot of trouble and reduce wear and tear on those cheap card reader

My card reader:

MicroSD Card Reader (SPI Communication)

And my interface is with Arduino Mega (you can use any arduino board as long as the connection is to the SPI and no other SPI device connected)

The code below written for Arduino UNO i think hence the pin attachment. For mega it would be at:

Arduino Mega double pin layout

Code:

/*
  SD card file dump

  This example shows how to read a file from the SD card using the
  SD library and send it over the serial port.

  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  This example code is in the public domain.

*/

#include 
#include 

// const int chipSelect = 4;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  // see if the card is present and can be initialized:
  if (!SD.begin()) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("data.csv");

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening data.csv");
  }
}

void loop() {
}

So Basically this would try to find data.csv and read it. you guys can change it to any filename stored.