12V Relay Drive circuits conflicting with AnalogRead of Battery

We are using a MEGA2560 to control some farming equipment and a 400lb Linear actuator that is driven via 12V switching relays. The entire unit is powered by a 12V battery, where the A1 Port is measuring the voltage via a voltage divider (510KOhm and 1.1MOhm), and converting it to a “battery remaining” time in hours. Anything less than 11.5V is considered “dead” because it won’t have enough juice to drive the rest of the system. The linear actuator is controlled by two 12VDC Relays that control “on/off” and “fwd/rev”, and the actuator has an encoder system that is fed back to the Arduino to calculate the distance that the actuator is extended. The actuator is controlled by a potentiometer. The issue that is popping up is that even when both relays are “off”, the ADC read of the battery goes to “0 hours remaining”. If the system is in idle, it prints the battery correctly. I wrote some pseudocode because the real code for this is astronomically long:

void loop() {
  if(run_switch is OFF){ //IDLE mode
    retract linear actuator if it is extended;
    read battery ADC;
    Print to LCD;
    other stuff;
  }
  else{ //RUN mode
    while(potentiometer is moving){
      calculate distance setting of linear actuator;
      //We'll refer to this as "setting" (3 inchs, 5 inches, etc)
    }

    read linear actuator distace;
    //we'll call this "distance" (3 inchs, 5 inches, etc)

    if(distance > setting + hysteresis){ //retract
      fwd/rev relay = ON;
      on/off relay = OFF;
    }
    else if(distance > setting - hysteresis){ //extend
      fwd/rev relay = ON;
      on/off relay = ON;
    }
    else{ //off and read battery
      fwd/rev relay = OFF;
      on/off relay = OFF;
      read battery ADC; //this is where it reads 0 hrs remaining
      Print to LCD;
      other stuff;
    }
  }
}

Any Idea as to why the second half of the code keeps reading a low battery voltage? Any help would be appreciated.

Better use 51k and 110k and measure each channel twice, discarding the first reading. One difference of idle mode is that the pot channel is not measured, i.e. the ADC channel is never changed.

Did you prevent cut-off spikes at all coils using snubbers or diodes?
Please show a complete circuit diagram, including power supplies.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

To add code please click this link;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

A 0.1uF capacitor between A1 and ground will also help with any noise on the battery voltage.

Thanks… Tom… :grinning: :+1: :coffee: :australia:

Your wiring layout and cable sizes can influence things to a great extent( hence prev request for diagram) - you don’t want to measure battery voltage at the end of a long cable. .

I’d agree with lower divider resistors and I would also use the internal reference as the 5v supply to the processor may alter or not be exactly 5v
Any voltmeter you design needs calibrating anyway .

I’d put a couple of diodes in series with the Arduino supply to drop 1.5v and give the on board regulator an easier time , and don’t power any significant load directly .