Intellgent Solar Charge Controller using ARM Cortex M3 board

AbstractDue to the energy crisis in Pakistan, the demand for alternative energy sources has increased. The Battery is a component of primary importance in these applications. This paper presents the design of PWM based Solar charge controller that prevents battery from damage. It control flow of charges to battery and load such that prevents battery from over charging, under charging and overvoltage meanwhile providing power to the load when required. It also prevents the system from abnormal conditions i.e. over current. It uses PWM to control the flow of power. It also displays state of battery on LCD and two LEDs are used to indicate the position of the system. Furthermore, a data acquisition system is also built to view data on a computer.
 
Keywords—Charge Controller; Cortex M3; Microcontroller; Battery;
Introduction
Due to the energy crisis in Pakistan, use of alternate energy sources like solar has increased to an extent. The battery in one of the main component used in these systems to store energy for later use. Every battery has a certain capacity to store the electricity. Also over charging, over voltage , over current and other conditions can reduce the lifetime of the battery and even cause damage. As a battery is one of the most expensive components of the system so its protection is important. So in this paper, we present a charge controller that regulates the voltage of battery and source and prevents it from unwanted conditions. A charge controller is a device that regulates the condition of a battery and prevents it from damage.













Figure 1: A typical stand alone solar system with charge controller[1]

It prevents overcharging and may protect against overvoltage, which can reduce battery performance or lifespan, and may pose a safety risk. It may also prevent completely draining ("deep discharging") a battery, or perform controlled discharges, depending on the battery technology, to protect battery life[2].
The battery storage in a PV system should be properly controlled to avoid catastrophic operating conditions like overcharging or frequent deep discharging. Storage batteries account for most PV system failures and contribute significantly to both the initial and the eventual replacement costs. Charge controllers regulate the charge transfer and prevent the battery from being excessively charged and discharged.[3]
The output of the solar panel is firstly checked for over voltage and over current condition for protection purpose. The voltage is then given to microcontroller.  The controller keeps the battery fully charged without over-charging it. When the load is drawing power, the controller allows the charge to flow from the generation source into the battery, the load, or both using PWM and switch. When the controller senses that the battery is fully (or nearly fully) charged, it reduces or stops the flow of electricity from the generation source to the battery. It allows the load to connect with battery only when a source is not generating enough energy. It automatically disconnects the system once a battery is in the deep discharge position.  An LCD and two LEDs are used to display the condition of the system as shown in Figure 2.





Figure 2: Block Diagram of the Intelligent solar charge controller

Protection is provided with Zener diode and fuse. A diode was used to prevent reverse current. The load is on off using switch and battery is charged with PWM.

     I.     System Design

A.    Protection

First of all input of solar is passed through a protection block which consists of a Zener, a diode, and a capacitor. Rating for the fuse is 3A so it will protect for over current. All the components used in the further circuit is of rating above 3A. A Zener is used in reverse to protect the circuit from over voltage and its rating is 15V. So any voltage above 15V will cause the system to shutdown. A diode is used to block reverse current and capacitor is used to protect the system from ripples that can cause damage to battery and microcontroller as well.
















Figure 3: Protection Circuit for Charge controller

B.    Voltage divider for Sampling of Voltage

Voltages are sampled by controller in two places of circuit for making decision. First it sampled input from solar panel, second it was sampled from battery. TM4C123 can bare <5V and it's a good practice to fed voltages less than 3.3V so we designed a voltage divider with 10k and 4.7k ohm resistance which is placed near battery and solar panel in parallel fashion.























Figure 4: Voltage divider for sampling

In this voltages for controller will be
.  
  

                          (1)



As our R1=4.7K,  R2=10K so our voltage dividing ratio is 0.3197. So maximum voltage that can appear in MCU analog pin is
Vout= 8.2*0 .3197
        =2.62154V
And in cause of some fault in our protection system, our MCU will be safe for voltage upto15.6396 V.
Power used by this circuit is very small due to high resistance.

 P  =  V2/R                       (2)
 P  =  8.22/14.7k  =  0.00457W which is negligible.

C.  Switching

There are two spots in the circuit where switching is used, one is battery and second is load. Battery and load are in series so battery is charged by PWM so that power can be provided to load by battery when solar power is not available. MOSFET are used for switching. N channel MOSFET is used for switching of load namely IRF540  and P-Channel MOSFET is used for  battery namely IRF5430.
Load is simply switched on or off while battery needs PWM based charging. Duty cycle of is controlled by Microcontroller based on charging state of battery.




















Figure 5: Switching Circuit for battery

MOSFET driver is used here with Mosfet reason to used this is MOSFETs have a large stray capacitance between the gate and the other terminals, which must be charged or discharged each time the MOSFET is switched on or off. As a transistor requires a particular gate voltage in order to switch on, the gate capacitor must be charged to at least the required gate voltage for the transistor to be switched on. Similarly, to switch the transistor off, this charge must be dissipated, i.e. the gate capacitor must be discharged.

   II.    Circuit Design

The complete circuit is first simulated on proteus. As proteus don't have TM4C123 so instead another controller is used for the simulation purposes. Solar panel was also simulated as battery for simulation purpose only. Simulation
Input of solar panel is first passed through some protection block which checks for over voltage, over current and reverse current flow as discussed earlier.  Then this is sampled with a voltage divider connected in parallel. This is for voltage of solar panel. This voltage value is used in software to make some decisions that will be discussed in software design. Then there is switching driver for battery charging. This uses a mosfet and transistor. PWM is generated on the terminals of the transistor which then charges battery according to duty cycle of the PWM. Battery voltage is also sampled to see the present condition of the battery. Then load is connected in series with battery. Another driver is used to switch on or off the load. Theses decisions are made on based of software. Two Leds are used to indicate system condition. When Red is on means battery is fully discharged (<10%). Green led blinking means charging of battery and green led on means battery is fully charged. LCD shows percentage charging of the battery and voltages of panel and battery. Serial communication is also used to communicate with computer if needed.
Figure 6: Complete Circuit of charge controller.

    III.   Software Design

This is heart of the project where all the logic is applied. Voltage is sampled from solar panel and battery and based on these two inputs, all the logic is applied. Main work of software is to decide charging of battery, on and off situation of load. Display, leds and serial communication is also done by this software.

A.    Battery and Solar Panle Voltages

Battery and solar voltages are two inputs of the system that decides situation of the system. For this system following logic was used

a)       if Solar Voltage > Battery Voltage and also battery Voltage is less than 6.96 then it means battery is not chareged so battery need to be charged at 95% PWMb)       if Solar Voltage > Battery Voltage and also battery Voltage is between 6.96 and 7.2 then it means battery is > 80% chareged so battery need to be charged at 10 % PWM
c)       if Battery Voltage is >= 7.2 or Solar Voltage is less than Battery voltage it means battery can't be charged as >7.2 Voltage means battery is fully charged and Solar voltage is less than battery voltage means it cannot be charged by panel.
d)       if Battery voltage is less than 6 it means battery is dead. Red light will be on and Display and serial communication will show alarming situation.
e)       If Solar Voltage is less than 3 and battery Voltage is 6.2 which means battery is charged and solar light is not enough to produce power, so load will switched to battery.
f)        Battery voltage between 6 and 6.2 shows that battery is fully discharged.

B.    ADC Sampling

TM4C123 have two ADC modules with 12 bit resolution. In this application we used software triggered conversion with busy wait synchronization. As speed of conversion doesn't matter much in this application so we used lowest sampling rate of 125k samples/second. Output of ADC is in bits which is then converted to voltage level using following formula.

V Measure = (ADC reading/ Resolution of ADC)*V System     (3)

C.    PWM generation

There are three modes related to PWM.
If battery is fully charged than duty cycle will be 0%. If battery is 80% charged than PWM duty cycle will be 10%  and if battery is less than 80% charged  than PWM duty cycle is 80%.
TM4C123 has 2 specific pwm modules with 16 bit counters.
 Figure 8: 10% and 90% PWM duty cycle

    

When timer is initially start with count value of zero, immediately loaded with reload value which is proportional to  frequency of PWM. Reloading also sets output flip flop to be low as in figure 8. Now counter starts decrementing with each cycle and current value is continuously compared against duty cycle value. Initially counter value is higher than duty cycle comparator. When current value equals to duty cycle comparator value, flip flop value goes high and it resets. Count continue decrementing un till it reaches  0 and underflow occurs which causes reload so PWM cycle ends and new cycle start again. [4]
Figure 8: PWM generation in TM4C123

Two parameters are used here for PWM generation. We are using 100Hz PWM with duty cycle of 90% and 10%. Re laod value determines frequency of PWM with following relationship
V Reload = f clock/f PWM                               (4)
V reload will be 160,000, as counter is 16 bit which can have maximum value of 65,356 so pre-sclaer needed  to be used here. A pre-scaler of 16 is used so new reload value of 10,000 is used.
For duty cycle, value is subtracted from reload value. So for 90% duty cycle, duty cycle comparator will have value of 1, 000 and for 10% it will be 9,000.
PWM charge controller will increase charging efficiency, allow for rapid recharging, and maintain healthy battery life. In all, a PWM charge controller comes with the following advantages Battery Aging Adjustments,  Battery Gassing and Heating Reductions, Charge Acceptance Increase,High Battery Capacity Maintenance: Lost Battery Recovery, Self-Regulation with Drops in Voltage or Temperature.[5]

 D. Algorithm


IV.   Hardware

PCB of the circuit is made using Proteus. Circuit is designed in two different modules, one is main controlling circuit and second is LCD for display.

Figure 10: PCB of the main circuit.               

V.    Data Acquisition

Data is send using serial communication with a baud rate of 9600. A application using Labview is made to get and display this data.
Current status of the system is send using 3 values battery voltage , Solar Voltage and % charging of battery and end using e\n like 07,08,9,e\n. This is then separated using string function of Labview and converted into float value which is then displayed. In this application we use usb cable for data communication. For practical purposes, BLE or another appropriate device

VI.   Conclusion

A small voltage, low cost charge controller construction is described using ARM Cortex microcontroller. PWM is used as it is cheap to build and much better than traditional  on-off charge controller as in perspective of battery life and efficient use of energy. A data acquisition system is also built to view the data on computer.  This type of small charge controller can be used in small applications to increases battery life and efficient use of energy.

VII.  References


[1]     Power Electronics Circuits Devices and Applications, 3rd ed., Elsevier B.V.,USA,2010,pp728
[2]     Webarchive backup: Brown, David. "Technical Article: Battery Charging Options for Portable Products." (Commercial website). Analogic Tech, 2006-07-01. Retrieved on 2007-08-21.
[3]     Power Electronics Circuits Devices and Applications, 3rd ed., Elsevier B.V.,USA,2010,pp727
[4]     ARM Microprocessors Systems, Cortex-M Archtecture Programming and Interface,Dr. M. Tahir,Dr. kashif Javed,1st ed,UET Lahore,2015,ch 9
[5]     Blue light, 'Overview of PWM Charge Controller',2010, [online] Availabl:http://e-bluelight.com/article-345-Pulse+Width+Modulation +Charge+Controllers+Explained.html



Automatic Railway Crossing


Abstract: This project implements idea of automatic railway gate. A microprocessor is used to implement logic. A sensor is used to detect arrival of a train. In normal conditions, Traffic light is green, Seven Segment is showing 0. Motor is at stationary position. When a train is approaching, it will turn on green light, motor will rotate to off the gate and seven segment will start showing count down.
Motivation:
Railway crossing are all over the Pakistan and most of them are manual They causes a lot of trouble and accidents have become common. Also govt. have to spend a lot of money on servants too. Also there are a lot of railway crossing without gates. The purpose of this project is to present a way to make the automatic to overcome these problems.

High Level Design

Background:

This is a open loop system where input is being fetched to the system with sensors and then accordingly motor is being derived and output is being shown.
 
                                

A pressure sensor will be implemented on both side of railway gate at a appropriate distance that will give a signal to microcontroller that will command a motor controller ic to rum a motor and also a red traffic signal to on. When train will reach other side of the gate, another sensor will input microcontroller to open the gate and change the light to green.

Figure 1
Logical Construction:

Hardware/Software  Design   

1) Motor Driver
This is half bridge motor driver IC that controls DC motor direction with 2 inputs and that can rotate motor clock and anti clock wise. Below is a picture of half bridge. This allows to apply voltage in either direction to move motor forward or backward direction.

Figure 2
We uses LM293D motor driver IC which has this circuit embedded in it. It can control 2 motors and can provide a current of 600 mA. Pin configuration are as follows.
Figure 3
This IC can handle 4.5 to 36V motors, but we need only 9V  for our motor. For input -0.3 to 1.5V are considered as low and  2.5 to Vcc1 is considered as high.
L293D Circuit:
    The circuit shown to the right is the most basic implementation of L293D IC.



Driver using Arduino:
Simulation:
Flow Chart:

Code:


/*
Author: Muhammad Awais
Program Name: Motor Driver
Descrption: It is minimal Motro driver program that rotates motor in clock and anti clock wise.
*/
#define MotorInput1 2
#define MotorInput2 3
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pins as an output.
  pinMode(MotorInput1, OUTPUT);
  pinMode(MotorInput2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
  MotorDriver(1);
  delay(1000);              // wait for a second
  MotorDriver(0);
  delay(1000);
}
/*
Descrption: This function is used to drive motor clock or anticlok wise as needed. It will be used with
L293D IC. 
Input: bool, 1 for clockwise and 0 for anticlock wise
Output: It makes pins relative IC pins 1 and zero accordingly
*/
void MotorDriver(bool Direction)
{
  if (Direction==1)
  { 
digitalWrite(MotorInput1, 1);   // turn the LED on (HIGH is the voltage level)
digitalWrite(MotorInput2, 0);    // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(MotorInput1, 0);   // turn the LED on (HIGH is the voltage level)
digitalWrite(MotorInput2, 0);    // turn the LED off by making the voltage LOW
  }
  else if(Direction==0)
{
digitalWrite(MotorInput1, 0);   // turn the LED on (HIGH is the voltage level)
digitalWrite(MotorInput2, 1);    // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(MotorInput1, 0);   // turn the LED on (HIGH is the voltage level)
digitalWrite(MotorInput2, 0);    // turn the LED off by making the voltage LOW
  }
}

IR Sensors:
Infrared Sensors are used to detect arrival of train. Infrared sensor comprises of a transmitter and  a receiver. Transmitter is a LED that transmits infrared light at given frequency and receiver receives it. It is used for communication by PWM but as we only need to detect train arrival so we are using it to send 1 for not arrival and 0 for arrival. So transmitter will continue sending light to receiver but when train will arrive, it will send 0. So we can detect arrival of train.


In this sensor circuit, we will use a Op-Amp. This will be used to make analog input to digital output so that we can see arrival of train as 0 and not arrival as 1. Otherwise this sensor produces analog values based on incoming waveform. We will be using LM324 IC which contains four op amps but we are here only one is used. Following is IC information.





We used  Op-Amp in comparator mod. The output of the comparator is high if voltage at positive end is more than voltage at negative end and gives low output if voltage at positive end is lesser than negative end.


Sensor Simulation:
As IR sensor is not available in simulation so we are simulating it with a switch. When switch will be on, it will be simulating as train arrival and Open will be simulating departure form the location. Then amplifier is converting these values to zero and one accordingly.

This is hardware design of the sensors. Two sensor with 12cm distance are installed. 12cm is for train passing.

Display:
As soon as sensor will detect train arrival, the red light will be on showing gate closure and Seven Segment will be showing S. When train will be passed, seven segment will show O for open and light will turn into green. Also our controller will communicate with central train system and will give them present situation of the gate.
A seven segment is used to show traffic remaining time. A seven segment is simply 7 led containing box which can show numbers from 0 to 9.

To program it, we should know what led to on and what to off. So here is a list where we can see how to led on or off corresponding to each number.

Flow Chart:

This function is used to turn light on or off accordingly.
For Count down timer. This function shows reaming time on seven segment

This function is used to communicate with external UART device.






Code:
/*
Descrption: This function have two things to do.On red  with off green or, off red with on Gree.
If input is 1,RED will be on and Green will be off
If input is 0,Green will be on and Red will be off
Input: State, 1 or ) only
Output: Makes Light on and off accordingly
*/
void trafficLight(int state)
{
  if (state==1)
{
digitalWrite(Red,1);
digitalWrite(Green,0);
}
else if (state==0)
{
digitalWrite(Red,0);
digitalWrite(Green,1);
}
}
/*
Descrption: It serially communicates with any device attached and tell the train situation.
Input: Train state
Output: Serially communicates state of train.
*/
void Communication(int state)
{
 if (state==1)
{
Serial.println("Train is approaching at Railway Crossing Number: 25");
}
else if (state==0)
{
Serial.println("NO Train at Railway Crossing Nubmer: 25");
}
}
/*
Descrption: It is for Seven segment display.Starts form 9, it deceremtns counter each second.
As soon as it is called, it will start counting down.
Input: No input
Output: 
*/
void CountDownTimer()
{
  SevenSegOff();
 // write '9'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 0);
 digitalWrite(E, 0);
 digitalWrite(F, 1);
 digitalWrite(G, 1);
 delay(1000);
 // write '8'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 1);
 digitalWrite(E, 1);
 digitalWrite(F, 1);
 digitalWrite(G, 1);
 delay(1000);
 // write '7'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 0);
 digitalWrite(E, 0);
 digitalWrite(F, 0);
 digitalWrite(G, 0);
 delay(1000);
 // write '6'
 digitalWrite(A, 1);
 digitalWrite(B, 0);
 digitalWrite(C, 1);
 digitalWrite(D, 1);
 digitalWrite(E, 1);
 digitalWrite(F, 1);
 digitalWrite(G, 1);
 delay(1000);
 // write '5'
 digitalWrite(A, 1);
 digitalWrite(B, 0);
 digitalWrite(C, 1);
 digitalWrite(D, 1);
 digitalWrite(E, 0);
 digitalWrite(F, 1);
 digitalWrite(G, 1);
 delay(1000);
 // write '4'
 digitalWrite(A, 0);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 0);
 digitalWrite(E, 0);
 digitalWrite(F, 1);
 digitalWrite(G, 1);
 delay(1000);
 // write '3'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 1);
 digitalWrite(E, 0);
 digitalWrite(F, 0);
 digitalWrite(G, 1);
 delay(1000);
 // write '2'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 0);
 digitalWrite(D, 1);
 digitalWrite(E, 1);
 digitalWrite(F, 0);
 digitalWrite(G, 1);
 delay(1000);
 // write '1'
 digitalWrite(A, 0);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 0);
 digitalWrite(E, 0);
 digitalWrite(F, 0);
 digitalWrite(G, 0);
 delay(1000);
 // write '0'
 digitalWrite(A, 1);
 digitalWrite(B, 1);
 digitalWrite(C, 1);
 digitalWrite(D, 1);
 digitalWrite(E, 1);
 digitalWrite(F, 1);
 digitalWrite(G, 0);
  
}
void SevenSeg0() {  digitalWrite(A, 1);  digitalWrite(B, 1);  digitalWrite(C, 1);  digitalWrite(D, 1);  digitalWrite(E, 1);  digitalWrite(F, 1);  digitalWrite(G, 0); }

/*
Descrption: To off Seven Segment
*/
void SevenSegOff()
{
digitalWrite(A, 0);
 digitalWrite(B, 0);
 digitalWrite(C, 0);
 digitalWrite(D, 0);
 digitalWrite(E, 0);
 digitalWrite(F, 0);
 digitalWrite(G, 0);
}

Simulation:
When no Train is arriving


Serial Communication:


When Train is Arriving:



Serial Communication:

Complete System

Hardware:

Simulated Hardware:
This is complete circuit design on simulation.







1) When Train is approaching.

2) When NO train




Software: 

Following flow chart describes the whole software.



Code:

#define MotorInput1 3
#define MotorInput2 4
int Sensor = 2;
int Red=A0;
int Green=A1;
int A=12;
int B=11;
int C=10;
int D=9;
int E=8;
int F=7;
int G=6;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
//Initilize pins for sensor, traffic lights and seven Segment
  pinMode(Sensor, INPUT);
  pinMode(Red,OUTPUT);
  pinMode(Green,OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F,OUTPUT);
  pinMode(G, OUTPUT);
  //For motor
  pinMode(MotorInput1, OUTPUT);
  pinMode(MotorInput2, OUTPUT);
  
  
}
// Main program
void loop() {
  // read the Sensor
  int Train = digitalRead(Sensor);
  delay(1);        // delay in between reads for stability
  
  //When train is approaching 
  if(Train==1)
  {
   trafFcLight(Train);
   MotorDriver(1);
   Communication(Train);
   CountDownTimer();
   MotorDriver(0);
 
  }
  
  //When No train
  else if(Train==0)
  {
   trafFcLight(Train);
    SevenSeg0();
   Communication(Train);
   }
}






Open Source:

For all the codes:

For all the circuits Simulations: