Software requirements for MPPT controller V3.1
Draft by Keith Hungerford, updated 11 September 2015
Charging states
The charger operates in one of several charging states, depending on the sunlight level. 
Charger OFF state
At nil or low sunlight levels, as indicated by solar voltage below battery voltage + 0.5 Volts, the charger state is OFF.
Having transitioned into the OFF state, the charger must stay in this state for at least 1 second, irrespective of input voltage conditions. 
Transition from the OFF state occurs when 
a) if battery voltage is greater than 12.0 volts and solar voltage rises above battery voltage + 0.5 Volts; OR 
b) if battery voltage is less than 12.0 volts and solar voltage rises above 12.5 volts.
When either of these voltage conditions is met the charger goes into the Startup state.
Charger STARTUP state
The STARTUP state provides the algorithm to initialise the current flow in the converter and the control voltages for its MOSFETs. 
Immediately after completing the STARTUP steps the controller will enter one of the following states:
i) Float state if the battery voltage is equal to or greater than the Float voltage;
ii) Boost DCM state if the battery voltage is less than the Float voltage and the Boost time since the last night time is less than [1 hour];
iii) Bulk DCM state if the battery voltage is less than the Float voltage and the Boost time since the last night time is greater than [1 hour]

Boost DCM and Bulk DCM states
When in the Boost DCM or Bulk DCM state, the charger can go to OFF state if the solar voltage falls below (battery voltage + 0.5 Volts). 
The charger can go to Boost CCM or Bulk CCM state if the Solar Power rises above 10% of the rated power.
The charger can go to the Float state if Battery voltage rises above Float.
The charger can go from Boost DCM to Bulk DCM if the Boost time since the last night is greater than Boost duration.
Boost CCM and Bulk CCM states
When in the Boost CCM or Bulk CCM state the charger can go to Boost DCM or Bulk DCM if the Solar Power falls below 10% of rated power. 
The charger can go from Boost CCM to Bulk CCM if the Boost time since the last night is greater than Boost duration. 
Float state
The float state is functionally the same as Bulk DCM state when the battery voltage reaches Float.
When in Float state the charger keeps just sufficient current flowing to the battery to maintain the battery voltage at Float.
Charging algorithms
CCM algorithm
Continuous Current Mode (CCM) applies when the available solar power is more than 10% of the rated power (strictly, when the current in the inductor always flows towards the battery, but this is difficult to measure directly in the software so we use the power level as a proxy estimate). 
In this mode, the PWM period is set at 12 microseconds. The PWM duty cycle is set either at a value that achieves maximum power, or to meet the charging voltage required by the battery (see battery management conditions below). When it is required to reduce the charging rate so as to limit the battery voltage, the PWM duty is reduced, thereby increasing the panel voltage above the maximum power point and reducing the current accordingly. 
The Fast PWM mode of the Arduino is used to generate this PWM. The PWM period of 12 microseconds is 192 clock cycles at 16 MHz. The PWM period is divided into a Panel Connect phase followed by an Earth Connect phase. The length of the Panel Connect phase is controlled by PWM Duty. 
When maximising power, the power level is measured approximately every 1.0 milliseconds and is compared to the previous power level measurement. The PWM duty cycle is changed up or down by 1 CPU clock cycle after every measurement. After the first measurement, the PWM duty cycle is increased. On subsequent measurements, if the power level is increased or is the same as the previous measurement, the PWM duty cycle is changed in the same direction as in the previous cycle. If the power level is decreased from the previous cycle, the PWM duty cycle is changed to go in the opposite direction to that in the previous cycle. 
When tracking the allowable battery voltage, the battery voltage is measured approximately every 1.0 milliseconds and is compared to the target value. If the new value is within +0.1 volts of the target value, the PWM duty is left unchanged. If the new value is higher than the target value by more than 0.1 volts, the PWM duty is increased by one step. If the new value is lower than the target value by more than 0.1 volts, the PWM duty is decreased by one step. 
Whether maximising power or tracking battery voltage, the battery charging current must not be allowed to exceed 105% of the rated current. If the battery charging current voltage reaches 105% of the rated current, the charging rate is reduced to keep within  that limit. 
DCM algorithm
Discontinuous Current Mode (DCM) applies when the available solar power is less than 10% of the rated power.
In this mode, the PWM period is set at 100 microseconds which is 1600 clock cycles at 16 MHz. The PWM period is divided into three phases: a Panel Connect phase, followed by an Earth Connect phase, followed by a Null phase. The length of the Panel Connect phase is controlled by PWM Duty through setting a “1” on both IN and SD pins of the driver IC of Q2. The length of the Earth Connect phase is controlled by setting a “0” on IN and a “1” on SD pins of the driver IC for Q3. The Null phase is the remainder of the PWM period, during which time the SD pins of the drivers for Q2 and Q3 are both “0”, so that both transistors are OFF.
In this mode the length of the Earth Connect phase is set to be Panel Connect phase multiplied by the ratio of (Panel voltage - Battery voltage) to Panel voltage. Thus if the battery voltage is 13 and the panel voltage is 19.5 volts, the Earth Connect phase is (19.5-13)/19.5 = 1/3 of the Panel Connect phase. So if the Panel connect phase lasts 9 microseconds, the Earth connect phase will last 3 microseconds. In this case the Null phase will be (100 – 9 – 3) = 88 microseconds. 
When maximising power, the power level is measured approximately every 1.0 milliseconds and is compared to the previous power level measurement. The Panel Connect time is changed up or down by 1 CPU clock cycle after every measurement. The Earth Connect time is changed to follow the Panel Connect time. After the first measurement, the Panel connect time is increased. On subsequent measurements, if the power level is increased or is the same as the previous measurement, the Panel connect time is changed in the same direction as in the previous cycle. If the power level is decreased from the previous cycle, the Panel connect time is changed to go in the opposite direction to that in the previous cycle. Once the new Panel connect time is known, the corresponding Earth connect time is calculated. 
Since the Panel connect time and Earth connect time are defined in integer numbers of clock cycles, it will often not be possible to make the Earth connect time the exact multiple of the Panel connect time required for the ratio of (Panel voltage - Battery voltage) / Panel voltage. In this case the Earth connect time should be made the next lowest integer of clock cycles, below the exact value.
When tracking the allowable battery voltage, the battery voltage is measured approximately every 1.0 milliseconds and is compared to the target value. If the new value is within +0.1 volts of the target value, the Panel connect time is left unchanged. If the new value is higher than the target value by more than 0.1 volts, the Panel connect time is increased by one step. If the new value is lower than the target value by more than 0.1 volts, the Panel connect time is decreased by one step. Then the corresponding Earth connect time is determined as defined for CCM.

Float algorithm
The algorithm for tracking the float voltage is the same as defined for DCM.
STARTUP algorithm
This algorithm is to make sure the current is flowing in the right direction from the outset – ie from the panel towards the battery. This software is intimately related to the hardware. If the hardware is different, the software may need to be updated. 
It operates in 4 phases:
Phase 1: provide 3 pulses to the input circuit of IR2104-1 to give its IN and SD a logic 1. It does not (yet) turn on Q1 because there is no bias voltage on IR2104-1 Vb. The 3 pulses are of 3 microseconds duration with 3 microsecond spaces between them.
Phase 2: provide a single pulse to LO of IR2104-2, by setting IN to High for 17 microseconds and SD High for 19 microseconds. This gives a 2 microsecond pulse (less dead time) to LO, which turns out to be about 1 microsecond, which is the shortest time that can be guaranteed to actually work given the variations in the various delays. This gives a short pulse of reverse current through Q3, and (critically) starts the charge pump for the two IR2104s. 
Phase 3: provide synchronised PWM on D9 and D10 (IN and SD of IR2104-2) for 4 cycles, 10 microseconds High and 10 microseconds Low. During these 4 cycles, the charge pump for the two IR2104s gets to its maximum level, reliably. The controller is working in asynchronous mode, that is we never turn on Q3, we are just relying on its body diode or the parallel Schottky diode to provide the inductor current until it is finished. 
At the end of Phase 3, check that the current at the panel is flowing from the panel towards the controller. If not, pause for 100 microseconds then repeat the startup sequence starting in Phase 1, after also checking that the Panel voltage exceeds the Battery voltage by the specified margins to commence the Startup state. 
If the conditions for the Startup state are not met, go to the OFF state.
Phase 4: provide synchronised PWM on D9 and D10 (IN and SD of IR2104-2) continuously until modified by the next state. This PWM will be 10 microseconds High and 90 microseconds Low. 
Once this Phase 4 PWM has been started, transition to the following state (Float, Boost DCM or Bulk DCM) according to the voltage levels. 
Off algorithm
All MOSFETs (Q1, Q2 and Q3) are turned OFF in this state.
Battery management
Float charging
When battery voltage exceeds the Float value, the charger goes into a Float Charging state which provides just enough current to keep the battery within 0.1 volts of the Float value. 
Bulk charging
When sufficient solar power is available, the charger goes into a Bulk Charging state using the appropriate algorithms (DCM or CCM). In this state the battery voltage will be allowed to go up to Float voltage and the battery charging current must not be allowed to exceed 105% of the rated current. If the battery voltage reaches Float voltage – 0.1 volts, or the current reaches the rated current, the charging rate is reduced to stay below those limits. 
Boost charging
For a maximum of Boost Duration in each daily charging cycle, the battery voltage will be allowed to go up to Boost Voltage, provided the battery voltage has fallen below the Boost Reconnect voltage since the previous boost charging period. During this state, the battery charging current must not be allowed to exceed 105% of the rated current. If the battery voltage reaches Boost Voltage, or the charging current reaches the rated current, the charging rate is reduced to stay below those limits. 
Battery management parameters
The controller will use the following parameters to manage the battery charge and discharge:
Float voltage	13.8 volts
Boost voltage	14.2 volts
Boost reconnect voltage	13.2 volts
Low Voltage Disconnect	11.1 volts
Low Voltage Reconnect	12.6 volts
Boost duration	1 hour
The values given above are the default for 25 degrees Celsius. If a temperature sensor is installed, these voltages should all be adjusted by minus 0.018 volts per degree Celsius. For example, at 0 degrees C the Float voltage would become 13.8 + 25*0.018 = 14.25 volts.
All these values, including the temperature compensation factor, are to be adjustable via the serial interface. 
Load control states
This function controls the load MOSFET(Q4).
Via the serial monitor, the user can select either After Dark Lighting mode or Continuous mode.
Load control algorithms
Battery discharge protection
If the battery voltage falls below Low Voltage Disconnect voltage the load is disconnected.
Once the battery voltage rises to the Low Voltage Reconnect voltage the load is reconnected.
After dark Lighting
1. 	ON: Turn on loads at night (when the solar panel is producing less than 5% power) and the battery voltage is not disconnected due to the Low Voltage Disconnect logic.
 2.	OFF: Turn off loads in day time as well as due to the Low Voltage Disconnect logic.
Continuous
Turn on the load at all times subject to the Low Voltage Disconnect logic.

LED display
This function display the current state of charge of the battery via LED as follows:
 1.	YELLOW means overvoltage (over Float voltage)
 2.	RED means undervoltage (disconnected due to battery voltage falling below Low Voltage Disconnect, and still below Low Voltage Reconnect voltage)
 3.	GREEN is Normal, ie above LVD / LVR and at or below Float voltage. 

LCD display
This function is used to display the status of solar panel, battery and load of the controller on a 20X4 character I2C LCD display.

WiFi data logging
This function sends parameters (sol voltage, current, power & battery voltage, charging current etc) to the www.thingspeak.com via a ESP8266 wifi module.
First the wifi module will connect to the wifi router automatically (User has to set the SSID, Router Password and Thingspeak API key).
If the Wifi module connects successfully, it will transmit data to wifi @ 15sec intervals (this may be greatly reduced after development is complete, to maybe once every 15 minutes or even once per hour). 
Current Status : Working but wifi module has to be first connected to the router by using a separate program and using AT command from serial monitor.
Serial monitor
This function prints software parameters on a computer connected via the Arduino USB port useful for debugging. 
It also provides a command line interface for changing settable parameters in the controller. Settable parameters are:
Load control mode (After Dark or Continuous); 
Battery management values (see battery management section).
In basic operation the default mode is to output data to the serial monitor. The user can put in a command such as "stop" which will stop the stream of serial data and print a single line with a prompt such as "type LIST for a list of commands, type START to restart data logging". 
If the user types "LIST” or list" the serial monitor puts out a list of parameters and their current values. The user can then put in a command to change any value.
Hardware interfaces
The following pins will be used for interfacing to the various hardware devices.
// A0 - Voltage divider (solar) 150K / 20 K scale factor 0.04150390625
// A1 - ACS 712 Solar current scale factors for 5A unit: current 0.026393581 Offset 13.5135135
// A2 - Voltage divider (battery) 100K / 20 K scale factor 0.029296875
// A3 - ACS 712 Battery current scale factors for 5A unit: current 0.026393581 Offset 13.5135135
// A4 - LCD SDA
// A5 - LCD SCL
// A6 - ACS 712 Load  current scale factors for 5A unit: current 0.026393581 Offset 13.5135135
// A7 - not used
// D0, D1 - reserved, USB port
// D2 - ESP8266 Tx
// D3 - ESP8266 Rx through the voltage divider
// D4 - temperature from DS18B20 probe
// D5 - LCD back light control button
// D6 - Load Control 
// D7 - not used
// D8 - not used
// D9 – IR2104 MOSFET driver IN
// D10 – IR2104 MOSFET driver SD
// D11- Green LED
// D12- Yellow LED
// D13- Red LED