Hello, I am trying to make a school project about monitoring a river with a TDS sensor. Everything works, except for the fact that when I try to run it without a computer data doesn't save to the SD card. I believe there is something wrong with the code.
#include <DS3231.h>
#include <SPI.h>
#include <SD.h>
#include <EEPROM.h>
#include "GravityTDS.h"
#define TdsSensorPin A1
GravityTDS gravityTds;
int CS_Pin = 4;
float temperature = 25,tdsValue = 0;
DS3231 rtc(SDA, SCL);
File file;
void setup()
{
pinMode(CS_Pin, OUTPUT);
SD.begin();
gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds.begin(); //initialization
rtc.begin();
rtc.setTime(21, 10, 0);
rtc.setDate(6, 2, 2025);
}
void loop()
{
//temperature = readTemperature(); //add your temperature sensor and read it
file = SD.open("data.txt", FILE_WRITE);
if (file)
{
gravityTds.setTemperature(temperature); // set the temperature and execute temperature compensation
gravityTds.update(); //sample and calculate
tdsValue = gravityTds.getTdsValue(); // then get the value
file.print(rtc.getTimeStr());
file.print(",");
file.print(rtc.getDateStr());
file.print(",");
file.print(tdsValue,0);
file.println("ppm");
delay(1000);
}
}
Let me check I understood correctly: It works correctly when powered via USB from PC, but it doesn't work when you power it some other way?
How are you powering it when it's not connected to PC via USB? Ideally show us a drawing of how the power is wired in this case.
First you need to learn the correct way to post your code on the Forum.
In the IDE, click on Edit then Copy for Forum. That will copy your code for use on this forum.
Then come back to the forum and do a simple paste.
In order to power your project with external batteries, through the DC2 1MMX input, they will need to provide at more then 7V.
The schematic of the power section of R3 shows that it uses the NCP1117ST50 regulator, and 1 diode, and below the drawing are the specifications of this regulator for 5.0V output.
You can put two of those in series for 12V. Not ideal though.
Or if you are for tinkering, you can fill only 5 batteries and make a jumper to close the circuit.
I suggest to buy a 5xAA holder.