This article will guide you through the process of using an SD card with your Raspberry Pi Pico, delving into the SPI communication standard and the SD library, and providing a practical example of data logging and retrieval.
MOSI (Master Out Slave In): The master device transmits data to the slave device.MISO (Master In Slave Out): The slave device sends data back to the master.SCK (Clock): This signal, generated by the master, controls the timing of data transfer.
CPOL (Clock Polarity): This defines whether the clock signal is idle high or low. A CPOL of 0 indicates a low idle state, while a CPOL of 1 indicates a high idle state.CPHA (Clock Phase): This determines when data is transmitted relative to the clock signal's transitions. For CPHA = 0, the first data bit is sent immediately after the CS line becomes active. However, for CPHA = 1, the first data bit is sent on the first clock edge after CS activation.
Maximum File Size: Up to 4 GB per file.Volume Size: Supports volumes up to 2 TB.32-bit Entries: This structure allows for efficient access and management of files and directories.
SD Card Module —Raspberry Pi Pico: GND —GND +3.3V —3V3OUT CS —SPI0 CSn MOSI —SPI0 TX SCK —SPI0 SCK MISO —SPI0 RX
Connect the SD Card: Insert your SD card into your computer's SD card slot or an external SD card reader.Access Disk Management: On Windows, navigate to "This PC," right-click the SD card drive, and select "Format."Select FAT32: Ensure "FAT32" is chosen as the file system.Start Formatting: Click "Start" and confirm the formatting process, understanding that all data on the SD card will be erased.
Install the SD Library: Open the Arduino IDE and navigate to "Sketch" > "Include Library" > "Manage Libraries..." Search for "SD" and install the library.Select the Board Manager: Choose "Tools" > "Board" > "Raspberry Pi Pico — Arduino Mbed OS RP2040 Boards."Create a New Sketch: Select "File" > "New" and name it "PicoSDCardDataLogger."
#include <SD.h>
const int chipSelect = 17; // GP pin ID on Pico for chip select
// Sample data logging values
const String dataValues[] = {
"<D: 12.5, 23.1, 34.2>",
"<D: 13.6, 22.5, 31.9>",
"<D: 11.4, 25.6, 30.0>",
"<D: 14.1, 24.3, 29.8>",
"<D: 10.9, 22.0, 32.5>"
};
int randomIndex; // A random index to retrieve data from the array
File dataLog; // Declare the log file to be used in the loop
void setup() {
Serial.begin(115200); // Initiate the serial monitor
while(!Serial); // Wait for the serial monitor to initialize
while (!SD.begin(chipSelect)) {
Serial.println("Failed to initialize SD card.");
}
Serial.println("SD card initialized successfully.");
}
void loop() {
// Open a file in the SD card
// Only one file can be open at a time
// Specifying only the name or having a single forward
// slash means the file will be located in the root directory
File dataLog = SD.open("dataLog.txt", FILE_WRITE);
// If the file is OK, write to it
if (dataLog) {
randomIndex = random(0, 5); // A random index between 0 and 4
dataLog.println(dataValues[randomIndex]); // Write a random data line to the file
dataLog.close(); // Close the file after we're done with it
Serial.println(dataValues[randomIndex]); // Display the same line in the serial monitor to compare the integrity
} else {
Serial.println("Failed to open dataLog.txt."); // Display error message in the serial monitor
}
delay(1000); // Wait for a second between loggings
}
Library Inclusion: The #include <SD.h> line brings in the SD library, providing the necessary functions for SD card interaction.Chip Select: const int chipSelect = 17; defines the GP pin connected to the CS line on the SD card module.Data Values: The dataValues array simulates sensor data, holding strings representing different data points.Setup Function: The setup() function initializes the serial monitor and attempts to initialize the SD card using SD.begin(chipSelect).Loop Function: The loop() function opens the "dataLog.txt" file for writing, generates a random data line, writes it to the file, and displays it on the serial monitor.
Install the SD Library: Ensure you have the SD library installed as described in the previous section.Select the Board Manager: Choose "Tools" > "Board" > "Raspberry Pi Pico — Arduino Mbed OS RP2040 Boards."Create a New Sketch: Select "File" > "New" and name it "PicoSDCardFileReader."
#include <SD.h>
const int chipSelect = 17; // GP pin ID on Pico for chip select
File dataLog; // Declare the file we're gonna read from
void setup() {
Serial.begin(115200); // Initiate the serial monitor
while(!Serial); // Wait for the serial monitor to initialize
while (!SD.begin(chipSelect)) {
Serial.println("Failed to initialize SD card.");
}
Serial.println("SD card initialized successfully.");
dataLog = SD.open("dataLog.txt"); // Open the file named dataLog.txt
if (dataLog) { // If dataLog is open
Serial.println("Reading from dataLog.txt");
while (dataLog.available()) { // While there are bytes that can be read
Serial.print((char) dataLog.read()); // Read bytes from the dataLog, cast bytes to char, print to serial monitor
}
dataLog.close(); // Close the file after we're done with it.
} else {
Serial.println("Failed to open dataLog.txt");
}
}
void loop() {} // No code in the loop function as we only read once
Library Inclusion: The #include <SD.h> line brings in the SD library, providing the necessary functions for SD card interaction.Chip Select: const int chipSelect = 17; defines the GP pin connected to the CS line on the SD card module.File Declaration: File dataLog; declares a variable to hold the file object.Setup Function: The setup() function initializes the serial monitor, initializes the SD card using SD.begin(chipSelect), opens the "dataLog.txt" file, and reads the contents, displaying them on the serial monitor.Loop Function: The loop() function is empty since we are only reading the data once.
Monitor the Serial Monitor: Observe the serial monitor during the "PicoSDCardDataLogger" sketch execution. It should display the randomly generated data values.Check the SD Card: After running the "PicoSDCardDataLogger" sketch, connect the SD card to your computer. You should find the "dataLog.txt" file containing the logged data.Run the "PicoSDCardFileReader" sketch: The serial monitor should display the data read from the "dataLog.txt" file, confirming that the reading operation is successful.
0 comments:
Post a Comment