Hi guys, I am currently doing a project to create a cooling fan for a room. For the temperature and time reading I am using DS18B20 temperature sensor and a DS1302 RTC. I am using a SPI ICSP micro SD card adapter module to record the temperature reading and time.
Currently I am stuck at the code, I manage to create the code that will show the temperature and time on serial monitor but I am having trouble to code the Arduino to save the data in the SDCard.
Can someone help to solve the problem? Bellow is my sketch code
//Start of RTC
#include <virtuabotixRTC.h>
// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(6, 7, 8);
//End of RTC
//Stat of temperature
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;
//end of temperature
void setup() {
Serial.begin(9600);
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
myRTC.setDS1302Time(0, 7, 12, 3, 31, 5, 2023);
//Start of temperature setup
sensors.begin(); // Start up the library
Serial.begin(9600);
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
}
//End of temperature setup
void loop() {
//Start of RTC loop
// This allows for the update of variables for time or accessing the individual elements.
myRTC.updateTime();
// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
// Delay so the program doesn't print non-stop
delay(500);
//End of RTC loop
//start of temperature loop
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
Serial.print((char)176);//shows degrees character
Serial.println("F");
//end of temperature loop
}
Serial.println("");
delay(1000);
}