Learn how to build a maximum power point tracking (MPPT) controller with an Arduino. This tutorial explains how MPPT works and gives you working code for the popular perturb and observe method, so you can get more energy from your solar panels.
⚠️Disclaimer:
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
What is MPPT?
An MPPT controller is a DC-DC converter that sits between a solar panel and a battery. It keeps the panel working at the voltage where it delivers the most power. At the same time, it converts that power to the lower voltage the battery needs. Because power is conserved, the battery receives more current than the panel supplies.

A solar panel does not deliver the same power at every voltage. At low voltages, its current is almost constant, so power rises as the voltage rises. However, close to the open-circuit voltage, the current drops quickly. Somewhere in between sits the maximum power point (MPP), usually at about 80 % of the open-circuit voltage.
The MPP also moves all the time. For example, it shifts with sunlight, temperature and shading. If you connect a panel directly to a battery, the battery fixes the panel voltage, which is usually well below the MPP. As a result, you lose a lot of the available power.
An MPPT controller solves this by searching for the MPP continuously and adjusting its converter to stay there. You can read more about MPPT here.
- Solar system size calculator – size solar panels, battery and charge controller
- Voltage divider & ADC scaling calculator – scale a voltage into your MCU ADC range
- Buck converter calculator – duty cycle, inductor and capacitor values
Making an MPPT controller
To build an MPPT controller, you need a DC-DC converter that a microcontroller can control, usually a buck converter. You also need to measure the panel voltage and current, and the battery voltage. I have written a complete guide to building one, which you can read here.
First, here is the core of the perturb and observe (P&O) algorithm in a few lines:
Arduino Example code to find MPPT
// Perturb and Observe (P&O): the idea in a few lines.
// Every step, measure the panel power and compare it with the previous step.
// If the power went up, keep moving the duty cycle the same way.
// If the power went down, reverse the direction.
const int DUTY_STEP = 2;
float lastPower = 0;
int direction = +1; // +1 = raise the duty cycle, -1 = lower it
int mpptStep(float vPanel, float iPanel, int duty)
{
float power = vPanel * iPanel; // measured panel power in watts
if (power < lastPower) {
direction = -direction; // we went past the peak: turn around
}
lastPower = power;
return duty + direction * DUTY_STEP;
}
The idea is simple. The controller changes the duty cycle a little, then measures the new panel power. If the power went up, it keeps moving in the same direction. If the power went down, it has passed the peak, so it turns around. In this way, it climbs to the MPP and then oscillates closely around it.
Next, here is a complete sketch for an Arduino UNO that drives a synchronous buck converter. It measures the panel voltage and current, runs P&O and stops charging when the battery is full:
MPPT Controller code for Arduino
// MPPT charge controller with an Arduino UNO and a synchronous buck converter
// Pin 9 (OC1A) -> PWM input of the MOSFET gate driver (e.g. IR2104), 62.5 kHz
// A0 <- panel voltage through a 100k/10k divider
// A1 <- panel current from an ACS712-5A sensor
// A2 <- battery voltage through a 100k/10k divider
const float VREF = 5.0; // ADC reference, volts
const float DIV_RATIO = 11.0; // (100k + 10k) / 10k
const float ACS_ZERO = 2.5; // ACS712 output at 0 A, volts (calibrate)
const float ACS_V_PER_A = 0.185; // ACS712-5A sensitivity, volts per amp
const float BATT_FULL_V = 14.4; // 12 V lead-acid absorption voltage
const int DUTY_MIN = 20; // out of 255
const int DUTY_MAX = 240; // leave off-time for the bootstrap capacitor
const int DUTY_STEP = 2;
int duty = 128;
float lastPower = 0;
int direction = +1;
// Average 16 ADC samples to reduce noise, return volts at the pin
float readPin(int pin)
{
long sum = 0;
for (int i = 0; i < 16; i++) sum += analogRead(pin);
return (sum / 16.0) * VREF / 1023.0;
}
void setup()
{
Serial.begin(115200);
pinMode(9, OUTPUT);
// Timer1: 8-bit fast PWM (mode 5), no prescaler -> 16 MHz / 256 = 62.5 kHz
TCCR1A = _BV(COM1A1) | _BV(WGM10);
TCCR1B = _BV(WGM12) | _BV(CS10);
OCR1A = duty;
}
void loop()
{
float vPanel = readPin(A0) * DIV_RATIO;
float iPanel = (readPin(A1) - ACS_ZERO) / ACS_V_PER_A;
float vBatt = readPin(A2) * DIV_RATIO;
float power = vPanel * iPanel;
if (vBatt >= BATT_FULL_V) {
duty -= DUTY_STEP; // battery full: reduce charging
} else {
if (power < lastPower) direction = -direction; // P&O: passed the peak
duty += direction * DUTY_STEP;
}
lastPower = power;
duty = constrain(duty, DUTY_MIN, DUTY_MAX);
OCR1A = duty;
Serial.print("Vpv: "); Serial.print(vPanel);
Serial.print(" Ipv: "); Serial.print(iPanel);
Serial.print(" Ppv: "); Serial.print(power);
Serial.print(" Vbat: "); Serial.print(vBatt);
Serial.print(" Duty: "); Serial.println((float)duty);
delay(50); // about 20 tracking steps per second
}
The sketch sets Timer1 to produce a 62.5 kHz PWM signal on pin 9. This is important, because the default analogWrite() frequency of about 490 Hz is far too low for a buck converter. Every 50 ms, the code measures the panel power and moves the duty cycle one step. Also, when the battery reaches 14.4 V, it reduces the duty cycle instead, so the battery is not over-charged. Before you run it, calibrate the divider ratios and the ACS712 zero point with a multimeter.
Conclusion
Perturb and observe is the most common MPPT method, but there are others. For example, incremental conductance reacts better to fast changes in sunlight, and fuzzy-logic or model-based methods can track faster still.
Never connect a solar panel directly to the Arduino analog pins. Instead, use resistor dividers to bring the voltages below 5 V, and a current sensor or shunt amplifier for the current.
Also, a real charger needs more than MPPT. It must follow the battery charging profile (bulk, absorption and float), protect against reverse polarity and over-current, and handle temperature. For lithium batteries, it must respect the BMS limits.
In summary, the code above is a solid starting point for an Arduino MPPT controller. Test it with a current-limited supply first, then tune the step size and timing for your panel and converter.
Read more on:
- Make an MPPT Solar charge Controller with Synchronous Buck Converter
- Solar Charge Controller circuit & working principle of ON/OFF charge controller
- PWM Solar Charge Controller with PIC12F675
- Make a simple Data-Logger with Arduino UNO for your solar power plant
- Designing a BMS with STM32
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
Have a project in mind? LabProjectsBD designs power electronics, embedded firmware, IoT devices and production-ready PCBs for clients. Message us on WhatsApp, use the contact form, or see our services and portfolio.
2 Comments
Djalltra · 31/03/2024 at 6:41 pm
Can you please provide the same algorithm for pure sine wave inverter with change over and battery charging
MKDas · 31/03/2024 at 7:37 pm
???