项目笔记:Arduino读取SD卡
1 硬件连接(使用Arduino Uno):
CS -> 10
SCK -> 13
MOSI -> 11
MISO -> 12
VCC ->5V
GND -> GND
2 让Arduino检测到SD卡
官方测试程序:检测SD卡连接并输出卡型号
/*SD card testThis example shows how use the utility libraries on which the'SD library is based in order to get info about your SD card.Very useful for testing a card when you're not sure whether its working or not.The circuit:SD card attached to SPI bus as follows:** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila** CS - depends on your SD card shield or module.Pin 4 used here for consistency with other Arduino examplescreated 28 Mar 2011by Limor Friedmodified 9 Apr 2012by Tom Igoe
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;void setup() {// Open serial communications and wait for port to open:Serial.begin(9600);while (!Serial) {; // wait for serial port to connect. Needed for native USB port only}Serial.print("\nInitializing SD card...");// we'll use the initialization code from the utility libraries// since we're just testing if the card is working!if (!card.init(SPI_HALF_SPEED, chipSelect)) {Serial.println("initialization failed. Things to check:");Serial.println("* is a card inserted?");Serial.println("* is your wiring correct?");Serial.println("* did you change the chipSelect pin to match your shield or module?");while (1);} else {Serial.println("Wiring is correct and a card is present.");}// print the type of cardSerial.println();Serial.print("Card type: ");switch (card.type()) {case SD_CARD_TYPE_SD1:Serial.println("SD1");break;case SD_CARD_TYPE_SD2:Serial.println("SD2");break;case SD_CARD_TYPE_SDHC:Serial.println("SDHC");break;default:Serial.println("Unknown");}// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32if (!volume.init(card)) {Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");while (1);}Serial.print("Clusters: ");Serial.println(volume.clusterCount());Serial.print("Blocks x Cluster: ");Serial.println(volume.blocksPerCluster());Serial.print("Total Blocks: ");Serial.println(volume.blocksPerCluster() * volume.clusterCount());Serial.println();// print the type and size of the first FAT-type volumeuint32_t volumesize;Serial.print("Volume type is: FAT");Serial.println(volume.fatType(), DEC);volumesize = volume.blocksPerCluster(); // clusters are collections of blocksvolumesize *= volume.clusterCount(); // we'll have a lot of clustersvolumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)Serial.print("Volume size (Kb): ");Serial.println(volumesize);Serial.print("Volume size (Mb): ");volumesize /= 1024;Serial.println(volumesize);Serial.print("Volume size (Gb): ");Serial.println((float)volumesize / 1024.0);Serial.println("\nFiles found on the card (name, date and size in bytes): ");root.openRoot(volume);// list all files in the card with date and sizeroot.ls(LS_R | LS_DATE | LS_SIZE);
}void loop(void) {}
如果测试失败,要注意以下操作:
1 Initializing failed:
确认接线:对于Arduino Uno,SD卡读卡器的CS引脚要连接4.
如果接线正确,确认chipSelect值也为4
2 Could not find FAT16/FAT32 partition.\nMake sure you’ve formatted the card:
这一步需要对SD卡进行格式化,一定要选择FAT16或FAT32格式。对于内存大于32G的SD卡,windows自带的格式化工具不会提供FAT32选项,因此需要借用第三方工具:
http://ridgecrop.co.uk/index.htm?guiformat.htm
测试成功结果如下
2 利用SD卡读写文件
#include <SD.h>
#include <SPI.h>const int chipSelect = 10;
String fileName = "test.txt";
File myFile;void setup() {// Open serial communications and wait for port to open:Serial.begin(9600);pinMode(chipSelect, OUTPUT);// wait for Serial Monitor to connect. Needed for native USB port boards only:while (!Serial);Serial.print("Initializing SD card...");if (!SD.begin(chipSelect)) {Serial.println("initialization failed. Things to check:");Serial.println("1. is a card inserted?");Serial.println("2. is your wiring correct?");Serial.println("3. did you change the chipSelect pin to match your shield or module?");Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");while (true);}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 = SD.open(fileName, FILE_WRITE);// if the file opened okay, write to it:if (myFile) {Serial.print("Writing to test.txt...");myFile.println("testing 1, 2, 3.");if (SD.exists(fileName)) {Serial.println("file exists");}// close the file:myFile.close();Serial.println("done.");} else {// if the file didn't open, print an error:Serial.println("error opening test.txt");}Serial.println("reopening...");// re-open the file for reading:myFile = SD.open(fileName);if (myFile) {Serial.println(fileName + ":");// read from the file until there's nothing else in it:while (myFile.available()) {Serial.write(myFile.read());}// close the file:Serial.println("done.");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类方法:
1
SD.begin(chipSelect)
启动SD类,chipSelect为SD读卡器CS引脚连接的Arduino引脚。返回值为boolean,代表SD卡是否成功启动
2
SD.open(fileName, FILE_WRITE)
打开文件,参数为(文件名,读取方式)。注意文件名采用8.3格式,即文件名最多8字符,文件拓展名最多3字符,不区分大小写。如果文件名长度超过限制可能造成文件创建失败。
读取方式分为FILE_READ和FILE_WRITE,如果该参数不填默认为FILE_READ。在FILE_READ为只读状态,而FILE_WRITE允许修改文件。在FILE_WRITE状态下如果open的文件名不存在会创建新文件。
在FILE_READ模式下默认从文件头读取,在FILE_WRITE模式下默认从文件末尾读取
该方法返回值为一个文件(File类型),代表打开的文件。File类型支持被作为boolean,true代表存在文件,false代表不存在文件
3
SD.exists(fileName)
判断文件是否存在,返回boolean
文件操作方法:
myFile.println()
在文件里打印信息
myFile.close()
关闭并退出当前文件
上述测试程序运行结果:
注:我在测试中调用了程序多次,导致test.txt里面有多行内容。第一次运行后test.txt中只会有一行testing 1 2 3
项目笔记:Arduino读取SD卡相关推荐
- stm32 arduino 读取sd卡并在lcd(jlx12864)上播放badapple
目录 前言 引脚连接 代码示例 问题排查 1. sd卡无法读取 2. lcd无显示 前言 要问什么是测试单片机控制单色屏幕的最佳办法,我想badapple应该当之无愧,经典程度如helloworld. ...
- Arduino ESP32 第三方库读取SD卡信息(三)
Arduino ESP32 第三方库读取SD卡信息(三) 相关篇<Arduino ESP32 第三方库读取SD卡信息(一)> <Arduino ESP32 第三方库读取SD卡信息(二 ...
- Arduino ESP32 读取SD卡接口选择参考
ESP32 读取SD卡接口选择参考 ESP3232读取SD卡可以通过spi和sdmmc两种方式来读取,不过我们在市面上能买到的都基本上是4线的SPI接口的SD卡模块套件. 卡类型有如下,8-9Pin被 ...
- Arduino UNO 读取SD卡的信息
Arduino UNO 读取SD卡的信息 SD卡座模块 接口描述: 控制接口:共6个引脚(GND.VCC.MISO.MOSI.SCK.CS),GND为地,VCC为供电电源,MISO.MOSI.SCK为 ...
- 《Arduino》开发 TFT_eSPI-master 库 之用 ESP32 读取SD卡上的图片并显示在1.14IPS屏幕上
前几天解决了 TFT_eSPI-master 库 图片取模问题,但尽管是ESP32的 flash 也无法存储太多图片的数组,因此我找到了ESP32从SD卡读取图片并显示在LCD屏幕上的方法,SD卡可以 ...
- 【Arduino 连接 SD 卡模块实现数据读写】
Arduino 连接 SD 卡模块实现数据读写 前言 Micro SD 卡模块引脚分配 Micro SD卡模块电路图 Arduino Micro SD卡模块电路连接图 Arduino 代码说明 模块的 ...
- Arduino控制SD卡(一)
学习如何使用Arduino读写SD卡.SD卡读卡器是在淘宝上买的,下图右侧的SD卡是行车记录仪上面的.里面有文件夹和视频文件. Arduino开发平台中自带了SD卡的示例程序,如下图所示: 总共六个示 ...
- Arduino ESP32 SD卡读写实例
Arduino ESP32 SD卡读写实例 Arduino官方文档参考:https://www.arduino.cc/en/Reference/SD SD卡硬件连接方式 ESP32有两种使用SD卡的方 ...
- android 读取sd卡中的图片
一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限 --><uses-permission android:name="android.permissio ...
最新文章
- samba3.2的安装全过程(tar方式)
- spark 数据框 删除列_pandas 常用的数据处理函数
- OpenGL FBO
- 全网首发:linux任务栏分组的研究
- 网页自动关机代码HTML,电脑怎么设置自动关机时间
- android qq轻聊版,Android QQ轻聊版怎么样 Android QQ轻聊版介绍
- 打造薪酬体系,有效激励员工 穆穆-movno1
- java银行叫号课程设计_课程设计-银行排队叫号机设计.doc
- 图像采集——OV5640摄像头简介、硬件电路及上电控制的Verilog代码实现并进行modelsim仿真
- 记单词01-vict, vinc(胜利,征服)
- 爱回收提交订单一直显示服务器来小差,京东的爱回收太垃圾,千万不要用。
- 小甲鱼Python学习笔记之函数(四)
- css3制作广告栏效果的疑问? 1
- 计算机网络协议,以太网帧格式
- 2021支付宝扫福玩法攻略
- 利用python,求解数独
- KINOVA公司JACO2、MICO2从安装到开发
- matlab中zeros()函数
- 进制转换(非常详细+算法代码)
- 其他:Pycharm 常用快捷键
热门文章
- 是什么让一个优秀的派对服饰
- 技术经理成长复盘-发现团队的瓶颈
- Android中MVC,MVP和MVVM的区别
- 转:听听别人怎么说:VueJS 与 ReactJS
- 关闭compactos_开启Compact压缩Win10系统文件,为Win10系统盘瘦身
- 三元运算 微信小程序_微信小程序 三元运算符 条件语句if和循环语句for(三目运算+hidden)...
- latex中自动生成参考文献
- java lzw_lzw在java中解压缩
- 姜小白的python日记Day3 初识模块与数据运算
- 文字转语音软件哪个好?这些软件值得收藏