Skip to content

Instantly share code, notes, and snippets.

@skittleson
Created February 23, 2023 06:39
Show Gist options
  • Save skittleson/c1e43f9c88368e759f06f209eef3a9cb to your computer and use it in GitHub Desktop.
Save skittleson/c1e43f9c88368e759f06f209eef3a9cb to your computer and use it in GitHub Desktop.
Basic MPPT for JS
setInterval(function() {
// Define the parameters
var vpv = 18; // The solar panel's output voltage
var ipv = 2; // The solar panel's output current
var vbat = 14.8; // The battery's voltage (4-series lithium polymer battery)
var ibat = 0; // The battery's current
var vmax = 20; // The maximum power voltage of the solar panel
var imax = 2.5; // The maximum power current of the solar panel
var max_vbat = 16.8; // Maximum voltage of the 4-series lithium polymer battery
var min_vbat = 12.0; // Minimum voltage of the 4-series lithium polymer battery
var max_ibat = 5.0; // Maximum current of the 4-series lithium polymer battery
// Calculate the power output of the solar panel
var pout = vpv * ipv;
// Calculate the power input to the battery
var pin = vbat * ibat;
// Implement the MPPT algorithm with battery protection
if (vpv < vmax && ipv < imax && vbat < max_vbat) {
vpv = vpv + 0.1;
} else if (vpv > vmax || ipv > imax || vbat >= max_vbat) {
vpv = vpv - 0.1;
}
if (vbat <= min_vbat) {
ibat = 0;
} else if (vbat >= max_vbat || ibat >= max_ibat) {
ibat = ibat - 0.1;
} else {
ibat = ibat + 0.1;
}
// Calculate the new power output of the solar panel
pout = vpv * ipv;
// Calculate the new power input to the battery
pin = vbat * ibat;
// Print the results
console.log("Solar Panel Output Voltage: " + vpv + " V");
console.log("Solar Panel Output Current: " + ipv + " A");
console.log("Battery Voltage: " + vbat + " V");
console.log("Battery Current: " + ibat + " A");
console.log("Solar Panel Power Output: " + pout + " W");
console.log("Battery Power Input: " + pin + " W");
}, 1000); // Run the code every 1000 milliseconds (i.e., every second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment