/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
analog sensors on analog ins 0, 1, and 2
SD card attached to SPI bus as follows (Raspberry Pi Pico):
** MISO - pin 4
** MOSI - pin 7
** CS - pin 5
** SCK - pin 6
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// This are GP pins for SPI0 on the Raspberry Pi Pico board, and connect
// to different *board* level pinouts. Check the PCB while wiring.
// Only certain pins can be used by the SPI hardware, so if you change
// these be sure they are legal or the program will crash.
// See: https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf
// Include SPI (communication bus) and SD card libraries (for filesystem FAT32)
#include <SPI.h>
#include <SD.h>
// Configure SPI pins
const int _MISO = 16;
const int _MOSI = 19;
const int _CS = 17;
const int _SCK = 18;
// store a byte received from UART
int incomingByte = 0;
// create a file object to interface the write/read to/from sdcard
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial1.begin(115200);
Serial1.println("Press ENTER to start!");
delay(500);
// Wait until a newline character is received
while (incomingByte != '\n') {
if (Serial1.available() > 0) {
incomingByte = Serial1.read();
}
}
// print some info to the uart
Serial1.println("SD card example started!");
// Ensure the SPI pinout the SD card is connected to is configured properly
// SPI.setRX(_MISO);
// SPI.setTX(_MOSI);
// SPI.setSCK(_SCK);
if (!SD.begin(_CS)) {
Serial1.println("initialization failed!");
return;
}
Serial1.println("initialization done.");
}
void loop() {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("file.txt", FILE_WRITE);
// if the file opened okay, write some data:
if (myFile) {
// this goes to the uart. check the Serial monitor
Serial1.print("Writing to file.txt...");
// this goes to the file "altimeter.txt" inside the sdcard
myFile.println("testing 1, 2, 3..............................");
// close the file:
myFile.close();
// this goes to the Serial monitor
Serial1.println("done.");
} else {
// if the file didn't open, print an error:
Serial1.println("error opening file.txt");
}
// re-open the file for reading:
myFile = SD.open("file.txt");
if (myFile) {
Serial1.println("Content of file.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial1.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial1.println("error opening file.txt");
}
// List files inside the SD card root directory
Serial1.println("Listing files in the root directory:");
File root = SD.open("/");
if (root) {
File entry = root.openNextFile();
while (entry) {
Serial1.print(" ");
Serial1.print(entry.name());
if (entry.isDirectory()) {
Serial1.println("/");
} else {
// Files have sizes, directories do not
Serial1.print("\t\t");
Serial1.println(entry.size(), DEC);
}
entry = root.openNextFile();
}
root.close();
} else {
Serial1.println("Failed to open root directory");
}
while(1);
}