hi
i am trying to build an mppt charger using arduino nano and right now i am having a problem with my mppt.
i already test all the components and working fine. the current sensor is okay and also the voltage sensor is okay.
but when the mppt start pwm then the arduino keeps resetting every seconds.
i am trying to build an mppt charger using arduino nano and right now i am having a problem with my mppt.
i already test all the components and working fine. the current sensor is okay and also the voltage sensor is okay.
but when the mppt start pwm then the arduino keeps resetting every seconds.
Code:
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
int show = -1;
uint16_t PWM_MIN = 96;
uint16_t PWM_MAX = 150;
uint16_t PWM_DUTY = 120;
#define PWM_INC 1 //the value the increment to the pwm value for the ppt algorithm
#define AVG_NUM 20
#define SOL_AMPS_PIN A0
#define SOL_VOLTS_PIN A3
#define BAT_AMPS_PIN A1
#define BAT_VOLTS_PIN A2
#define SENSOR_SCALE 0.05 // 50mv per amper
double VOLTAGE_DIVIDER = 0.04912836767; //(r2/(r1+r2))
double VOLTAGE_REFERENCE = 4.096; //external Vref
double SOL_AMPS = 0.0;
double SOL_VOLTS = 0.0;
double BAT_AMPS = 0.0;
double BAT_VOLTS = 0.0;
double SOL_WATTS = 0.0;
double OLD_SOL_WATTS = 0.0;
double OLD_SOL_VOLT = 0.0;
double OFFSET_SOLAR = 0.0;
double OFFSET_BATTERY = 0.0;
void setup() {
// put your setup code here, to run once:
TCCR1A = 0xB2;
TCCR1B = 0x11;
ICR1 = 160;
DDRB = 0x06;
OCR1A = 0; // pin 9
OCR1B = 0; // pin 10
int error;
analogReference(EXTERNAL);
analogRead(A0);
analogRead(A1);
analogRead(A2);
analogRead(A3);
Serial.begin(115200);
Serial.println("Dose: check for LCD");
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
show = 0;
lcd.begin(16, 2); // initialize the lcd
} else {
Serial.println(": LCD not found.");
}
lcd.setBacklight(255);
OFFSET_SOLAR = read_adc(SOL_AMPS_PIN);
OFFSET_BATTERY = read_adc(BAT_VOLTS_PIN);
}
void loop() {
read_data();
if (SOL_WATTS > OLD_SOL_WATTS) {
if (SOL_VOLTS > OLD_SOL_VOLT)
{
PWM_DUTY += PWM_INC;
}
else
{
PWM_DUTY -= PWM_INC;
}
}
else {
if (SOL_VOLTS > OLD_SOL_VOLT)
{
PWM_DUTY -= PWM_INC;
}
else
{
PWM_DUTY += PWM_INC;
}
}
if (PWM_DUTY < PWM_MIN) {
PWM_DUTY = 96;
}
if (PWM_DUTY > PWM_MAX) {
PWM_DUTY = 150;
}
OLD_SOL_VOLT = SOL_VOLTS;
OLD_SOL_WATTS = SOL_WATTS; // load old_watts with current watts value for next time
OCR1A = PWM_DUTY - 10;
OCR1B = PWM_DUTY;
delay(100);
lcd.setCursor(0, 0);
lcd.print(SOL_WATTS);
lcd.setCursor(8, 0);
lcd.print(SOL_VOLTS);
lcd.setCursor(0, 1);
lcd.print(BAT_VOLTS);
lcd.setCursor(8, 1);
lcd.print(SOL_AMPS);
}
void read_data(void)
{
int temp_sol_amps = read_adc(SOL_AMPS_PIN);
int temp_sol_offset = temp_sol_amps - OFFSET_SOLAR;
if (temp_sol_offset < 0)
{
temp_sol_offset = 0;
}
int temp_bat_amps = read_adc(BAT_AMPS_PIN);
int temp_bat_offset = temp_bat_amps - OFFSET_BATTERY;
if (temp_bat_offset < 0)
{
temp_bat_offset = 0;
}
SOL_AMPS = (temp_sol_offset * (VOLTAGE_REFERENCE / 1023.0)) / 0.05; //input of solar amps
//SOL_AMPS = read_adc(SOL_AMPS_PIN); //input of solar amps
SOL_VOLTS = (read_adc(SOL_VOLTS_PIN) * (VOLTAGE_REFERENCE / 1023.0)) / VOLTAGE_DIVIDER; //input of solar volts
BAT_AMPS = (temp_bat_offset * (VOLTAGE_REFERENCE / 1023.0)) / 0.05; //output of battery amps
//BAT_AMPS = read_adc(BAT_AMPS_PIN); //output of battery amps
BAT_VOLTS = (read_adc(BAT_VOLTS_PIN) * (VOLTAGE_REFERENCE / 1023.0)) / VOLTAGE_DIVIDER; //calculations of solar watts
SOL_WATTS = SOL_AMPS * SOL_VOLTS;
}
int read_adc(int channel)
{
int sum = 0;
int temp;
int i;
for (i = 0; i < AVG_NUM; i++)
{ // loop through reading raw adc values AVG_NUM number of times
temp = analogRead(channel); // read the input pin
sum += temp; // store sum for averaging
delayMicroseconds(100); // pauses for 50 microseconds
}
return (sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
}