8000 SD card FILE_WRITE issue · Issue #214 · earlephilhower/arduino-pico · GitHub
[go: up one dir, main page]

Skip to content
SD card FILE_WRITE issue #214
@kenb2054

Description

@kenb2054

Hi,
I am trying to create a new file for writing on an SD card from my Pico using:

myFile = SD.open("example.txt", FILE_WRITE);

I can successfully initialize the SD Card reader and can open a file for reading and read the data OK but cannot create a new file with the FILE_WRITE command.

I am using code based on the example file known as Files.ino within the Arduino IDE with core ver 1.8.4. I have slightly modified the file to configure the pins for the SPI bus. The fact that I can initialize and read files means the SPI bus is working ok.

Below is the example code I am using and the result is shown further down:

/*
  SD card basic file example
*/
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {

  delay(5000);    //This delay is just to give time for user to open Serial Monitor window
                   //after uploading the sketch
               
  //Configure pins for SPI comms to SD card when used with Raspberry Pi Pico.
  SPI.setRX(0);    // MISO
  SPI.setCS(1);
  SPI.setSCK(2);
  SPI.setTX(3);    // MOSI

  // Open serial communications and wait for port to open:
  Serial.begin(115200);

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

  if (!SD.begin(1)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);        //  I expected this to create a new file but it didn't in core ver 1.8.4
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // delete the file:
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
}

void loop() {
  // nothing happens after setup finishes.
}

The result I am getting is
FILE_WRITE Result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0