Full width home advertisement

Arduino project

Raspberry project

Post Page Advertisement [Top]


This tutorial shows how to make data logger by Arduino & SD card module
It will read data from real time module DS3231 (real time, temperature), then save data into text file which is latter imported into Excel file
Important thing of data logger is to know real time of recorded data, so real time DS3231 is used to read real time (link here to know how to use DS3231)
In this example, Arduino will read temperature (also from DS3231) and record in text file. Microsoft Excel will help us to import data from text file, from here we can draw a graph.

Hardware need to purchase
1. Arduino Pro Mini
2. Real time moduel DS3231
3. SD card module SPI
4. Micro SD card 8GB (with adapter)
1. Arduino Pro Mini
2. Real time module DS3231
3. SD card module SPI
4. Micro SD card 8GB




Step 1. Arduino SD card schematic




Step 2. Arduino SD card data logger code
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "DS3231.h"

RTClib RTC;
DS3231 Clock;

int Year;
int Month;
int Date;
int Hour;
int Minute;
int Second;
int tempC;
int interval;
int Minute_last;

File myFile;

void setup() {
  Serial.begin(9600);
  Wire.begin(); //for DS3231
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(2000);  //afer reset Arduino, 2s to take out SD

  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
  }
  else
  {
    Serial.println("initialization done.");
  }
}

void loop() {
  interval = 1; //interval to write data
  DateTime now = RTC.now();
  Year = now.year();
  Month = now.month();
  Date = now.day();
  Hour = now.hour();
  Minute = now.minute();
  Second = now.second();
  tempC = Clock.getTemperature();
  
  if ((Minute % interval == 0)&(Minute_last!=Minute))
  {
    write_data(tempC);  //write data
    Minute_last = Minute;
  }
}

void write_data(int temperature_input)
{
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.print(Year);
    myFile.print("/");
    myFile.print(Month);
    myFile.print("/");
    myFile.print(Date);
    myFile.print(" ");
    myFile.print(Hour);
    myFile.print(":");
    myFile.print(Minute);
    myFile.print("_temperature_");
    myFile.println(temperature_input);
    myFile.close();
    Serial.println("Write file successful!"); //print out COM Port
  } else {
    Serial.println("error opening test.txt");
  }
}
What the code do is: Initialize the card, if any failure from here, it will show "initialization failed!". I got experience with it when the card 16GB can't be recognized by Arduino, but it works when replace new card 8GB.

Then the code read temperature from DS3231 and write data to file test.txt. It will inform out to COM screen the result of data writing activity.
In this code, Arduino will write data every 1 minutes (it is controlled by variable "interval", if change it into 5, then it will record data every 5 minutes)

The text file test.txt then will import to Excel by import data function

No comments:

Post a Comment

Bottom Ad [Post Page]