A naive solar panel maximum power point control circuit

I am fooling around with a solar panel, just to try stuff and learn in the process (not trying to produce something I would actually use).
Its placement is very far from optimal - vertically behind a window pointing south-west. In the late afternoons it gets somewhat close to optimal conditions - sun light coming from a decent angle. The rest of the time it works off ambient light more or less.
Under these circumstances, it produces around 18-20V open circuit. But when I connect a load (let's say 60mA) it quickly drops to 5V (these numbers vary a lot depending on the conditions).
This is all normal and to be expected, but I was wondering if there is an easy way to squeeze more power from it. So I read about solar charge controllers, MPPT, etc. I could just buy one and be done, but my goal is not to actually use the panel for anything (I just bought it because it was relatively cheap and sounded like a fun gadget to play with). So, I am trying to see what I can do myself with parts lying around.
So far I have come up with this:

simulate this circuit – Schematic created using CircuitLab

The Load is an adjustable step down buck converter, with a beefy 1000uF cap across its output, which is then powering an arduino pro micro through an INA219 voltage and current monitor breakout and has an LCD attached to display voltage and current.
The arduino and LCD consume about 60mA at 5V. I can also attach additional load, like a LiPo battery charger and its current consumption will also be measured.
The regulator is pretty bad for this application (it consumes 10-20mA itself), but it is the only one I got which will work with these input voltages.

Now, to the point: The whole idea of this circuit is to keep the solar panel voltage near its maximum power voltage. When the panel voltage drops (due to insufficient current) bellow some threshold (which I can adjust with the pot) the comparator will output low and cut the ground of the load through M1. This will result in the panel voltage increasing (due to no load) and the comparator outputs high again, allowing current to flow through the load. Then the voltage would drop again etc. etc.
The result is rapidly switching the load on and off, but keeping Vcc near its optimal value, which should result in increased power output.
If the panel produces enough power, Vcc will not drop bellow threshold voltage and the whole circuit will behave more or less like if the load was directly connected to the panel.

It sort of works - I was able to power the arduino through it at noon, while connecting the load directly to the panel would not. So there is at least some minimal gain.
But then I noticed that M1 got a bit warm to the touch. In my (very little) experience this indicates that the NMOS is not always fully open or fully closed. Which is strange, because its gate is fed from a comparator, so it should be always either very close to GND or the zener voltage (which I measured around 5.4V). Looking at the data sheet of IRF510, it does seem completely possible that it is not fully on at 5.4V.
But then I decided to check the gate voltage with an oscilloscope. Note that it is not a real professional oscilloscope, it is something I made myself (an arduino measuring analog voltages as fast as it can and sending the data over serial to the PC). So it is slow, inaccurate, but still fairly useful in many cases.
Here is what gate voltage looks like:

And here is Vcc (panel voltage):

So it does the job somewhat, but it seems M1 is constantly driven in the "intermediate" region (never fully on or fully off). Which means Rds is not as low as it should be and there are power losses as result. The heat is not a problem by itself (it doesn't get really hot, just a bit warm to the touch), but losing power like this in this application is a little embarassing :)
Any idea why the gate voltage is not sharp rectangular wave from ~0V to ~5.4V?
Can it be the switching speed? According to my "wooden" scope, it is around 14.6Kz, which should be no problem for IRF510. Or am I wrong?
Or is the comparator being a bad boy and not outputting sharp LOW and HIGH values, but something inbetween?
Or is it something else?
Oh and while writing this I just realized I should move the pullup on the comparator output from the zener voltage to Vcc, so the HIGH output is higher voltage. I guess it will improve things, but not solve the problem with "intermediate" voltage levels at the gate. I just have to be careful, as Vcc can go above 20V, which is the maximum gate-source voltage specified for IRF510.

EDIT: Due to lack of answers and my failure at understanding what is going on I reworked the circuit, so instead of the LM2903P comparator I am now using an ATTiny85 - same form factor (DIP8) and for the moment doing the same thing ... almost. I added hysteresis and ditched the potentiometer (replaced by a fixed voltage divider), because I can now control the threshold voltages in software.
Now, things look like they should:

Green (channel 1) is panel voltage and red (channel 2) is MOSFET gate voltage.
I am now powering the ATTiny from a voltage regulator (the same one that used to be at the front of the load), the MOSFET is now BS170 instead of IRF510 (because of the lower gate threshold).
Power consumption went up a little - the ATTiny is sampling the ADC constantly, no time to sleep it, which consumes ~10mA, compared to the less than 1mA power consumption of the comparator IC, but it is not that much of a big deal.
This does shift the problem to software (which is my comfort zone) and opens up a lot of possibilities, but completely misses the point of the whole exercise - to learn more about electronics.
So, I would still love to know - what was wrong with the original circuit using a comparator IC, and why it didn't produce the nice square waves, like the ones I get with the microcontroller.

• I will try to look at this in greater depth later. But two things that may help are 1) Adding a large capacitor to the solar panel side, and 2) reconfiguring your comparator to have some hysteresis when it switches. Commented May 1, 2016 at 16:08
• I was waiting for you to show up :) I thought about capacitors too, but unfortunately all I have are rated 16V, so I am afraid to put them across the panel output - it often stays at 17-18V. I guess the goal of this capacitor would be to slow down / smooth the voltage fluctuations of the panel? Which would result in slower switching speed? About hysteresis on the comparator - it is just a DIP8 chip with vcc, gnd, 2 x 2 inputs and 2 outputs - no apparent way to add hysteresis. But I think I've seen a schematic which achieves that somewhere (will try to look it up). Commented May 1, 2016 at 16:18
• What is the purpose of R3? Generally you don't want negative feedback on a comparator. Try getting rid of that first. See how things behave. Then, if you want to add hysteresis, you can try adding 100k from comparator output to non-inverting terminal of the comparator. I know in op-amp circuits positive feedback is often portrayed as a bad thing, but it is often used in switching circuits to add hysteresis. Commented May 1, 2016 at 16:30
• It is just a pull up to define the "high" output of the comparator (I saw it in the datasheet, page 10: ti.com/lit/ds/symlink/lm393.pdf). Note that I already moved it to a different place - added another ~5.6V zener between R1 and D1 and moved R3 form D1 to it, so "high" is now about 11V instead of 5.4V. Little improvement, if any. Commented May 1, 2016 at 16:35
• Right, you have 5.6v through a 10k resistor to MOSFET gate, the only other thing capable of driving it high is the comparator itself, which can't output much current. $\tau = RC$ which is the time to charge to 63%, so for a resistance of 10k and gate capacitance $C_{iss}$ of 150pF, the time constant is 1.5uS. That's pretty fast already, but note that the gate voltages are not going from 0 to 5.6v but 1.7v-3.5v in the chart, which is in the linear region. Commented May 3, 2016 at 0:17

First off, great job so far. There are certainly chips and modules that could do this for you and at efficiencies higher than one could realistically obtain using discrete circuitry, but using them would be of little educational value. Just playing around and reinventing the wheel from stuff on hand is a great way to learn, and it looks like you're doing the 'playing around' part very well. And an oscilloscope is an oscilloscope. The only difference between a 'professional' one and an arduino sampling as hard as it can is what you can measure. If the thing you are measuring can be measured accurately enough with an 'arduinoscope', then there is no difference between it and a 'professional' oscilloscope. Just be sure you can trust your code and it's a perfectly adequate tool for what you're doing.

There is nothing wrong with your comparator circuit. In fact, it's behaving exactly as it should. Which unfortunately is not how you expected or intended it to behave (electrons just don't care about our intentions, they do what they want!).

This is a common problem I've seen when someone at home with digital electronics starts getting into analog circuitry and thinks about it as if it were digital. It isn't. Things aren't high or low, on or off. And any closed loop control circuit (closed loop meaning the output can effect the input - in this case, the comparator can effect the voltage it sees at its inverting input) is going to settle on a specific operating point, or just oscillate uselessly (because it is unstable due to taking too long to react - or too out of phase with it's feedback).

Comparators are not digital. They behave very non-linearly if they are open loop, and are the most 'digitally' in that usage case. Open loop means their output will not effect their input. This is, of course, not the case in your circuit. And here is the dirty secret: comparators are just high gain differential amplifiers. In other words, they're op amps with internal resistors arranged in a differential amplifier topology for the sake of convenience. They have very high gain, but it is not infinite. If you have a closed negative feedback loop, exactly like you do, it is going to to behave like an op amp. It is not a digital logic gate, it's an analog component that is designed to interface with digital circuitry, but you're not using it like that.

Also, a MOSFET is not digital either. They are not switches. They are transimpedance amplifiers. In fact, BJTs behave much more like switches than MOSFETs do. FETs can be modeled as a voltage controlled resistor, and a very linear one at 'intermediate' voltages. What you call intermediate is known as the linear region - and MOSFETs have a very wide such region. Much larger than other semiconductors. A FET is about the least-digital switching element you can find.

As the gate voltage gets higher, it loses its linear voltage to Rds behavior, but to get to the point where it is 'on', it must cross that linear region. Your comparator is not going to turn on the MOSFET sharply because it will rapidly close in on the intermediate voltage, just like the op amp it secretly is, and do whatever it needs to to keep the voltage at its inputs the same. It's oscillating near the ideal control point due to being configured to be a comparator, but it WANTS to turn the MOSFET partly on, it wants to make it a resistor, and it is doing a decent job despite being configured for a very different purpose. The true nature and op amp heart of a comparator reveals itself.

Why does it want the FET to be a resistor? Because that is what it needs to do. If the voltage goes above its noninverting terminal, it will turn on the FET until the voltage drops below the inverting terminal, and it will back off. If it could, it would settle on a specific voltage that keeps the FET only turned on enough to keep the voltage at its terminals equal. It can't do that, but it is trying and it is still doing a half-way respectable job of achieving that, even if it is oscillating around the voltage instead of settling on it. It will never turn the FET on quickly, or even all the way, as that would make the panel voltage drop too much. The ATtiny is unable to react fast enough, and so the voltage over and undershoots all over the place, but the comparator is fast and reacts continuously.

And it's working perfectly. It's tracking the power point you've set for the panel. You've given it a resistor it can control, so it is going vary the resistance of the MOSFET as needed to keep the panel voltage near the set point. What you've built is a somewhat awkward constant power dummy load. It's getting hot because the panel can't deliver the power drawn by the 100Ω resistor, so the MOSFET is being used to dynamically add extra series resistance until a power point is tracked. But it will only add just enough resistance to track that power point, and lower or raise it to constantly consume that amount of power. So it should get hot. Not because anything is wrong, however. The circuit is working, or trying to. If you used a proper op amp, it wouldn't oscillate, and instead keep it at whatever resistance is needed to consume a fixed wattage from the panel.

This is why MPPT is hard. You can't track a power point by turning the load on and off, you just get massive voltage swings up and down like you see with your ATTiny. You can't deliver all the power to the load without switching something on and off, because anything else means burning up the excess power. This is fundamentally the same reason linear regulators operate as they do. This is, really just a linear regulator. It's regulating a voltage. It doesn't matter that it is the voltage from a solar panel. It's still linear, and it is still trying to keep that voltage at a set point. And the only way it will do that is by burning power off as heat. Which is exactly what it is doing.

There is no software problem here. No amount of software can overcome this problem, unfortunately. If you want efficiency, you cannot use linear power point tracking. You will need to make it switch (which I know was your original intention), and you will need an energy storage device that will be alternatively charged and discharged by the switch. What I just described, and what you are envisioning (even if you didn't realize it) is a switching regulator. I recommend an inductor as your energy storage device. A PWM control chip would work in place of the comparator. A TL494 is a classic.

There is no way to do this with any amount of efficiency in the way you've set up, no matter software or anything else. A MPPT controller is usually an input-tracking buck-boost regulator for that reason, and one that charges a battery or super capacitor through an inductor. Building such a circuit is beyond the scope of this answer, but it is certainly worth giving 'TL494 MPPT' a quick google. Image search brings up a lot of examples. You could even try to do it using the attiny85 controlling the MOSFET, but you'll need to add an inductor, diode, capacitors, some other components. Unfortunately, the problem is physics, and it has not really shifted to software as it is, though it might seem like it. No matter what you do, you'll not get around burning that power without first adding the necessary components in a switching topology.

Anyway, building that would certainly be a terrific way to learn about electronics - and the scary parts like inductance and magnetics. It is also not for the feint of heart. Either way, good luck!

• Wooow, thanks for taking the time to explain all of this so detailed! I think it pretty much answers my actual question - why the comparator is not acting "digitally". And yeah, being a programmer I know stuff has the bad habit of doing exactly what you told it to do, not what you want. Switching to attiny was kind of a cheat (or give up) as I knew it would work - by "work" I mean "produce the square wave I expected to get from the comparator". The word "inductor" scares me for the moment, just like for-loops scare people who are just learning to program. For now :) Commented May 4, 2016 at 19:36
• @Mishony - I'm on a similar path it sounds like, having spent my youth in software, far away from physics :) I always thought inductors were for radio guys, so didn't pay attention in Electromagnetics. Now for me they're the coolest passive component. I found learning about them challenging because of the approach of most materials I found. One resource stood well out from the rest, Ch.1 of Maniktala; Switching Power Supplies A-Z, starting on p.21 Understanding the Inductor. Basic idea is feed a square (V) wave in, get a ramp (I) wave out. Amazon Look Inside is your friend on that one :) Commented May 4, 2016 at 22:21
• There is another important point. Most loads do not desire to have constant input power. Most loads need a voltage in some range, and will take the amount of power they need. If you (OP) manage to implement MPPT, but your load does not use all the power, then you will have to store the extra power (in a battery) or dissipate it (in a resistor or some type of load bank). There seems to be little advantage to using MPPT when you have no way to store the exta power. Commented May 7, 2016 at 19:50
• -1 for feint of heart. AND +2 for feint of heart :-).(ie in this case it should be "faint" BUT feint makes it fun. Also, good answer :-) Commented Sep 6, 2016 at 1:15