Practical case: Battery charging automation with relay

Schematic — Practical case: Battery charging automation with relay

Objective and use case

What you will build: An automatic charging system for a 12 V battery that connects and disconnects a 15–18 V DC charger using a comparator, a relay and LED signaling. It will control charging between configurable thresholds to protect lead-acid batteries.

What it is for

  • Automate disconnection of the charger when the battery reaches ~14.4 V (lead-acid), preventing overcharging due to oversight.
  • Reconnect the charger automatically when the battery drops below ~12.5 V, keeping it operational without constant supervision.
  • Protect batteries in caravans, boats or small off‑grid solar systems where the user cannot constantly monitor the charge level.
  • Integrate basic battery management automation in low-power energy systems (LED lighting, routers, small pumps).
  • Log voltage and battery behavior during charge/discharge cycles for state‑of‑health and capacity analysis.

Expected result

  • Battery voltage stabilized between 12.5 V and 14.4 V, verifiable with a multimeter at the battery terminals after several cycles.
  • Relay switching hysteresis of approximately 1.5–2 V between the on and off thresholds, measurable at the comparator reference point.
  • Charging current interrupted (I_CARGA ≈ 0 A) when V_BAT ≥ 14.4 V, observable in series with the charger output.
  • Charging current restored (I_CARGA > 0 A according to the charger) when V_BAT ≤ 12.5 V, with repeatable switching in different tests.
  • Status LED with clear indication of “Charging” (LED on) and “Charge finished” (LED off or different color), visible to the naked eye.

Target audience: Students and advanced hobbyists in power and control electronics; Level: Intermediate–advanced (working with comparators, relays and multimeter measurements).

Architecture/flow: 15–18 V DC supply/charger → power relay in series with the charging line → 12 V battery; the battery voltage is taken via a resistive divider to a voltage comparator with hysteresis (feedback network), whose output drives the relay coil and the status LED. When 14.4 V is exceeded, the comparator deactivates the relay (cuts off I_CARGA); when it drops below 12.5 V, the comparator reactivates the relay and restores charging.

Materials

  • 1× 15–18 V DC supply (for example, 18 V / 3 A, or the rectified secondary of a transformer).
  • 1× 12 V battery (sealed lead-acid or AGM, 7–20 Ah for the example).
  • 1× 12 VDC SPST or SPDT relay, 12 V coil, contacts ≥ 10 A @ 30 VDC.
  • 1× LM358 operational amplifier (to be used as a comparator).
  • 1× NPN transistor such as 2N2222 or BC337 (to drive the relay coil).
  • 1× 1N4007 diode (flyback diode for the relay coil).
  • 1× Red LED (“Charging” indicator).
  • 1× Resistor [R_LED] 1 kΩ 1/4 W (current limiting for the LED).
  • 1× Resistor [R1] 100 kΩ 1/4 W (top of the reference divider).
  • 1× Resistor [R2] 27 kΩ 1/4 W (bottom of the reference divider).
  • 1× Resistor [R3] 100 kΩ 1/4 W (top of the divider from the battery).
  • 1× Resistor [R4] 27 kΩ 1/4 W (bottom of the divider from the battery).
  • 1× Resistor [R5] 220 kΩ 1/4 W (feedback for comparator hysteresis).
  • 1× Resistor [R6] 4.7 kΩ 1/4 W (transistor base resistor).
  • 1× 1N4148 diode ([D_REF]) to improve stability of the reference point (optional but we include it).
  • 1× Breadboard or prototyping board.
  • Connecting wires and terminals suitable for the charging current.
  • 1× Digital multimeter.
  • 1× Test load for the battery (for example, a 12 V / 21 W car bulb).

Wiring guide

  • General power:
  • Connect the positive of the 15–18 V supply to the node labeled +V_FUENTE.
  • Connect the negative of the supply to GND (system common ground).
  • Connect the negative of the battery to GND.
  • Keep the battery positive (+BAT) separated from the charger; they will only be joined through the relay contact.

  • Reference divider (comparison threshold):

  • Connect [R1] 100 kΩ between +V_FUENTE and node V_REF.
  • Connect [R2] 27 kΩ between V_REF and GND.
  • Connect diode [D_REF] 1N4148 between V_REF and GND, with the cathode at V_REF and the anode at GND (for soft voltage clipping and stability).
  • Connect the non‑inverting pin (+) of the LM358 (channel A, labeled here as LM358A+) to node V_REF.

  • Battery voltage divider (V_BAT measurement):

  • Connect [R3] 100 kΩ between the battery positive (+BAT) and node V_SENS.
  • Connect [R4] 27 kΩ between V_SENS and GND.
  • Connect the inverting pin (–) of the LM358 (channel A, labeled as LM358A-) to node V_SENS.

  • Hysteresis (positive feedback):

  • Connect [R5] 220 kΩ between the LM358A output (node V_OUT_CMP) and node V_SENS.

  • LM358 power:

  • Connect the VCC pin of the LM358 to +V_FUENTE.
  • Connect the GND pin of the LM358 to GND.

  • Power stage with transistor and relay:

  • Connect the LM358A output (node V_OUT_CMP) to one end of [R6] 4.7 kΩ.
  • Connect the other end of [R6] to node VB (base of the NPN transistor).
  • Connect the emitter of the NPN transistor (2N2222 or BC337) to GND.
  • Connect the relay coil between +V_FUENTE and node VC (transistor collector).
  • Connect the collector of the transistor to node VC.
  • Connect the 1N4007 diode in parallel with the relay coil: anode to VC, cathode to +V_FUENTE (flyback diode).

  • Relay power contacts:

  • Use the relay common contact (COM) to connect to the charger positive (+CARGA from the supply) or to +V_FUENTE if using the same supply.
  • Use the relay normally open (NO) contact to connect to the battery positive (+BAT).
  • Leave the normally closed (NC) contact unused in this case.
  • Verify that the negative of the charging supply and the negative of the battery are tied together at GND.

  • Charging indicator LED:

  • Connect [R_LED] 1 kΩ between +V_FUENTE and the LED anode.
  • Connect the LED cathode to node VC (collector / low end of the relay coil). The LED will light when the transistor conducts and the relay is activated (“Charging” mode).

Schematic

                                                 +V_FUENTE
                          |
                +---------+----------------------+
                |                                |
              [R1] 100kΩ                        [R_LED] 1kΩ
                |                                |
          V_REF o                                o---[LED]---+
                |                                |           |
             [R2] 27kΩ                           |          VC node
                |                                |           o
               GND                              GND          |
                |                                             |
             [D_REF] 1N4148                                  |
              (k a V_REF, a a GND)                           |
                                                              |
                       LM358 (canal A)                       |
                   +---------------------+                    |
    V_REF node --> | LM358A+         OUT| --> V_OUT_CMP o----+----[R6] 4.7kΩ----o VB node
    V_SENS node -->| LM358A-            |                                  |
                   |                 GND|------------------------------+   |
                   |                VCC |--------------------------+   |   |
                   +---------------------+                        |   |   |
                                                                 |   |   |
                                                              +V_FUENTE   |
                                                                 |        |
                                                                 |       [Q1] NPN 2N2222
                                                                 |        C
                                                              [Relay]     |
                                                              Coil        |
                                                                       +V_FUENTE  VC node o
                                                                       |
                                                                       E
                                                                       |
                                                                      GND


Batería y divisor de medición:

                                   +BAT o----[R3] 100kΩ----o V_SENS node
                                   |
                                 [R4] 27kΩ
                                   |
                                  GND


Histéresis (realimentación):

      V_OUT_CMP o----[R5] 220kΩ----o V_SENS node


Contactos de potencia del relé:

                          +CARGA (+ de la fuente de carga o +V_FUENTE)
                          |
                        COM (relé)
                          |
                         NO
                          |
                        +BAT o (positivo de batería)

   Negativo de fuente de carga ---- GND ---- Negativo de batería
                            ---
Schematic (ASCII)

Measurements and tests

  • Pre‑check without battery:

    • Disconnect the battery and the charger from the relay contacts.
    • Power the circuit with nominal +V_FUENTE (for example, 15–18 V).
    • Measure V_REF with the multimeter (red probe on V_REF, black probe on GND). Adjust, if you change values, so that it is around 4–5 V, such that it corresponds to 14.4 V on the battery scale (ratio of divider R3–R4).
    • Measure V_SENS with the multimeter and check that its value follows the voltage you manually apply (for example, using an adjustable supply on node +BAT, with low current).
  • High threshold measurement (disconnection):

    • With the battery connected and a charging source on +CARGA, slowly raise the battery voltage (ideally with an adjustable charger or lab supply in parallel with the battery).
    • Measure the voltage at +BAT with respect to GND using the multimeter.
    • Observe the point at which the relay deactivates (you will hear a “click” and the LED will go off). Record that battery voltage as V_UMBRAL_ALTO (for example, 14.4 V).
    • Verify that at that time the charging current (measured in series between the supply and the battery, with the multimeter in ammeter mode) drops practically to zero. This current is called I_CARGA (current flowing into the battery).
  • Low threshold measurement (reconnection):

    • Disconnect the charging source so that the battery discharges slightly through the test load (for example, a 12 V / 21 W car bulb connected between +BAT and GND).
    • Measure the voltage at +BAT with the multimeter and wait for it to gradually drop.
    • Once V_BAT falls, observe at what voltage the relay turns back on (LED lights and you hear another “click”). Record that voltage as V_UMBRAL_BAJO (for example, ~12.5 V).
    • Check I_CARGA again by placing the multimeter in series between the charger and the relay COM terminal: when the relay activates, the current I_CARGA should be consistent with the charging stage (for example, 0.5–2 A depending on your supply).
  • Hysteresis measurement:

    • Calculate the difference ΔV_HIST = V_UMBRAL_ALTO – V_UMBRAL_BAJO.
    • This difference should be approximately 1–2 V. If it is too large or too small, you can adjust the value of [R5] 220 kΩ (increasing it reduces hysteresis, decreasing it increases it).
    • During these tests, observe the comparator output voltage V_OUT_CMP with the multimeter:
      • High (close to +V_FUENTE) when the relay is active (“Charging” mode).
      • Close to GND when the relay is deactivated (“Charge finished” mode).
  • Definition of measured variables:

    • V_BAT: battery voltage; measured between +BAT and GND with the multimeter in voltmeter mode.
    • V_REF: internal comparator reference voltage; measured between node V_REF and GND.
    • V_SENS: voltage proportional to V_BAT in the R3–R4 divider; measured between node V_SENS and GND.
    • I_CARGA: charging current flowing from the supply to the battery; measured by opening the series circuit and placing the multimeter in ammeter mode between the charger output and the relay COM contact.

Fine tuning and advanced considerations

  • Exact threshold adjustment:
  • Values R1, R2, R3 and R4 determine the relationship between V_BAT and V_SENS, and therefore the switching thresholds.
  • You can replace R1 with a 100 kΩ potentiometer in series with a fixed resistor to have fine adjustment of V_REF.
  • Remember that the LM358 operates from a single supply and its output does not go down to exact zero, but low enough to control the transistor without issue.

  • Relay contact protection:

  • If the charging current is high or the load is inductive (for example, a DC‑DC converter), consider adding a diode, RC snubber or varistor across the relay contacts to extend its life.
  • Verify that the relay has sufficient current and voltage rating (for example, ≥10 A @ 30 VDC for typical 12 V / 10 A systems).

  • Power and dissipation estimation:

  • Dissipation in the divider resistors (R1–R4) is low due to their high value (100 kΩ / 27 kΩ), so 1/4 W is sufficient.
  • Transistor Q1 will barely dissipate any power (the coil typically draws 30–100 mA), but it is good practice to allow thermal margin, especially if a relay with a higher‑consumption coil is used.

Common mistakes

  • Miswiring the relay contacts:
  • Make sure to use COM and NO, not NC, if you want to charge only when the relay is activated.
  • Forgetting the flyback diode:
  • Without the 1N4007 diode in parallel with the coil, voltage spikes when deactivating the relay can damage the transistor or the LM358.
  • Not sharing ground:
  • The negative of the supply, the negative of the battery, the LM358 GND and the transistor emitter must all be connected together at a single common GND node.
  • Inverted LED polarity:
  • The anode must go to +V_FUENTE through [R_LED], the cathode to node VC (which approaches GND when the transistor conducts).

Safety

  • You are working with relatively high currents (possibly tens of amperes in large batteries). Use cables suitable for the rated current.
  • Never deliberately short‑circuit the battery: you can generate sparks, excessive heat and risk of burns.
  • Add a fuse in series with the battery positive near the (+BAT) terminal to protect against accidental short circuits.
  • Always verify polarity before connecting; reversing +BAT and GND can destroy the circuit and be dangerous for the battery.

Possible improvements

  • Replace the LM358 + transistor + relay with a P‑channel MOSFET or a solid‑state relay module to reduce losses and mechanical noise.
  • Add the second LM358 comparator as undervoltage protection (battery cutoff when it drops below 11 V to extend its life).
  • Incorporate a microcontroller (for example, an Arduino) to log voltages, currents and charge times, and display data on a screen.
  • Include an NTC (temperature sensor) and an extra channel to compensate the charge threshold according to battery temperature.

More Practical Cases at Prometeo.blog

Find this product and/or books on this topic on Amazon

Go to Amazon

As an Amazon affiliate, I earn from qualifying purchases. If you buy through this link, you help keep this project running.

Quick quiz

Question 1: What is the main function of the system described in the article?




Question 2: What type of batteries is this system mainly intended to protect?




Question 3: Which component is used to physically switch the connection of the charger to the battery?




Question 4: In what voltage range does the charger connected to the system typically operate?




Question 5: What approximate battery voltage is mentioned as the upper threshold for disconnecting charging in lead‑acid batteries?




Question 6: Around what battery voltage will the charger automatically reconnect according to the description?




Question 7: Which feature of the system prevents the relay from continuously switching around a single voltage point?




Question 8: What is the expected result for the battery voltage after several charge and discharge cycles?




Question 9: How should the charging current behave when the battery voltage reaches or exceeds the upper threshold (~14.4 V)?




Question 10: In what type of applications is this battery management system especially useful?




More Practical Cases on Prometeo.blog

Carlos Núñez Zorrilla
Carlos Núñez Zorrilla
Electronics & Computer Engineer

Telecommunications Electronics Engineer and Computer Engineer (official degrees in Spain).

Follow me:
Scroll to Top