PIC 12F675 based Solar Battery Charger

This circuit was build to control charging/discharging of a solar powered battery. A 12F675 monitors the battery voltage through an ADC channel and connects/disconnects solar panel and the load to avoid over charging and over discharging the battery.

An LED connected to the micro controller indicates the state of the battery. When the battery is fully charged (>13v) it blinks in long pulses (2s) and the solar panel is disconnected. When the battery is charging i.e. 12.3v to 13v short pulses about 200ms in each 2s. When the voltage drops below 12.3v the load is disconnected from the battery and LED is blinked  two 25ms pulses in each 2s interval.

But there is an issue with this circuit. When the load is connected while the battery is charged to say 12.5v, battery voltage suddenly drops below 12.3v due to current demand from the load and the internal resistance. Open circuit voltage of a battery is not what you get while running. When the load is disconnected, voltage goes back to the original value. This causes the circuit to take false decision and disconnect the load, then voltage restores and the load is connected back… endless loop. This can be avoided if we can detect when the load (say home lighting system) is taking current. A current sense resistor can be used for this. At that time program should use somewhat lower voltage to determine the ‘low battery’ state.





Note that the solar panel is connected through a  normally closed relay pins and the load connected through normally open pins.


Program (HiTec-C)

#include <htc.h>
#include <pic.h>


#define _XTAL_FREQ 4000000

void init()
    TRISIO = 0b00000001; 
    CMCON = 0b00000111; // Comparator Off
    ADCON0 = 0b10000001; // Ref = Vdd, Chan = 0, AD = On
    ANSEL = 0b01010001; // FOSC/16
    __delay_us(30); // Acquisition Delay Min 20 uSec

int read_adc()
    unsigned int n;
    GODONE = 1; // Start conversion
    while(GODONE); // wait for done
    n = (unsigned int)ADRESH << 8 | ADRESL;
    return n;

void delay_ms(int n)
    while (n-- > 0)

void FlashLED(int delay, int count)
    while (count-- > 0)
        GPIO2 = 1;
        GPIO2 = 0;

void main(void)

    GPIO1 = 0;
    GPIO5 = 0;

    FlashLED(1000, 1);

    while (1)
        unsigned int t;
        int bCharging;
        int bReady;

        t = read_adc();
        // Note: adjust following magic numbers according to your setup.
        // Battery Level High
        if (t > 900)
            GPIO5 = 1; // stop charging
            bCharging = 0;
        else if (t < 860)
            GPIO5 = 0; // charging
            bCharging = 1;
        // Battery Level Low
        if (t < 750)
            bReady = 0; // battery is discharged a lot
            GPIO1 = 0; // disconnect the load
        else if (t > 770)
            GPIO1 = 1; // battery ready for working
            bReady = 1;

        if (bCharging == 0) // > 13v
            FlashLED(2000, 1);
        else if (bReady == 1) // > 12.3v 
            FlashLED(200, 1);
        else if (bReady == 0) // < 12.3v
            FlashLED(25, 2);


15 thoughts on “PIC 12F675 based Solar Battery Charger

  1. Nice circuit, Thank you for sharring. please advice how can i change the cut off chrge voltage to max 14.4v? and load cutt off to 11v?

    • Good to here you like this. Actually that is experimental.

      1. Disconnect the solar panel and give 14.4v to the terminal named Bat+.
      2. Change the 100k preset such that ADC pin receives about 4v.You should never touch the preset after this
      3. Now the hard part. You have to check what read_adc() returns. You can use a debugger, or by blinking the led if the value is larger than a value you guess. With trial and error you can find the value.
      4. Substitute that value (say n) for 900 in the program. That is the fully charged state.
      5. Replace 860 with n-40.
      6. Give 11v to Bat+ and find what read_adc returns. Replace 750 with that value (say m).
      7. Replace 770 with m+20
      8. Done.

      Hope this helps.

  2. Thank you for your comment, can i put instead two mosfets, Pchanel instead of the Relays?
    as it is experimental, did this code tested even in proteus?


    • I don’t think MC can output the voltages required to directly drive p-mosfets specially as they are working attached to the upper voltage rail 14v. You may use a mosfet driving stage in between them like this

      I have not used proteus but the trial and error method explained above. A pic simulator alone cannot do this. You need an ICD. I’m not sure this pic supports in circuit debugging 😦

  3. Thank you sir for sharing this great helpfull tutorial. Sir i have a SMPS of 15V for charging 12V lead acid battery. Can you please guide me how to monitor whether my device is working on smps or battery. My purpose is to display that whether equipment is running on mains or battery and also monitoring low battery voltage.

    • Using a 15v smps is not good for charging a 12v battery. You should use a battery charger to avoid overcharging the battery. If the mains is down you should stop the battery feeding the smps. I.e. Current flow from battery to smps. This can be achieved by using a diode in series with smps. Then just connect a bulb to the smps output. If the bulb is glowing you are running on mains. To monitor low battery state you have to use a voltage comparator circuit.

      • Thanks sir for your quick reply. Sir I have already done my hardware portion for battery charging, I have used 1N5402 diode and a 12V/10W small bulb to limit current while charging. When battery is discharged lamp glows with maximum brightness and as the battery is charging its brightness keeps on decreasing and when it gets fully charged then lamps gets switched off. Sir I wanted to know that how can i implement this circuit with my micro-controller to detect whether the device is running on battery mode or charging mode.

      • First you have to sense SMPS output and battery output. Use two 100k presets as i have I have used in A0 pin. Then read their ADC values and decide at what level you should turn on indicators. Finding the correct ADC value is by trial and error. You should make sure that not more than 5v is given to micro controller pins.

  4. Wao…! Thanks a lot for sharing your knowledge. Please can this circuit modifies to charge 12V/ 200AH Deep cycle battery through power supply source (instead of solar panels) and also, the load would not be required. Jus only to charge battery and cut off when it is fully charged.

  5. :0A0000008316FF2390008301562BA6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s