# How long do solar cells live? (maximum power point tracking)

In other posts, I’ve talked about developing the lifetester board and output from the prototypes that I’ve built. So far however, I haven’t given any detail on how maximum point tracking actually works and in this post, I want to unravel things a bit. For this first attempt, I’ve gone for a really simple hill-climbing algorithm which looks like this:

In summary, It does the following steps to update the drive voltage to maintain the MPP:

1. Scan the drive voltage and look for the maximum power point to be used as an initial guess (not shown).
2. Set the drive voltage (V) for this point, measure the current.
3. Set the drive voltage (V + dV) for the next point, measure the current.
4. If Power(next) > Power(this), set V -= dV else set V += dV.
5. Repeat step 2.

In software, the update (step) function looks like this:

```void IV_MpptUpdate(LifeTester_t *const lifeTester)
{
uint32_t tElapsed = millis() - lifeTester->timer;

if ((lifeTester->error != currentThreshold)
{
if ((tElapsed >= TRACK_DELAY_TIME)
&& tElapsed < (TRACK_DELAY_TIME + SETTLE_TIME))
{
//STAGE 1: SET INITIAL STATE OF DAC V0
DacSetOutput(lifeTester->IVData.v, lifeTester->channel.dac);
}
else if ((tElapsed >= (TRACK_DELAY_TIME + SETTLE_TIME))
&& (tElapsed < (TRACK_DELAY_TIME + SETTLE_TIME + SAMPLING_TIME)))
{
//STAGE 2: KEEP READING THE CURRENT AND SUMMING IT AFTER THE SETTLE TIME
}
else if ((tElapsed >= (TRACK_DELAY_TIME + SETTLE_TIME + SAMPLING_TIME))
&& (tElapsed < (TRACK_DELAY_TIME + 2 * SETTLE_TIME + SAMPLING_TIME)))
{
//STAGE 3: STOP SAMPLING. SET DAC TO V1
DacSetOutput((lifeTester->IVData.v + DV_MPPT), lifeTester->channel.dac);
}
else if ((tElapsed >= (TRACK_DELAY_TIME + 2 * SETTLE_TIME + SAMPLING_TIME))
&& (tElapsed < (TRACK_DELAY_TIME + 2 * SETTLE_TIME + 2 * SAMPLING_TIME)))
{
//STAGE 4: KEEP READING THE CURRENT AND SUMMING IT AFTER ANOTHER SETTLE TIME
}
//STAGE 5: MEASUREMENTS DONE. DO CALCULATIONS
else if (tElapsed >= (TRACK_DELAY_TIME + 2 * SETTLE_TIME + 2 * SAMPLING_TIME))
{
// Readings are summed together and then averaged.
lifeTester->IVData.pCurrent =
lifeTester->IVData.v * lifeTester->IVData.iCurrent;

lifeTester->IVData.pNext =
(lifeTester->IVData.v + DV_MPPT) * lifeTester->IVData.iNext;

// if power is lower here, we must be going downhill then move back one point for next loop
if (lifeTester->IVData.pNext > lifeTester->IVData.pCurrent)
{
lifeTester->IVData.v += DV_MPPT;
lifeTester->Led.stopAfter(2); //two flashes
}
else
{
lifeTester->IVData.v -= DV_MPPT;
lifeTester->Led.stopAfter(1); //one flash
}
// finished measurement now so do error detection
if (lifeTester->IVData.iCurrent < MIN_CURRENT)
{
lifeTester->error = lowCurrent;
}
else if (lifeTester->IVData.iCurrent >= MAX_CURRENT)
{
lifeTester->error = currentLimit;  //reached current limit
}
else //no error here so reset error counter and err_code to 0
{
lifeTester->error = ok;
}
PrintLifeTesterData(lifeTester);

lifeTester->IVData.iTransmit =
0.5 * (lifeTester->IVData.iCurrent + lifeTester->IVData.iNext);
lifeTester->timer = millis(); //reset timer
lifeTester->IVData.iCurrent = 0;
lifeTester->IVData.iNext = 0;
}
}
else //error condition - trigger LED
{
lifeTester->Led.t(500,500);
lifeTester->Led.keepFlashing();
}
}
```

This function operates on a custom lifetester type that contains all the relevant information regarding the state of the device under test. We pass a pointer to this data which the update function works on. It’s a psudo-object oriented approach. C is obviously not an object oriented language but by using a struct like an instance, this function is a bit like a method. This way, we can have another lifetester instance to represent another device under test or many more if we choose and they should not interact.

As illustrated inĀ this solution, it’s important not to block the microcontroller with calls to delay(). If you call this, the device won’t be able to update another channel say or the state of leds. I wrote this code almost a year ago and although it works, it’s not clean:

• The function is too long – it’s doing more than one thing.
• There are unnecessary comments. If the code were written well, it would be self-documenting.
• There is duplication: this point and next point share almost identical code.
• Spot the magic numbers.

I’ve now refactored this code by means of a state-machine and will present it in a coming article.