Hello there,
I am experiencing a problem with running an Arduino Sketch on an MKR Zero. The sketch that is malfunctioning is adapted from
Arduino's MKR Zero Weather Data Logger, I have connected the Arduino MKR Zero to a DHT11 sensor via digital pin 3, a simple project that prints the temperature and humidity works. Anyway here is the code of the sketch,
#include <SD.h>
#include <RTCZero.h>
#include <DHT.h>
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = SS1;
unsigned long previousTime;
int loadDataCheck; //Checks if data needs to be loaded
RTCZero rtc;
/* Change these values to set the current initial time */
const byte seconds = 50;
const byte minutes = 44;
const byte hours = 17;
/* Change these values to set the current initial date */
const byte day = 1;
const byte month = 9;
const byte year = 16;
// the setup function runs once when you press reset
// or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("DataLogger Example:");
// 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:
return;
}
Serial.println("card initialized.");
//When power is supplied to the DHT22 sensor,
//don't send any instruction to the sensor
//within one second to pass unstable status
delay(1000);
Serial.println("Initializing DHT");
dht.begin();
Serial.println("Initializing RTC");
rtc.begin();
rtc.setTime(hours, minutes, seconds);
rtc.setDate(day, month, year);
rtc.setAlarmTime(0, 0, 0);
rtc.enableAlarm(rtc.MATCH_SS); //alarm attached every minute
rtc.attachInterrupt(dataCheck);
loadDataCheck=0;
previousTime=millis();
Serial.println("System ready...");
}
// the loop function runs over and over again forever
void loop() {
unsigned long currentTime=millis();
if ((currentTime-previousTime)>5000)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(100); // wait for a bit
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
previousTime=millis();
}
if (loadDataCheck) logData();
}
void dataCheck(){
loadDataCheck=1;
}
void logData(void) {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
String dataString = "";
dataString += "Temperature: " + String(temperature) + " C" + "\t" + "Humidity: " + String(humidity) + "%\t" + "Time: " + getTime();
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the Serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
loadDataCheck=0;
}
String getTime(void) {
String returnString = "";
if (rtc.getHours() < 10)
returnString += "0" + String(rtc.getHours());
else
returnString += String(rtc.getHours());
returnString += ":";
if (rtc.getMinutes() < 10)
returnString += "0" + String(rtc.getMinutes());
else
returnString += String(rtc.getMinutes());
returnString += ":";
if (rtc.getSeconds() < 10)
returnString += "0" + String(rtc.getSeconds());
else
returnString += String(rtc.getSeconds());
return returnString;
}
In this code, I have edited the DHT sensor module from DHT22 to DHT11 and the pin of the sensor, from 7 to 3 (I tested and it works). When I upload the sketch to my board via the Arduino Web Editor, the project runs as expected, but when I upload it using the desktop IDE, the SD Card module fails to initialise, in the section of code stating:
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
The Arduino MKRZero prints
Card failed, or not present to the Serial Monitor, this does not happen when running the code on the Arduino Web Editor. This is my first project working with the built-in SD Module of the Arduino MKRZero, though I have used the SD library several times before with an external SD Module and all projects worked.
I am running the latest version of the Arduino desktop IDE (1.8.5) on an iMac, the SD library is of version 1.2.1 both on the Desktop IDE and the Web Editor. This has never happened to me before, I am not sure if it is a Desktop IDE error or if the Arduino is malfunctioning, as I said, both the IDE and Web Editor use the same versions of the libraries and are at its newest version.
thank you for your help in advance.