Solar Controller / PV Router code - working on 60Hz / 120V power systems

I wanted to share my experience in getting the Solar Controller / PV Router code (I have seen it referred to by many names) running on a North American 60 Hz / 120 V power system. The code is really nice code written by MartinR for the purpose of diverting excess PV production into a hot water heater instead of exporting it. The code, although very well written, has several spots in it hard coded for a 50 Hz line frequency, and it also is setup for only 2 CTs instead of the standard 3 on a EmonTX. With some assistance (thanks to all who gave feedback) I have been able to make the necessary changes to get it to work on 60 Hz line frequency and with 3 CTs. I am currently benchmarking it over 3 - 4 days for accuracy against some external meters. I would like to make some suggestions to the community to make the current release of the software more internationally friendly. Now, I have not tested my code on a 50 Hz install (unless someone wants to fly me and my hardware over and host me for a few days ;-) ), so I cannot guarantee that my changes did not affect that function.

My first suggestion would be to transfer the code to GIT so all of the various edits can be tracked. This may already be done, but it was a bit confusing to follow the long forum posts with various edits from the original post that MartinR released the code on. I was uncertain if the file at the top of that long discussion was the original, or had been updated with the many changes that had been suggested over the months of discussion. It appears it was updated, but that was not obvious at first.

Secondly, I changed the sections of code that were hardcoded for 50 Hz to use compiler calculations using the user defined frequency. (I didn't have these compiler calculations and branching in my FORTRAN / Pascal days, they are neat!) I also used these compiler options to include / exclude the temperature measurement, power diversion, and syncpin pulsing. I don't have the one wire libraries and didn't feel like hunting them down since I am not doing any temp measurement. For power diversion, the code is very useful even if you don't need to divert excess power. Much of the U.S. is a net metered area, meaning I sell my excess back to the grid at the same price as I buy, so no motivation to use excess power. Lastly, looking at the PLL Lock / Unlock status on the serial monitor, by disabling the syncpin, it may have reduced some of the ADC noise and made it easier to lock. I tightened up the PLLUNLOCKRANGE (amount the first voltage sample is able to vary off of zero before you declare the PLL to be unlocked) quite a bit to see how well the loop was tracking. By using a value of 3 instead of 80, I could see how much the loop was bouncing around. Removing the syncpin output lines seemed to drastically improve the locking of the loop.

Anyhow, I am attaching that code here in the hopes that it may assist some one else. I am certain my edits are in no way optimized so any suggestions on how to improve would be useful. MartinR, thank you for sharing your work. I hope you see my additions as helpful for getting more 60 Hz folks able to utilize it.

I look forward to discussion and feedback. Thanks.


MartinR's picture

Re: Solar Controller / PV Router code - working on 60Hz / 120V power systems

Well done Dan, some good work there.

Interesting re the sync pin introducing noise. I suppose it makes sense that toggling a digital output just at the critical point for PLL lock will add noise. I will look into it on the original code. It's only really there to sync a scope anyway.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.