Hi. I am hoping someone can help with this issue.
I have a project that uses DHT22, Photoresistor, RTC, and MKR GPS on an Arduino MKR Zero.
After I upload the sketch I unplug it from the computer and plug it to a 5v battery. The light is on and I let it run for a while.
When I look at the SD card, nothing has been written to it. However, when I leave it plugged to the computer, it works fine and it writes the data to the card.
I've tried resetting it several times when I unplug/plug it back but nothing seems to work.
Does anyone have any ideas as to why this happens?
I was thinking that it was the modules but not all can be faulty at the same time.
I appreciate any help. Thank you in advance.
This is my code.
//Libraries
#include <Wire.h>
#include <RTClib.h>
#include <DHT.h>
#include <Arduino_MKRGPS.h>
#include <SPI.h>
#include <SD.h>
//Constants
const int chipSelect = SDCARD_SS_PIN;
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
RTC_DS3231 rtc; // create an instance of the RTC_DS3231 class
//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
int lightVal;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Wire.begin();
rtc.begin();
// If you are using the MKR GPS as shield, change the next line to pass
// the GPS_MODE_SHIELD parameter to the GPS.begin(...)
if (!GPS.begin()) {
Serial.println("Failed to initialize GPS!");
while (1);
}
dht.begin();
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
// check if there is new GPS data available
if (GPS.available()) {
// read GPS values
float latitude = GPS.latitude();
float longitude = GPS.longitude();
float altitude = GPS.altitude();
float speed = GPS.speed();
int satellites = GPS.satellites();
// print all data on a single line separated by commas
Serial.print(rtc.now().timestamp());
Serial.print(",");
Serial.print(latitude, 7);
Serial.print(",");
Serial.print(longitude, 7);
Serial.print(",");
Serial.print(altitude);
Serial.print(",");
Serial.print(speed);
Serial.print(",");
Serial.print(satellites);
Serial.print(",");
lightVal = analogRead(A1);
Serial.print(lightVal);
Serial.print(",");
hum = dht.readHumidity();
Serial.print(hum);
Serial.print(",");
temp = dht.readTemperature();
Serial.print(temp);
DateTime now = rtc.now(); //get current date and time
Serial.print(","); //add comma to separate from previous data
Serial.print(now.year(), DEC); //print year
Serial.print("/"); //add slash between year and month
Serial.print(now.month(), DEC); //print month
Serial.print("/"); //add slash between month and day
Serial.print(now.day(), DEC); //print day
Serial.print(" "); //add space between date and time
Serial.print(now.hour(), DEC); //print hour
Serial.print(":"); //add colon between hour and minute
Serial.print(now.minute(), DEC); //print minute
Serial.print(":"); //add colon between minute and second
Serial.print(now.second(), DEC); //print second
Serial.println();
delay(900000); //Delay 15 min.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the fileis available, write to it:
if (dataFile) {
dataFile.print(latitude, 7);
dataFile.print(",");
dataFile.print(longitude, 7);
dataFile.print(",");
dataFile.print(altitude);
dataFile.print(",");
dataFile.print(speed);
dataFile.print(",");
dataFile.print(satellites);
dataFile.print(",");
dataFile.print(lightVal);
dataFile.print(",");
dataFile.print(hum);
dataFile.print(",");
dataFile.print(temp);
dataFile.print(",");
dataFile.print(now.year(), DEC);
dataFile.print("/");
dataFile.print(now.month(), DEC);
dataFile.print("/");
dataFile.print(now.day(), DEC);
dataFile.print(" ");
dataFile.print(",");
dataFile.print(now.hour(), DEC);
dataFile.print(":");
dataFile.print(now.minute(), DEC);
dataFile.print(":");
dataFile.print(now.second(), DEC);
dataFile.println();
dataFile.close();
} else {
// if the file isn't open, pop up an error:
Serial.println("error opening datalog.txt");
}
}
}


