Hi, friends!!! I need some help here.
I'm experiencing some strange issues when I try to log my CT readings to an SD card.
Somehow it seems that EmonLin is not compatible with the SD library.
My wiring is exactly the one presented here: http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor-measuring-current-only
The sketch works fine when:
- the sd card is not present (so, SD is not really being used); or
- the analog input used to read the CT sensor is grounded.
With the original circuit and the SD card present, the arduino resets all the time.
Here is my code:
#include <EmonLib.h>
#include <SD.h>
EnergyMonitor emon1;
void setup()
{
Serial.begin(9600);
Serial.println("\n\nStarting...");
// initialize sd card
Serial.print("- initializing sd card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) { // chipselect is pin 4
Serial.println(" failed.");
} else {
Serial.println(" done.");
Serial.print("- opening datalog.txt...");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
Serial.println(" done.");
dataFile.close();
} else {
Serial.println(" failed.");
}
}
Serial.print("- initializing energy monitors...");
emon1.current(0,30);
Serial.println(" done.");
Serial.println("Started!\n");
}
void loop()
{
// read current
float irms = emon1.calcIrms(1480);
// publish results
Serial.print(irms * 127.0);
Serial.print(" ");
Serial.print(irms);
// log results
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
char aux[12];
dtostrf(irms * 127, 10, 8, aux);
dataFile.print(aux);
dataFile.print(",");
dtostrf(irms, 10, 8, aux);
dataFile.print(aux);
dataFile.close();
Serial.println(" >> saved");
}
else { // if the file isn't open, pop up an error:
Serial.println(" >> save error");
}
}
And here is the wiring:
Re: Problems with SD storage (using Ethernet Shield R3) [SOLVED]
Ahh, just to avoid any wondering around memory issues, I tested that on Arduino Mega 2560 with exactly the same results.
Re: Problems with SD storage (using Ethernet Shield R3) [SOLVED]
My guess is you're crashing off the end of
char aux[12];
once you get a decent sized number back from the A/D conversion.
Re: Problems with SD storage (using Ethernet Shield R3) [SOLVED]
Hi dBC... I believe you nailed it!!!
I did a quick test here and it seems to be working now.
Many many sincere thanks for the light!!!!
Re: Problems with SD storage (using Ethernet Shield R3) [SOLVED]
Hi dBC, now it is confirmed, it solved the problem.
Many many thanks... the conversion was eventually generating numbers bigger than my array. And with time that was crashing everything. I changed the code and it runs flawlessly now.
Many Thanks!!!
Sincerely,
Manoel
Re: Problems with SD storage (using Ethernet Shield R3) [SOLVED]
Dear Manoel,
I am currently building kind of the same system as you have build here. I was wondering how you solved the problem as described here.
Regards,
Maarten