I am doing a field experiment and need to power my arduino uno with project with two 9 volt batteries. I know this isn't the best but it is what I have, so please let's not focus on that.
My project uses:
1 HW-125 micro sd reader
1 DS3231 RTC
1 16 channel digital to analog break out board
2 12V solenoid valves
6 HW-390 capacitive soil moisture sensors
I am using power down mode from the low power library in a for loop to make the arduino sleep for 6 hours at a time, wake up, read the moisture probes, write to the SD card, and if needed turn the valves on for 5 seconds every 3ish days. The valves will be connected to a second 9V battery, not the arduino at all.
My code ( I know it could be more efficient, but it works and that's good enough for me):
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <light_CD74HC4067.h>
#include <DS3231.h>
#include <uRTCLib.h>
#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers
#include <LowPower.h>
// SD card
const int chipSelect = 10;
File Data1;
// RTC
uRTCLib rtc(0x68);
// dry and wet solenoid valves
const uint8_t dryValve = 9;
const uint8_t wetValve = 8;
// 3 drought treatment probes
const int p1ValAir = 615;
const int p1ValWater = 300;
const int p2ValAir = 557;
const int p2ValWater = 230;
const int p3ValAir = 510;
const int p3ValWater = 203;
// 3 wet treatment probes
const int p4ValAir = 499;
const int p4ValWater = 184;
const int p5ValAir = 500;
const int p5ValWater = 194;
const int p6ValAir = 503;
const int p6ValWater = 197;
//Mux control pins
int s0 = 4;
int s1 = 5;
int s2 = 6;
int s3 = 7;
CD74HC4067 mux(s0, s1, s2, s3);
//Mux in "SIG" pin
int SIG_pin = A3;
void setup() {
Serial.begin(9600);
// valves
pinMode(dryValve, OUTPUT);
pinMode(wetValve, OUTPUT);
// for analog probes on MUX
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
// SD
SD.begin(chipSelect);
Data1 = SD.open("Data1.csv", FILE_WRITE);
String header = "Date, Time, Temp, p1, p2, p3, dryAverage, p4, p5, p6, wetAverage";
Data1.println(header);
Data1.close();
// RTC
URTCLIB_WIRE.begin();
}
void loop() {
int p1Val = analogRead(A0);
int p2Val = analogRead(A1);
int p3Val = analogRead(A2);
int p4Val = readMux(0);
int p5Val = readMux(1);
int p6Val = readMux(2);
// percentages for dry probes
int percent1 = map(p1Val, p1ValAir, p1ValWater, 0, 100);
percent1 = constrain(percent1, 0, 100);
int percent2 = map(p2Val, p2ValAir, p2ValWater, 0, 100);
percent2 = constrain(percent2, 0, 100);
int percent3 = map(p3Val, p3ValAir, p3ValWater, 0, 100);
percent3 = constrain(percent3, 0, 100);
// percentages for wet probes
int percent4 = map(p4Val, p4ValAir, p4ValWater, 0, 100);
percent4 = constrain(percent4, 0, 100);
int percent5 = map(p5Val, p5ValAir, p5ValWater, 0, 100);
percent5 = constrain(percent5, 0, 100);
int percent6 = map(p6Val, p6ValAir, p6ValWater, 0, 100);
percent6 = constrain(percent6, 0, 100);
// get average of 3 dry probes
int drySum = percent1 + percent2 + percent3;
int dryAverage = drySum / 3;
// get average of 3 wet probes
int wetSum = percent4 + percent5 + percent6;
int wetAverage = wetSum / 3;
// RTC reading
rtc.refresh();
// read to SD card here
Data1 = SD.open("Data1.csv", FILE_WRITE);
// record date, time, and temp to SD card
Data1.print(rtc.month());
Data1.print("/");
Data1.print(rtc.day());
Data1.print(",");
Data1.print(rtc.hour());
Data1.print(":");
Data1.print(rtc.minute());
Data1.print(",");
Data1.print(rtc.temp() / 100);
Data1.print(",");
// record dry probe data to SD
Data1.print(percent1);
Data1.print(",");
Data1.print(percent2);
Data1.print(",");
Data1.print(percent3);
Data1.print(",");
Data1.print(dryAverage);
Data1.print(",");
// record wet probe data to SD
Data1.print(percent4);
Data1.print(",");
Data1.print(percent5);
Data1.print(",");
Data1.print(percent6);
Data1.print(",");
Data1.print(wetAverage);
Data1.print(",");
// save data to SD
Data1.println("");
Data1.close();
Serial.println("data saved");
// turn on dry pump for 10 seconds when dry average = 30%
if (dryAverage <= 30) {
digitalWrite(dryValve, HIGH);
delay(1000);
digitalWrite(dryValve, LOW);
delay(500);
}
// turn on wet pump for 5 seconds when wet average = 60%
if (wetAverage <= 60) {
digitalWrite(wetValve, HIGH);
delay(1000);
digitalWrite(wetValve, LOW);
delay(500);
}
Serial.print("Average dry moisture = ");
Serial.println(dryAverage);
delay(500);
Serial.print("Average wet moisture = ");
Serial.println(wetAverage);
delay(500);
// enter deep sleep mode for 6 hours
sleepyTime();
}
// deep sleep for longer than 8 seconds - sorta
void sleepyTime()
{
digitalWrite(LED_BUILTIN,LOW);
for (int i = 0; i < 2700; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
//Serial.println(i);
//delay(200);
}
digitalWrite(LED_BUILTIN,HIGH);
}
int readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}
Roughly how long will a 9V battery power the arduino? Thank you!

