Arduino Modules – Log Temperature to SD Card Data Logging Module (HW-125)

This project shows you how to log Variable Values to an SD Card using a Data Logging Module.

Prerequisite Classes:

Functional Parts in the Project:

/*
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/

#include <SPI.h>
#include <SD.h>

#define sensorPin A5

File myFile;

void setup() {
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  while (!SD.begin(4)) {
    Serial.println("STARTUP SD Card Initialization FAILED!");
  }
  Serial.println("STARTUP initialization SUCCESS.");

}

void loop() {
  
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  long timestamp = millis();

  while (!SD.begin(4)) {
    Serial.println("SD CARD NO LONGER READABLE!");
  }

  myFile = SD.open("tempLog.txt", FILE_WRITE);

  if (myFile) {

    myFile.print(temperatureF);
    myFile.print(", ");
    myFile.println(timestamp);
    myFile.close();
  } else {
    Serial.println("error opening File");
  }

  Serial.print("Writing to SD: ");
  Serial.print(temperatureF);
  Serial.print(", ");
  Serial.print(timestamp);
  Serial.println("  done.");

  delay(5000);
}

Be the first to comment

Leave a Reply