Digital voltmeter to monitor two boat batteries

Hello,

I've been crawling these forums and using google-fu to no avail. I have an Arduino connected to my boat, which operates on two 12V batteries (House & Starter). Those batteries are connected to everything via a power switch, which can be selected to 1, 2 or BOTH (see diagram below). The Arduino is therefore powered by either, or both, batteries at various times.

I am trying to figure out how to monitor the voltage of both of the batteries, but from what I have been able to ascertain is that you can't measure the voltage of the battery if that's also the Arduino's power source... or at least not without something more than what I have. Is there a feasible (or better) way of doing what I'm trying to do?

Thanks in advance!

You could put each battery to the Arduino through a resistor divider, to the Analog inputs and, with some code, determine the charge on the batteries.

You just need a couple of resistors to bring the 12V signal down into the 5V range that the ADC works in.

Vout = Vin * R2/(R1 +R2).

float batteryVoltage = analogRead(A0) * 5.0/1024;

analogRead of 4V input will return ~820.
820*5/1024 = 4.004V

CrossRoads:
You just need a couple of resistors to bring the 12V signal down into the 5V range that the ADC works in.

Vout = Vin * R2/(R1 +R2).

Perfect example of practical voltage dividing for this application.
However the line from +12 to Barrel Jack, or Vin, disturbs me. Couldn't You have used Tippex there? I'm joking but You get the point.

CrossRoads:
You just need a couple of resistors to bring the 12V signal down into the 5V range that the ADC works in.

Vout = Vin * R2/(R1 +R2).

I think that's what I'm doing with the voltage sensors (on the right side of my diagram). When I power my Arduino from a separate power source from the batteries that I'm measuring, I am able to obtain the voltage of the two batteries... But my issue seems to occur when I power the Arduino from those batteries; the voltage readings become unstable/inaccurate.

Can You post a wiring of this setup, powering from the batteries?

Keep in mind also that boat batteries are not really 12 volts - more like 13.xx and sometimes higher when charging - like automotive applications you would be prudent to protect against high voltages - there should be a ton of posts here dealing with automotive voltage issues

Here’s an example at home connected to a single 12V.

When the arduino is powered from a separate source, it’s reading ~13.14 - 13.25V. When it’s powered from the battery (using the buck converter at the bottom of the image) it’s reading ~12.04 - 12.15V. My multimeter is showing the battery at 12.75V

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

const int voltageSensor = A0;

float vOUT = 0.0;
float vIN = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;

void setup() {
lcd.init();
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0,0);
}

void loop() {
int voltage = analogRead(voltageSensor);
vOUT = (voltage * 5.0) / 1024.0;
vIN = vOUT / (R2/(R1+R2));
lcd.setCursor(0,0);
lcd.print(vIN);
lcd.print(“V”);
delay(500);
}

(You can right click “View Image” for full size)

saildude:
Keep in mind also that boat batteries are not really 12 volts - more like 13.xx and sometimes higher when charging - like automotive applications you would be prudent to protect against high voltages - there should be a ton of posts here dealing with automotive voltage issues

The upper charging level for a lead acid cell is 14.4 volt. At that voltage gas will be produced and that should be awoided...

Don't disregards spikes introduced by other equipment, especially in cars. Maybe the electrical environment is less violent in a boat but it depends on the other 12 volt consumers.

kezei:
Here's an example at home connected to a single 12V.

When the arduino is powered from a separate source, it's reading ~13.14 - 13.25V. When it's powered from the battery (using the buck converter at the bottom of the image) it's reading ~12.04 - 12.15V. My multimeter is showing the battery at 12.75V

(You can right click "View Image" for full size)

Please write a wiring diagram. It's impossible to see the details.
How is the 12 volt system connected to the controller for powering it?

Full size image: https://i.imgur.com/Eubw8Lp.png

Full size image: https://i.imgur.com/3vE4k6k.png

What is that mysterious device sending something out in the green line? Links, data sheet please.

Please using real wiring diagrams and not pictures.

Which Arduino? How will it be powered in the boat? What will the voltage on the AREF pin be when powered in the boat?

float aRef = 4.99; // voltage on AREF pin measured with DMM
float divider = 0.2; // 1 / divider ratio
float konstant;
float volts;

void setup()
{
Serial.begin(9600);   
konstant = 1024.0 * divider / aRef;
}
void loop()
{
   volts = analogRead(A0) / konstant;
   Serial.println(volts);
   delay(1000);
}

@Railroader: https://bit.ly/2YRDeiH - can't find a datasheet on it. It's a buck converter that's outputting 5V on both the terminals and USB. I'm running a USB cable from it to the Arduino.

@JCA34F:
1.Arduino Uno
2. the buck converter linked above/shown is what I'm using now/will be using on the boat, so it'll be 5V in via USB. The same battery(ies) that I'm trying to test. It depends on what the battery switch is set to, 1 BOTH or 2, which will change from time to time.
3. I'm not currently using the AREF pin, so I'm not sure?

Thanks for all of your help, by the way!

Okey, a buck converter, hopefully delivering a sufficient amount if current.
It's my opinion, never make a paralleling connection between 2 lead acid batteries. In the contacting moment a high current rush from one battery to the other will take place and it might damage the switch, making burns to its contacting surfaces.

Look at the UNO, on the left end of the upper row of pins there is one labeled AREF, measure the voltage on that pin (with respect to GND) and enter the value in the aRef variable at the top of my program. That should make your analog measurements accurate as long as the Vcc voltage is stable.

JCA34F:
Look at the UNO, on the left end of the upper row of pins there is one labeled AREF, measure the voltage on that pin (with respect to GND) and enter the value in the aRef variable at the top of my program. That should make your analog measurements accurate as long as the Vcc voltage is stable.

Alright, I've done that and I think it's working (needs a slight calibration for the output, but that's fine). Now, I'm wondering if the AREF value is going to change when I switch between battery 1, 2, and BOTH. Would it be better/possible to use the Arduino's internal 1.1V reference? If so, I'm not entirely sure how to modify the calculation.

You would need a 15 to 1 divider instead of 5 to 1 like you have.

float aRef = 1.1; // voltage on AREF pin measured with DMM
float divider = 0.0667; // 1 / divider ratio, 15:1
float konstant;
float volts;

void setup()
{
Serial.begin(9600); 
analogReference(INTERNAL) ;
konstant = 1024.0 * divider / aRef;
}
void loop()
{
  volts = analogRead(A0) / konstant;
  Serial.println(volts);
  delay(1000);
}

Railroader:
Okey, a buck converter, hopefully delivering a sufficient amount if current.
It's my opinion, never make a paralleling connection between 2 lead acid batteries. In the contacting moment a high current rush from one battery to the other will take place and it might damage the switch, making burns to its contacting surfaces.

Sorry I, and numerous switch manufacturers disagree, there are any number of combiner switches on the market for exactly this situation. Any boat or motor home that has separate House and Start batteries could benefit form such a combiner. The way the OP is trying go about the measurements is perhaps overly complicated in my opinion, but the principle is sound.