当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Project 043 - SD CardTemperature Datalogger

RonWang2个月前 (12-10)电子编程169

Todady I made a simple Arduino datalogger using SD card and DHT11  relative humidity and temperature sensor.  The Arduino datalogger that logs: date, time, temperature and humidity on an SD card.

In this project the DS3231 real time clock chip will be used to maintain the time as well as the date and the DHT11/DTH22 sensor to detect the relative humidity and the temperature. Also, in the circuit there will be two pushbuttons to set up the time and date and a 20×4 LCD screen to display all parameters (time, date, temperature and humidity) as shown below:

Project 43 Temperature SD Datalogger

Arduino SD Card Record Temperature Circuit

Arduino SD Card Record Temperature  Circuit and Schematic

Arduino SD Card Record Temperature Schematic

Arduino SD Card Record Temperature  Circuit and Schematic

/* Coding Ron Wang
   Dec.10th 2024
   Autaba provides Hardware support for the project
   © Schematic Design Fritzing By Ron Wang 2024 NY
   © Circuit Design Fritzing By Ron Wang 2024 NY
  Project 43 Temperature DataLogger SD Card 
 */
#include <SPI.h> //for the SD card module
#include <SD.h> // for the  SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for  the RTC

//define DHT pin
#define DHTPIN A0 
s    
#define DHTTYPE DHT11   //  DHT 11 
//#define DHTTYPE DHT22 // DHT 22  (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// initialize DHT sensor for normal 16mhz Arduino
DHT  dht(DHTPIN, DHTTYPE);

// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules:  pin 10

const int chipSelect = 4;

// Create  a file to store the data
File myFile;

// RTC
RTC_DS1307 rtc;

void  setup() {
  //initializing the DHT sensor
  dht.begin();

  //initializing  Serial monitor
  Serial.begin(9600);
 
  // setup for the RTC
  while(!Serial);  // for Leonardo/Micro/Zero
    if(! rtc.begin()) {
      Serial.println("Couldn't  find RTC");
      while (1);
    }
    else {
      // following line  sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__),  F(__TIME__)));
    }
    if(! rtc.isrunning()) {
      Serial.println("RTC  is NOT running!");
    }
   
  // setup for the SD card
  Serial.print("Initializing  SD card...");

  if(!SD.begin()) {
    Serial.println("initialization  failed!");
    return;
  }
  Serial.println("initialization done.");
   
  //open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  //  if the file opened ok, write to it:
  if (myFile) {
    Serial.println("File  opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature  ºC");
  }
  myFile.close();
}

void loggingTime() {
  DateTime  now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile)  {
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(),  DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(),  DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(),  DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(),  DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  delay(1000);  
}

void  loggingTemperature() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read  temperature as Fahrenheit
  //float f = dht.readTemperature(true);
 
  // Check if any reads failed and exit early (to try again).
  if  (isnan(t)  /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  //debugging purposes
  Serial.print("Temperature:  ");
  Serial.print(t);
  Serial.println(" *C");
  //Serial.print(f);
  //Serial.println(" *F\  "); 
 
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
    myFile.print(t);
    myFile.println(",");
  }
  myFile.close();
}

void loop() {
  loggingTime();
  loggingTemperature();
  delay(5000);
}


版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://www.donstudio.cn/?id=300

标签: Arduino

相关文章

C语言调试运行环境TurboC的安装

C语言调试运行环境TurboC的安装

上大学时学习《C语言程序设计》(第二版)作者谭浩强,大部分编程时间是在学校机房度过,每次点开桌面的TurboC图标,就开始在里面敲代码,然后保存程序Abc.C,下一步进行编译,如果编译成功的话,就可以...

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Arduino Project 023C - Liquid Crystal Displays - Serial to Display

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码今天我们将使用Arduino UNO 和SD卡制作音乐播放器。这个播放器不需要添加多余的模块,只需要SD读卡器和Arduino UNO开发板就可以播放音频文件...

Arduino Project 016 - L293D Motor Driver IC

Arduino Project 016 - L293D Motor Driver IC

In the previous project, you used a transistor to control the motor. In this project, you are going...

 Arduino Project 037 - 18B20 Temperature Sensor

Arduino Project 037 - 18B20 Temperature Sensor

The DS18B20 Temperature Sensor is a digital temperature sensor capable of measuring temperatures wit...

Arduino Programming Basic - If and Loop

Arduino Programming Basic - If and Loop

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写了10个例子,关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。