|
存储数据是每个项目最重要的部分之一。根据数据类型和大小,有几种方法可以存储数据。 SD和micro SD卡是存储设备中最实用的一种,用于移动电话、小型机等设备。在本篇文章中,您将学习如何在Arduino开发板中使用SD和micro SD卡。最后,我们制作一个简单的项目,每小时测量一次环境温度并将其存储在SD卡上。
什么是SD和Micro SD卡模块? SD和micro SD卡模块允许您与存储卡通信并写入或读取存储卡上的信息。该模块通过SPI协议连接。
要在Arduino中使用这些模块,您需要SD库。默认情况下,此库安装在Arduino应用程序中。
所需的材料 ● Arduino UNO R3开发板 ● Micro SD TF卡适配器模块 ● DS3231 I2C RTC模块 ● 公对母跳线 ● micro SD卡 ● Arduino IDE
如何在Arduino中使用SD和Micro SD卡? 本文使用的模块是micro SD卡模块,您也可以将代码用于SD卡模块。
使用此模块非常简单,其电路连接如下:
代码:使用Arduino开发板在SD卡上写入数据: - #include <SPI.h>
- #include <SD.h>
- File myFile;
- 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("Initializing SD card...");
- if (!SD.begin(10)) {
- Serial.println("initialization failed!");
- while (1);
- }
- 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("test.txt", FILE_WRITE);
- // if the file opened okay, write to it:
- if (myFile) {
- Serial.print("Writing to test.txt...");
- myFile.println("This is a test file :)");
- myFile.println("testing 1, 2, 3.");
- for (int i = 0; i < 20; i++) {
- myFile.println(i);
- }
- // close the file:
- myFile.close();
- Serial.println("done.");
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening test.txt");
- }
- }
- void loop() {
- // nothing happens after setup
- }
复制代码上面代码执行的结果:
使用Arduino开发板从SD卡读取数据 - #include <SPI.h>
- #include <SD.h>
- File myFile;
- 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("Initializing SD card...");
- if (!SD.begin(10)) {
- Serial.println("initialization failed!");
- while (1);
- }
- Serial.println("initialization done.");
- // open the file for reading:
- myFile = SD.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
- }
复制代码上面代码执行的结果:
使用DS3231模块在microSD上保存温度数据 除了IC时钟和日历外,DS3231模块还具有温度传感器。
电路连接
代码 要使用DS3231模块,必须先将库(Sodaq_DS3231.h)添加到Arduino应用程序中。
- /*
- Save temperature in SD/microSD card every hour with DS3231 + SD/microSD module + Arduino
- modified on 15 Apr 2019
- by Mohammadreza Akbari @ Electropeak
- https://electropeak.com/learn/
- */
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include "Sodaq_DS3231.h"
- File myFile;
- DateTime now;
- int newHour = 0;
- int oldHour = 0;
- void save_temperature() {
- myFile = SD.open("temp.txt", FILE_WRITE);
- now = rtc.now();
- myFile.print(now.hour());
- myFile.print(":");
- myFile.print(now.minute());
- rtc.convertTemperature(); //convert current temperature into registers
- myFile.print(",");
- myFile.println(rtc.getTemperature()); //read registers and save temperature on deg C
- myFile.close();
- }
- void setup ()
- {
- Wire.begin();
- rtc.begin();
- Serial.begin(9600);
- Serial.print("Initializing SD card...");
- if (!SD.begin(10)) {
- Serial.println("initialization failed!");
- while (1);
- }
- Serial.println("initialization done.");
- now = rtc.now();
- oldHour = now.hour();
- }
- void loop ()
- {
- now = rtc.now();
- newHour = now.hour();
- if (oldHour != newHour) {
- save_temperature();
- oldHour = newHour;
- }
- }
复制代码在存储一天的不同时间温度后,您可以将此信息绘制到Excel中。
以上就是本篇文章的全部内容。如有问题,请随时在本帖下面进行回复。 |