UnoRP2040 SD卡
December 13, 2023About 2 min
SD卡示例
UNO RP2040有一个板载SD卡插槽,可以轻松从微型SD卡读取和写入文件。要使用Arduino执行此操作,您需要插入micro SD卡,安装SdFat库的Adafruit fork,并运行提供的示例代码。
步骤1.安装SDFat库
您可以使用Arduino IDE中的库管理器为Arduino安装SDFat库的Adafruit Fork。
步骤2.单击管理库...菜单项,搜索Adafruit SDFat,然后选择SDFat-Adafruit Fork库:
步骤 3.将以下代码复制并粘贴到 Arduino IDE 中
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI0 bus as follows:
** MOSI - pin 19
** MISO - pin 20
** CLK - pin 18
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 14 Feb 2023
by Liz Clark
This example code is in the public domain.
*/
#include "SdFat.h"
SdFat sd;
#define SD_FAT_TYPE 1
// default CS pin is 23 for Metro RP2040
#define SD_CS_PIN 23
File32 myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!sd.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// 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.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.println("hello metro rp2040!");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
现象
如果已插入SD卡,串口会打印initialization done.
如果未插入SD卡,串口会打印initialization failed!
疑难解答
更多问题及有趣的应用,请访问论坛 或加入QQ技术交流群:522420541