风筝
发表于: 2017-7-7 13:29:32 | 显示全部楼层

Arduino-temperature-time-data-logger-to-SD-card-using-dht11.jpg


作为工程师/开发人员,我们总是会依靠收集的数据来设计或改进系统。记录数据并进行分析是大多数行业的常见做法,在这里我们将制作一个Arduino数据记录仪项目,通过该项目我们将学习如何在一段特定时间间隔内记录数据。我们将使用一个Arduino开发板读取一些数据(温度、湿度、日期和时间),并且将它们保存在SD卡和计算机上。


保存的数据可以在Excel表格中轻松打开,并进一步分析。为了保留日期和时间,我们将使用通用的RTC模块DS3231,并且使用DHT11传感器获得温度和湿度。在项目的末尾时,您将学习到如何将日期、时间和传感器值等数据记录到SD卡中以及如何通过串行通讯将数据直接写入到Excel表格中。


需要的设备

●    面包板

●    Arduino UNO开发板

●    DHT11温度传感器

●    DS3231 RTC模块

●    SD卡模块和SD卡

●    连接导线

●    台式计算机或笔记本

       DHT11-Sensor.jpg


电路原理图

Arduino温度数据记录仪项目的电路图如下所示。

Arduino-temperature-data-logger-to-SD-card-and-computer-circuit.png

如电路图所示,连接非常简单,由于我们使用的是模块,因此可以直接在面包板上进行制作。详细的连接方式如下表所示:

温度传感器 - DHT11

Arduino开发板引脚
模块引脚
VCC
5V
GND
GND
NC
NC
OUT
7脚


RTC模块DS3231

Arduino开发板引脚
模块引脚
VCC
5V
GND
GND
SCL
A5引脚
SDA
A4引脚

SD卡模块

Arduino开发板引脚
模块引脚
VCC
5V
GND
GND
MSIO
12引脚
MOSI
引脚11
SCK
引脚13
CS
引脚4

您可以使用任何传感器来代替DHT11温度传感器。可以参考使用LM35和Arduino读取温度。

RTC模块DS3231使用I2C通信(SCL、SDA)与Arduino连接,SD卡模块使用SPI通信(MISO、MOSI、SCK、CS)进行接口连接。引脚4和引脚7由Arduino程序定义为CS引脚和输出引脚,如果需要,可将其更改为任何其他引脚。。


Arduino程序说明:

我们需要写出以下的Arduino程序:

1.    从DTH11传感器读取数据。

2.    初始化I2C总线以从RTC模块读取数据。

3.    初始化SPI总线,将SD卡模块连接到Arduino接口。

4.    将日期、时间、温度和湿度等数据存入SD卡。

5.    将日期、时间、温度和湿度等数据存储在运行在计算机/笔记本电脑上的Excel工作表上。


上述步骤可能听起来很复杂,但是实现起来非常容易,因为我们可以使用库来帮我们做这些事情。你需要下载以下两个库:

1.   GitHub的DHT11传感器库

2.   Rinky-Dink Electronics的DS3231 RTC模块库


一旦你下载了这个库,可以通过以下方式将它们添加到你的Arduino IDE中

  1. Sketch->Include Library -> Add .ZIP Library
复制代码

要将Arduino的数据从电脑中提取到Excel表格中,我们还需要安装由Parallax Inc.提供的PLX-DAQ软件。请按照链接下载文件并根据操作系统进行安装。这应该在桌面上创建一个名为PLS-DAQ的文件夹。稍后在我们将会介绍具体的操作。

现在加入这两个库,安装完软件后,可以使用完整代码,并将其上传到您的Arduino开发板。


1.    从DS3231读取数据:

DS3231是一个RTC(实时时钟)模块。它用于保持日期和时间。该模块具有自己的电池电源,即使在主电源被去除或MCU已经通过硬件复位时,它也能保持日期和时间。所以一旦我们在这个模块中设置日期和时间,它将始终保持。

使用这个模块是非常容易的,因为其由Arduino提供的库。

  1. // Init the DS3231 using the hardware interface
  2. DS3231  rtc(SDA, SCL);
  3. void Initialize_RTC()
  4. {
  5.    // Initialize the rtc object
  6.   rtc.begin();

  7. //#### the following lines can be uncommented to set the date and time for the first time###
  8. /*
  9. rtc.setDOW(FRIDAY);     // Set Day-of-Week to SUNDAY
  10. rtc.setTime(18, 46, 45);     // Set the time to 12:00:00 (24hr format)
  11. rtc.setDate(6, 30, 2017);   // Set the date to January 1st, 2014
  12. */
  13. }
复制代码

注意:首次使用本模块时,必须设置日期和时间。可以通过简单地删除上述的注释并编写日期和时间来完成。确保您将其注释并上传,否则每次运行电路板时,将再次设置日期和时间。


2.    从DHT11读取数据:

DHT11是温湿度传感器。它通过模块的输出引脚将温度和湿度值作为8位数据串行发送。该库通过使用Arduino的软件串行功能读取此数据。

  1. #define DHT11_PIN 7 //Sensor output pin is connected to pin 7
  2. dht DHT; //Sensor object named as DHT
  3. void Read_DHT11()
  4. {
  5. int chk = DHT.read11(DHT11_PIN);
  6. }
复制代码

这里我将输出引脚连接到引脚7,也可以选择任何支持软件串行的引脚。调用DHT.read(引脚号);将读取温度和湿度的值,并将其存储在参数DHT.temperature和DHT.Humidity中。


3.   初始化SC卡模块:

  1. void Initialize_SDcard()
  2. {
  3.   // see if the card is present and can be initialized:
  4.   if (!SD.begin(chipSelect)) {
  5.     Serial.println("Card failed, or not present");
  6.     // don't do anything more:
  7.     return;
  8.   }

  9.    // open the file. note that only one file can be open at a time,
  10.   // so you have to close this one before opening another.
  11.   File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  12.   // if the file is available, write to it:
  13.   if (dataFile) {
  14.     dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
  15.     dataFile.close();
  16.   }
  17. }
复制代码

Arduino使用SD卡很容易,因为Arduino IDE默认添加了SD卡库。在SD卡初始化功能中,我们将创建一个名为“LoggerCD.txt”的文本文件,并写入我们内容的第一行。这里我们使用“,”作为分隔符分隔值。这意味着我们必须移动到Excel表格中的下一个单元格。


4.   将数据写入SD卡

  1. void Write_SDcard()
  2. {
  3.     // open the file. note that only one file can be open at a time,
  4.   // so you have to close this one before opening another.
  5.   File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);

  6.   // if the file is available, write to it:
  7.   if (dataFile) {
  8.     dataFile.print(rtc.getDateStr()); //Store date on SD card
  9.     dataFile.print(","); //Move to next column using a ","

  10.     dataFile.print(rtc.getTimeStr()); //Store date on SD card
  11.     dataFile.print(","); //Move to next column using a ","

  12.     dataFile.print(DHT.temperature); //Store date on SD card
  13.     dataFile.print(","); //Move to next column using a ","

  14.     dataFile.print(DHT.humidity); //Store date on SD card
  15.     dataFile.print(","); //Move to next column using a ","

  16.     dataFile.println(); //End of Row move to next row
  17.     dataFile.close(); //Close the file
  18.   }
  19.   else
  20.   Serial.println("OOPS!! SD card writing failed");
  21. }
复制代码

如前所述,我们的目的是将日期、时间、温度和湿度保存在我们的SD卡中。在DS3231库和DHT11库的帮助下,我们的Arduino将能够读取所有这四个参数并将其存储到以下参数中,如下表所示:

日期 rtc.getDateStr();
时间 rtc.getTimeStr();
温度 DHT.temperature
湿度 DHT.humidity

现在我们可以直接使用这些参数,使用打印行将它们存储在SD卡上

  1. dataFile.print(parameter);
复制代码

您可以注意到,每个参数用逗号分隔,使其看起来清晰可见,并且dataFile.println();用于指示行的结尾。


5.    将数据写入PLX-DAQ

PLX-DAQ软件是Microsoft Excel插件,可帮助我们将Arduino的值直接写入笔记本电脑或PC上的Excel文件。我比较喜欢这个软件,主要是因为两个原因:

1.   您可以同时写入和监控数据,并可以根据这些数据绘制图形。

2.   您不需要像DS3231这样的RTC模块来跟踪日期和时间。您只需使用笔记本电脑/计算机上运行的日期和时间,并将其直接保存在Excel中。


要想将此软件与Arduino一起使用,我们必须以特定的模式串行发送数据,就像在串行监视器上显示值一样。主要内容如下:

  1. void Initialize_PlxDaq()
  2. {
  3. Serial.println("CLEARDATA"); //clears up any data left from previous projects
  4. Serial.println("LABEL,Date,Time,Temperature,Humidity"); //always write LABEL, to indicate it as first line
  5. }
  6. void Write_PlxDaq()
  7.   {
  8.     Serial.print("DATA"); //always write "DATA" to Inidicate the following as Data
  9.     Serial.print(","); //Move to next column using a ","

  10.     Serial.print("DATE"); //Store date on Excel
  11.     Serial.print(","); //Move to next column using a ","

  12.     Serial.print("TIME"); //Store date on Excel
  13.     Serial.print(","); //Move to next column using a ","

  14.     Serial.print(DHT.temperature); //Store date on Excel
  15.     Serial.print(","); //Move to next column using a ","

  16.     Serial.print(DHT.humidity); //Store date on Excel
  17.     Serial.print(","); //Move to next column using a ","

  18.     Serial.println(); //End of Row move to next row

  19.   }
复制代码

该软件可以识别LABEL、DATA、TIME、DATE等关键字。如初始化函数中所示,关键字“LABEL”用于写入Excel表格的第一个ROW。在写函数后面,我们使用关键字“DATA”来表示以下信息应被视为DATA。为了表明我们必须移动到下一行,我们必须使用逗号(“,”)。为了指示行的结尾,我们必须发送一个Serial.println();


如前所述,我们可以通过分别发送关键字“DATE”和“TIME”来写入系统日期和时间,如上所示。

注意:使用此PLX_DAQ软件时,请勿使用串口监视器。

跳转到指定楼层
风筝
发表于: 2017-7-7 13:54:39 | 显示全部楼层

工作原理说明

Arduino数据记录仪的工作过程很简单。一旦硬件和软件准备就绪,就可以将程序烧写到Arduino开发板中。当您的程序上传完成后,温度和湿度值将开始存储在SD卡中。您必须按照以下步骤启用PLX-DAQ记录到计算机中的Excel工作表。


步骤1:打开安装过程中在桌面上创建的“Plx-Daq Spreadsheet”文件。


步骤2:如果有安全模块,请单击Options->Enable the content -> Finish -> OK,显示以下界面。

logging-data-to-excel-sheet-using-Arduino-and-PLX-DAQ.png


步骤3:现在选择波特率为“9600”和Arduino开发板连接到计算机映射的端口,然后单击Connect。您的值应该会开始记录,如下图所示。

logging-data-to-excel-sheet-using-Arduino-and-PLX-DAQ-2.png


您可以打开excel表并监视它们记录的值。我们的SD卡也将保存相同的值。要检查其是否正常工作,只需去掉SD卡并将其在您的计算机上打开。您应该在其中找到一个名为“LoggerCD.txt”的文本文件。打开文件如所示:

logging-data-to-SD-card-notepad-file-using-Arduino.png

这个文件有数据,但是很难在记事本上分析它们。因此,我们可以在Excel中以CSV(逗号分隔值)文件的形式打开它,从而使其更有效。


1.  打开Excel。单击File->Open,选择右下角的“All file”,从SD卡中选择“LoggerCD”文件。这将打开一个文本导入向导。

2.   点击“Next”,选择逗号作为分隔符。再次点击“Next”。然后完成。

3.   现在,您的值将在Excel文件中打开,如下所示

logging-data-to-SD-card-using-Arduino.png


我已经记录了每5秒钟的值。您可以通过更改程序中的延迟函数设置记录的时间间隔。

Arduino-temperature-data-logger-to-SD-card-and-computer.jpg



代码

  1. /*
  2. * Program to demonstrate Data Logging/Visualisation using Arduino
  3. *
  4. * ###Connection with SD card module###
  5. * Vcc->5V
  6. * Gnd->Gnd
  7. * MISO->pin 12
  8. * MOSI->pin 11
  9. * SCK-> pin 13
  10. * CS-> pin 4
  11. *
  12. * ###Connection with DS3231###
  13. * Vcc->5V
  14. * Gns->Gnd
  15. * SCL->pin A5
  16. * SDA-> pin A4
  17. *
  18. * ###Connection with DT11###
  19. * Vcc->5V
  20. * Gnd->Gnd
  21. * Out-> pin 7
  22. *
  23. *
  24. */
  25. #include <DS3231.h> //Library for RTC module (Download from Link in article)
  26. #include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
  27. #include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
  28. #include <dht.h> //Library for dht11 Temperature and Humidity sensor (Download from Link in article)
  29. #define DHT11_PIN 7 //Sensor output pin is connected to pin 7
  30. dht DHT; //Sensor object named as DHT
  31. const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino
  32. // Init the DS3231 using the hardware interface
  33. DS3231  rtc(SDA, SCL);
  34. void setup()
  35. {
  36.   // Setup Serial connection
  37.   Serial.begin(9600);
  38.   Initialize_SDcard();
  39.   Initialize_RTC();
  40.   Initialize_PlxDaq();
  41. }
  42. void loop()
  43. {
  44.   Read_DHT11();
  45.   Write_SDcard();
  46.   Write_PlxDaq();
  47.   delay(5000); //Wait for 5 seconds before writing the next data
  48. }
  49. void Write_PlxDaq()
  50.   {
  51.     Serial.print("DATA"); //always write "DATA" to Indicate the following as Data
  52.     Serial.print(","); //Move to next column using a ","
  53.     Serial.print("DATE"); //Store date on Excel
  54.     Serial.print(","); //Move to next column using a ","
  55.     Serial.print("TIME"); //Store date on Excel
  56.     Serial.print(","); //Move to next column using a ","
  57.     Serial.print(DHT.temperature); //Store date on Excel
  58.     Serial.print(","); //Move to next column using a ","
  59.     Serial.print(DHT.humidity); //Store date on Excel
  60.     Serial.print(","); //Move to next column using a ","
  61.     Serial.println(); //End of Row move to next row
  62.   }
  63. void Initialize_PlxDaq()
  64. {
  65. Serial.println("CLEARDATA"); //clears up any data left from previous projects
  66. Serial.println("LABEL,Date,Time,Temperature,Humidity"); //always write LABEL, to indicate it as first line
  67. }
  68. void Write_SDcard()
  69. {
  70.     // open the file. note that only one file can be open at a time,
  71.   // so you have to close this one before opening another.
  72.   File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  73.   // if the file is available, write to it:
  74.   if (dataFile) {
  75.     dataFile.print(rtc.getDateStr()); //Store date on SD card
  76.     dataFile.print(","); //Move to next column using a ","
  77.     dataFile.print(rtc.getTimeStr()); //Store date on SD card
  78.     dataFile.print(","); //Move to next column using a ","
  79.     dataFile.print(DHT.temperature); //Store date on SD card
  80.     dataFile.print(","); //Move to next column using a ","
  81.     dataFile.print(DHT.humidity); //Store date on SD card
  82.     dataFile.print(","); //Move to next column using a ","
  83.     dataFile.println(); //End of Row move to next row
  84.     dataFile.close(); //Close the file
  85.   }
  86.   else
  87.   Serial.println("OOPS!! SD card writing failed");
  88. }
  89. void Initialize_SDcard()
  90. {
  91.   // see if the card is present and can be initialized:
  92.   if (!SD.begin(chipSelect)) {
  93.     Serial.println("Card failed, or not present");
  94.     // don't do anything more:
  95.     return;
  96.   }
  97.    // open the file. note that only one file can be open at a time,
  98.   // so you have to close this one before opening another.
  99.   File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  100.   // if the file is available, write to it:
  101.   if (dataFile) {
  102.     dataFile.println("Date,Time,Temperature,Humidity"); //Write the first row of the excel file
  103.     dataFile.close();
  104.   }
  105. }
  106. void Initialize_RTC()
  107. {
  108.    // Initialize the rtc object
  109.   rtc.begin();
  110. //#### The following lines can be uncommented to set the date and time for the first time###  
  111. /*
  112. rtc.setDOW(FRIDAY);     // Set Day-of-Week to SUNDAY
  113. rtc.setTime(18, 46, 45);     // Set the time to 12:00:00 (24hr format)
  114. rtc.setDate(6, 30, 2017);   // Set the date to January 1st, 2014
  115. */
  116. }
  117. void Read_DHT11()
  118. {
  119. int chk = DHT.read11(DHT11_PIN);
  120. }
  121. /*void Read_DateTime()
  122. {  
  123.   // Send date
  124.   Serial.print(rtc.getDateStr());
  125.   Serial.print(" -- ");
  126.   // Send time
  127.   Serial.println(rtc.getTimeStr());
  128. }*/
  129. /*void Read_TempHum()
  130. {
  131.   Serial.print("Temperature = ");
  132.   Serial.println(DHT.temperature);
  133.   Serial.print("Humidity = ");
  134.   Serial.println(DHT.humidity);
  135. // delay(1000);
  136. }*/
复制代码

译者注:本文翻译自https://circuitdigest.com/microcontroller-projects/arduino-data-logger-project,感谢B.Aswinth Raj 做出的贡献。如有错漏,敬请指正。

回复

使用道具 举报

mrcls
发表于: 2019-1-27 16:58:48 | 显示全部楼层

按代码编译上传arduino后,在sd卡和excell中只有标题显示,但没有温度和湿度数据,请问是什么原因?
回复

使用道具 举报

ywdwhy
发表于: 2019-6-16 09:25:18 | 显示全部楼层

学习中,希望能多增加其它传感器的例子。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

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

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

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