Hey there, I'm working on a temperature control system project using an AT mega 2560 and am including a data logging feature in it. The SD card is recognized fine, my issue is that when my Arduino is not connected to my computer, it is not writing data onto the SD card. I am powering the Arduino from a 9V barrel jack. When I'm powering the Arduino via 5V connection from my laptop, the data is recorded and saved perfectly.
Here's my code
Edit: Through further testing with the use of an LED, it seems that the SD.begin() function isn't going true when powered using the barrel jack, yet it does when powered by my laptop.
Edit: This is the SD module I am using Amazon.ca
#include <LiquidCrystal.h> //LCD library
#include <LiquidCrystal_I2C.h> //LCD I2C communication library
#include <OneWire.h> //One wire library
#include <DallasTemperature.h> //DS18B20 library
#include <SPI.h> //SPI library
#include <SD.h> //SD module library
#define ONE_WIRE_BUS 4 //One wire bus for enclosure temp sensor
#define ONE_WIRE_BUS 7 //One wire bus for outside temp sensor
OneWire oneWirebox(4); //Assign one wire bus to name for enclosure
OneWire oneWireroom(7); //Assign one wire bus to name for outside
DallasTemperature box(&oneWirebox); //Assign one wire variables to use DS18B20 library
DallasTemperature room(&oneWireroom); // ^^same as above^^
LiquidCrystal_I2C lcd(0x27, 20, 4); //Set parameters for LCD screen
int Lower_limit = 20; //Set lower limit as integer value
int Increase_state = 0; //Set heater signal to low/off to start
int Decrease_state = 0; //Set fan signal to low/off to start
int Increase_button = 5; //Set increase button signal to pin 5
int Decrease_button = 6; //Set decrease button signal to pin 5
int count_value = 25; //Set starting count value (desired temp)
int prestate = 0; //Used to control pushbutton hold to increment or push to increment
int Increase_signal = 2; //Set heater signal to pin 2
int Decrease_signal = 3; //Set fan signal to pin 3
int temp = 21;
int temp2 = 0;//Set temp to integer value
const int chipSelect = 53; //Set SD module CS pin to pin 53
unsigned long ms_from_start = 0;
unsigned long ms_previous_read_datalog = 0;
unsigned long datalog_interval = 5000; //This determines how often data is logged to SD card
unsigned long ms_previous_read_scan = 0;
unsigned long scan_interval = 100; //This determines how often rest of loop refreshes
File myFile; //Start myFile
void setup() {
Serial.begin(9600); //Set serial baud rate
lcd.begin(20,4); //Start LCD with screen size 20x4
box.begin(); //Start DS18B20 for enclosure
room.begin(); //Start DS18B20 for room
pinMode(Increase_button, INPUT_PULLUP); //Set Input Pullup for Increase button
pinMode(Decrease_button, INPUT_PULLUP); //Set Input Pullup for Decrease button
pinMode(Increase_signal, OUTPUT); //Set heater pin to output
pinMode(Decrease_signal, OUTPUT); //Set fan pin to output
lcd.init(); //initialize LCD screen
lcd.backlight(); //Turn on LCD backlight
room.requestTemperatures(); //Request temp from room DS18B20
int temp1 = room.getTempCByIndex(0); //Assign room temp reading to integer value
Lower_limit = temp1; //Assign integer value for room to lower limit
//Serial.print(Lower_limit); //Serial print lower limit value **UNCOMMENT FOR TESTING**
Serial.print("Initializing SD card..."); //Serial print to signal beginning SD initialization
if (!SD.begin()) { //Start if loop for if SD fails to open
Serial.println("initialization failed!"); //If SD fails to begin, serial print initialization failed
}
Serial.println("initialization done."); //Serial print initialization done if SD begins succesfully
myFile = SD.open("logdata.txt", FILE_WRITE); //Open SD and create file named logdata.txt
if (myFile){
myFile.print("Real Temp"); //Print Temp header into logdata.txt
myFile.print("\tSet Temp"); //Print Set Temp header into logdata.txt
myFile.print("\tState"); //Print State header into logdata.txt
myFile.println("\t\tTime"); //Print Time header into logdata.txt
myFile.close(); //Close file
}
}
void loop() {
ms_from_start = millis(); //Set time from start to variable
if (ms_from_start - ms_previous_read_datalog > datalog_interval){ //If time from start - previous read value is greater than datalog interval(1 minute), run if statement
ms_previous_read_datalog = ms_from_start; //Sets time from start to previous read value for next loop comparison
temp2 = box.getTempCByIndex(0); //Sets requested enclosure temp to variable
unsigned long minutes = ((millis()/1000)/60); //Converts milliseconds to minutes
myFile = SD.open("logdata.txt", FILE_WRITE); //Open logdata.txt file and write
if (myFile &&(count_value > temp2) && minutes <=60){ //If desired temperature > real temp and less than 60 minutes have passed
myFile.print(box.getTempCByIndex(0)); //Write real temp into logdata.txt
myFile.print("\t\t");
myFile.print(count_value); //Write desired temp into data file
myFile.print("\t\tHeating"); //Write "Heating" into logdata.txt
myFile.print("\t\t");
myFile.println(minutes); //Write minute that info was recorded
myFile.close(); //Save and close logdata.txt
}
else if (myFile &&(count_value < temp2) && minutes <= 60){ //If desired temperature < real temp and less than 60 minutes have passed
myFile.print(box.getTempCByIndex(0)); //Write real temp into logdata.txt
myFile.print("\t\t");
myFile.print(count_value); //Write desired temp into data file
myFile.print("\t\tCooling"); //Write "Cooling" into logdata.txt
myFile.print("\t\t");
myFile.println(minutes); //Write minute that info was recorded
myFile.close(); //Save and close logdata.txt
}
else{
Serial.println("Error opening file"); //If file fails to open display serial message
}
}
if (ms_from_start - ms_previous_read_scan > scan_interval){ //If time from start - previous scan time is greater than scan interval time, run if statement
//Serial.print(sensors.getTempCByIndex(0)); //Uncomment for testing
lcd.setCursor(0, 0); //Set LCD Cursor
lcd.print("Desired: "); //Print text on LCD
lcd.setCursor(10, 0); //Set LCD Cursor
lcd.print(count_value); //Print desired temperature on LCD
lcd.setCursor(0, 1); //Set LCD Cursor
lcd.print("Real Temp: "); //Print text on LCD
lcd.setCursor(13, 1); //Set LCD Cursor
lcd.print(box.getTempCByIndex(0)); //Print real temp on LCD
lcd.setCursor(0, 2); //Set LCD Cursor
lcd.print("State: ");
Increase_state = digitalRead(Increase_button); //Set Increase_button to be digitally read and assign value to Increase_state
Decrease_state = digitalRead(Decrease_button); //Set Decrease_button to be digitally read and assign value to Decrease_state
if(Increase_state == LOW && prestate == 0){ //If Increase pushbutton is pressed, increase count_value (desired temp) by 1
count_value++;
//Serial.print(count_value); //Uncomment for testing
prestate = 1; //Change to 0 for hold to increase
}
else if(Decrease_state == LOW && count_value > Lower_limit && prestate == 0){ //If Decrease pushbutton is pressed AND count_value is greater than lower limit, increase
count_value--; //count_value (desired temp_ by 1
//Serial.print(count_value); //Uncomment for testing
prestate = 1; //Change to 0 for hold to decrease
}
else if(Increase_state == HIGH && Decrease_state == HIGH){ //If no buttons are pressed set prestate as 0
prestate = 0;
}
if(count_value < box.getTempCByIndex(0)){ //If desired temp is lower than real temp, run if statement
digitalWrite(Increase_signal, HIGH);
digitalWrite(Decrease_signal, LOW);
lcd.setCursor(7, 2);
lcd.print("Cooling");
}
else if(count_value > box.getTempCByIndex(0)){ //If desired temp is greater than real temp, run if statement
digitalWrite(Increase_signal, LOW);
digitalWrite(Decrease_signal, HIGH);
lcd.setCursor(7, 2);
lcd.print("Heating");
}
}
}