Arduino R3 not running independently with external battery pack

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.

Then answer @Dave_Lowther questions.

Your assumption is correct. Here is the drawing:


I am no artist, but this will do.

5xAA would be good battery.

1 Like

Looks like 4 AA batteries going into the Jack connector. Is that correct?
If so, that's not enough voltage.

1 Like

Is that a USB type battery pack connected to the USB connector?

Thanks for posting the code correctly.

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.

5.0 V (Vin = 7.0 V, Iout = 10 mA, TA = 25 °C)
Ref: https://www.digikey.ca/en/products/detail/onsemi/NCP1117ST50T3G/921289

Thank you for the response(And thank you for everyone's response), so that's my answer. How would I connect another battery pack(I have 4 of them)?

All for 4xAA?
You need 5xAA in series.

You still have not told us what the battery packs are

Are the a 5V packs, Alkaline or NMIH 4 AA, 4AAA, 5AA 6AA?

4x 1.5V AA batteries

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.

So that's 6V.
To high for the 5V pin and not enough for Vin

Two pack in in series will give you 12V but you should not have to many thing connected to the Uno

What are all the things you have connected besides SD and TDS?

Do you have any diodes?

I have an RTC module. But thinking about it know I'll just buy a 5x AA battery holder. Thank you for trying to help me out! That answers my question.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.