I have a project of making a weather station recording data of temp, humidity, light by time, and date in an SD. However, when I tried to implement the code it keeps failing on me.
Can anyone please on what I did wrong on that code?
I am using Arduino UNO + Arduino Ethernet Shield.
my micro SD is ScanDisk Ultra 64 GB XC I
This is my code
#include <dht.h>
#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
File file; // associate file with SD library
dht DHT;
DS3231 rtc(SDA, SCL); // associate rtc with DS3231 library
int CSpin = 10; // chip select pin for SD card
#define Dht A2 //Dht
#define TemPin A0 //tmp
int LDR= A1; //Ldr
int i = 0; // data record counter
long check, light, temp, humid;
String data, date, time;
String filename = "data.csv"; // filename
void setup() {
Serial.begin(9600); // define Serial output baud rate
pinMode(LDR, INPUT); // LDR INPUT
// Begin serial communication at a baud rate of 9600:
rtc.begin();
//rtc.setDOW(SUNDAY); // set weekday
rtc.setTime(10, 43, 40); // set the time to hh mm ss
rtc.setDate(01, 3, 2022); // set the date to dd mm yyyy
Serial.println("checking SD card"); // check for presence of SD card
if(SD.begin(CSpin) == 0)
{
Serial.println("Card fail"); // return to void setup() if SD card not found
return;
}
Serial.println("Card OK");
if(SD.exists(filename)>0) SD.remove(filename); // delete old file
file = SD.open(filename, FILE_WRITE); // create new file
if(file == 1)
{ // column headers
String header = "record, time, light, temp, humid, ON_Date >>";
header = header + String(rtc.getDateStr()); // date
file.println(header); // write column headers to file
file.close(); // close file after writing to SD card
}
else Serial.println("Couldn't access file"); // file not opened
}
void loop() {
i++; // increase data record counter
Serial.print("record ");Serial.println(i); // print record number
light= analogRead(LDR); // light intensity reading
check = DHT.read11(Dht);
int Tempreading = analogRead(TemPin);
// Convert the reading into voltage:
float voltage = Tempreading * (5000 / 1024.0); //temp
// Convert the voltage into the temperature in degree Celsius:
float temperature = voltage / 10; //temp
temp = temperature ;// temperature reading
humid = DHT.humidity; // humidity reading
time = rtc.getTimeStr(); // time stamp
// combine measurements into a string
data = String(i) + "," + String(time) + "," + String(light);
data = data + "," + String(temp) + "," + String(humid);
file = SD.open(filename, FILE_WRITE); // open data file before writing
file.println(data); // write data string to file
file.close(); // close file after writing to SD card
delay(1000); // delay 1s before next reading
}
Hi Paul, what's failing is the data doesn't get created on a text file in the SD. when I try to check the SD card on my phone it doesn't display any text files containing the results of my sensors readings.
I’ve had issues getting my SD card module to work
Presuming you are using an uno, here is what ended up working for me
I have no idea why it works or even if all the steps below are needed
Switch the CS to pin 4
Add in the void setupvoid setup the following 2 lines
This is a very common beginner mistake. Opening and closing the file just to write one data point VASTLY increases the SD card error rate, and amount of time required to write data. Open the file once in setup() and close it when you are all done collecting data. Issue a file.flush() command once an hour or once a day to keep the file pointers up to date.
file = SD.open(filename, FILE_WRITE); // open data file before writing
file.println(data); // write data string to file
file.close(); // close file after writing to SD card
Also, on AVR based Arduinos like the Uno, use of Strings causes memory fragmentation, and program failures. They are never necessary, so get rid of them.
Sorry man, I'm confused. can you clarify more?
My CSpin is pin 10 and I was saying if it wasn't responding then type "Card fail".
I read on UNO its pin 10 ,right ?
I am not sure if I can switch CS to pin 4. I am using an Arduino Ethernet shield on top of my UNO. I heard it is set to 10 by default. But I will give it try and will tell you if it worked.
The issue is that it doesn't even need to complete an hour. I am testing it out within a minute. However, it complies but it gives me this annoying message that is failing to create a file.
the code should make the sensors readings written in a file on the SD card. but when I run the code it compiles fine but it gives me a message after the SD checking that it is failing.
this is the only way I learned from youtube so far and it worked in proteus simulation.
however, when I went to try it out on the hardware it just didn't work.