3.6 SD Card

Status
Not open for further replies.

rfresh737

Well-known member
I've been reading up on using the SD Card on the 3.6. I installed the SdFat.h library.

Is there a sample sketch that shows how to read and write files? I tried a couple I found but they were not written for the 3.6 and initialization failed with those sketches.

A post on this forum included a sketch to read the structure of my SD Card and it ran OK, displaying all the parameters in the monitor window. So I think my SD Card is formatted and ready to go.

Thank you...
 
I found this sketch to read and write to files on the SD Card.

Here is the serial monitor output:

Code:
Initializing SD card.
Reading ---------------------------------------
ScreenResolution.txt not found.
Writing ---------------------------------------
ScreenResolution.txt not found.

Here is the sketch:

Code:
/*
  SD card basic file example
 
 This example shows how to create and destroy an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11, pin 7 on Teensy with audio board
 ** MISO - pin 12
 ** CLK - pin 13, pin 14 on Teensy with audio board
 ** CS - pin 4, pin 10 on Teensy with audio board
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
#include <SD.h>
#include <SPI.h>

String str1;
File myFile;

const int chipSelect = BUILTIN_SDCARD;

void setup()
{
 //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
 //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
 //SPI.setSCK(14);  // Audio shield has SCK on pin 14
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial & millis() <4000)
  {
  }
  Serial.println("\nInitializing SD card.");
  if (!SD.begin(chipSelect))
  {
    Serial.println("initialization failed!");
    return;
  }

	Serial.println("Reading ---------------------------------------");
	if (SD.exists("ScreenResolution.txt"))
	{
		myFile = SD.open("ScreenResolution.txt");
		if (myFile)
		{
			while (myFile.available())
			{
				str1 = myFile.readStringUntil('\n');
				Serial.println(str1);
			}
			myFile.close();
		}
	}
	else
	{
		Serial.println("ScreenResolution.txt not found.");
	}

	Serial.println("Writing ---------------------------------------");

	if (SD.exists("ScreenResolution.txt"))
	{
		myFile = SD.open("ScreenResolution.txt", FILE_WRITE);
		if (myFile)
		{
			myFile.println("1920");
      myFile.println("1080");
			myFile.close();
		}
	}
	else
	{
		Serial.println("ScreenResolution.txt not found.");
	}
}


void loop()
{
  // nothing happens after setup finishes.
}
 
Status
Not open for further replies.
Back
Top