风筝
发表于: 2019-8-18 09:36:26 | 显示全部楼层

存储数据是每个项目最重要的部分之一。根据数据类型和大小,有几种方法可以存储数据。 SD和micro SD卡是存储设备中最实用的一种,用于移动电话、小型机等设备。在本篇文章中,您将学习如何在Arduino开发板中使用SD和micro SD卡。最后,我们制作一个简单的项目,每小时测量一次环境温度并将其存储在SD卡上。

micro-sd-module-arduino-teaser.jpg


什么是SD和Micro SD卡模块?

SD和micro SD卡模块允许您与存储卡通信并写入或读取存储卡上的信息。该模块通过SPI协议连接。

uSD-Arduino-SD.jpg


要在Arduino中使用这些模块,您需要SD库。默认情况下,此库安装在Arduino应用程序中。


所需的材料

●     Arduino UNO R3开发板

●     Micro SD TF卡适配器模块

●     DS3231 I2C RTC模块

●     公对母跳线

●     micro SD卡

●     Arduino IDE

uSD-Arduino-required-materials.jpg


如何在Arduino中使用SD和Micro SD卡?

本文使用的模块是micro SD卡模块,您也可以将代码用于SD卡模块。


使用此模块非常简单,其电路连接如下:

ds3231-sd-module-pinout.jpg

ds3231-sd-card-module-circuit.jpg

代码:使用Arduino开发板在SD卡上写入数据:

  1. #include <SPI.h>
  2. #include <SD.h>
  3. File myFile;
  4. void setup() {
  5. // Open serial communications and wait for port to open:
  6. Serial.begin(9600);
  7. while (!Serial) {
  8. ; // wait for serial port to connect. Needed for native USB port only
  9. }
  10. Serial.print("Initializing SD card...");
  11. if (!SD.begin(10)) {
  12. Serial.println("initialization failed!");
  13. while (1);
  14. }
  15. Serial.println("initialization done.");
  16. // open the file. note that only one file can be open at a time,
  17. // so you have to close this one before opening another.
  18. myFile = SD.open("test.txt", FILE_WRITE);
  19. // if the file opened okay, write to it:
  20. if (myFile) {
  21. Serial.print("Writing to test.txt...");
  22. myFile.println("This is a test file :)");
  23. myFile.println("testing 1, 2, 3.");
  24. for (int i = 0; i < 20; i++) {
  25. myFile.println(i);
  26. }
  27. // close the file:
  28. myFile.close();
  29. Serial.println("done.");
  30. } else {
  31. // if the file didn't open, print an error:
  32. Serial.println("error opening test.txt");
  33. }
  34. }
  35. void loop() {
  36. // nothing happens after setup
  37. }
复制代码

上面代码执行的结果:

write-1500x549.png

使用Arduino开发板从SD卡读取数据

  1. #include <SPI.h>
  2. #include <SD.h>
  3. File myFile;
  4. void setup() {
  5. // Open serial communications and wait for port to open:
  6. Serial.begin(9600);
  7. while (!Serial) {
  8. ; // wait for serial port to connect. Needed for native USB port only
  9. }
  10. Serial.print("Initializing SD card...");
  11. if (!SD.begin(10)) {
  12. Serial.println("initialization failed!");
  13. while (1);
  14. }
  15. Serial.println("initialization done.");
  16. // open the file for reading:
  17. myFile = SD.open("test.txt");
  18. if (myFile) {
  19. Serial.println("test.txt:");
  20. // read from the file until there's nothing else in it:
  21. while (myFile.available()) {
  22. Serial.write(myFile.read());
  23. }
  24. // close the file:
  25. myFile.close();
  26. } else {
  27. // if the file didn't open, print an error:
  28. Serial.println("error opening test.txt");
  29. }
  30. }
  31. void loop() {
  32. // nothing happens after setup
  33. }
复制代码

上面代码执行的结果:

read-1500x796.png


使用DS3231模块在microSD上保存温度数据

除了IC时钟和日历外,DS3231模块还具有温度传感器。


电路连接

SD_ds3231.png

代码

要使用DS3231模块,必须先将库(Sodaq_DS3231.h)添加到Arduino应用程序中。

ds3231-lib.png

  1. /*
  2. Save temperature in SD/microSD card every hour with DS3231 + SD/microSD module + Arduino
  3. modified on 15 Apr 2019
  4. by Mohammadreza Akbari @ Electropeak
  5. https://electropeak.com/learn/
  6. */
  7. #include <SPI.h>
  8. #include <SD.h>
  9. #include <Wire.h>
  10. #include "Sodaq_DS3231.h"
  11. File myFile;
  12. DateTime now;
  13. int newHour = 0;
  14. int oldHour = 0;
  15. void save_temperature() {
  16. myFile = SD.open("temp.txt", FILE_WRITE);
  17. now = rtc.now();
  18. myFile.print(now.hour());
  19. myFile.print(":");
  20. myFile.print(now.minute());
  21. rtc.convertTemperature(); //convert current temperature into registers
  22. myFile.print(",");
  23. myFile.println(rtc.getTemperature()); //read registers and save temperature on deg C
  24. myFile.close();
  25. }
  26. void setup ()
  27. {
  28. Wire.begin();
  29. rtc.begin();
  30. Serial.begin(9600);
  31. Serial.print("Initializing SD card...");
  32. if (!SD.begin(10)) {
  33. Serial.println("initialization failed!");
  34. while (1);
  35. }
  36. Serial.println("initialization done.");
  37. now = rtc.now();
  38. oldHour = now.hour();
  39. }
  40. void loop ()
  41. {
  42. now = rtc.now();
  43. newHour = now.hour();
  44. if (oldHour != newHour) {
  45. save_temperature();
  46. oldHour = newHour;
  47. }
  48. }
复制代码

在存储一天的不同时间温度后,您可以将此信息绘制到Excel中。


以上就是本篇文章的全部内容。如有问题,请随时在本帖下面进行回复。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1483



手机版|

GMT+8, 2024-5-10 10:18 , Processed in 0.036475 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表