Sun Tracking for Solar Power – Part 2 – The Software

Sun Tracking for Solar Power – Part 2 – The SoftwareIMG_7861 2

SwitchDoc Labs has built a lot of solar power systems over the past few years.   SunControl, Project Curacao2Project Curacao,  SunAirPlus,  WeatherPi and the recent Solar Powered ESP8266.  We have fooled around with sun tracking systems, but we have never built one all the way out and then gathered the data to figure out if it was worth it.

In this series of postings, we are going to show you how to build a simple solar tracking system using a Raspberry Pi and a stepper motor.   The purpose of this project is to verify experimentally the gain in power from a solar panel from using tracking versus a fixed solar panel.

All the graphs in this series of posting are done using MatPlotLib on the Raspberry Pi.

There are four parts in this series of postings.

 

The Sun Tracking Software

IMG_9479The code for Sun Tracker is pretty simple.   You start the unit by pointing the entire unit due east at 90 degrees bearing.   But you have to remember one thing.    If you are using a compass, due east is not due east on the compass.   You need to compensate for the fact that the magnetic north pole is not the same as the geographic north pole.   This is called magnetic declination [ref:  https://www.ngdc.noaa.gov/geomag/declination.shtml.   At the location of SwitchDoc Labs, this is about 14 degrees.   That means we point the unit at 76 degrees instead of 90 degrees to get due east.   Does this matter?   Yes.   14 degrees is almost an hour of sun travel and you won’t be pointing correctly if you don’t take this into account.   The figure to the right shows the initial test on March 30th before we had applied the magnetic declination correction.   Note that the fixed cell is generating more power than the tracked power in the latter half of the day.   This is because of the mis-aimed moving panel.SunPower-8134

We then track the sun over a total of 180 degrees (if we were being more careful, it would probably be ~150-160 degrees because of the horizon) over the course of the length of the day (based on 763 minutes of Sun on March 1st for our location).

After sunrise, the code starts tracking the sun by turning the stepper motor two steps every 6 minutes, which corresponds for our stepper motor of 0.7 degrees per step or 1.4 degrees every 6 minutes.   The following code snippet shows the loop and stepping code.

# we go 180 degrees from Due East to Due West 
# As of March 29, Sun rises at SwtichDoc Labs at 06:32 and sets at 19:15 for 12:43 minutes of Sun
# so we do the calcuations based upon 763 minutes of Sun.   That is 0.236 degrees per minute.
# based on the stepper motor we are using (513 for 360 degrees) We get 0.70 degree per step.
# this means we should do 1 step every three minutes or two steps every six minutes.

# calculate where the sun is depending on the time we start....
# set sunrise
sunriseTimeUTC = datetime.datetime.now();
sunriseTimeUTC = sunriseTimeUTC.replace(hour=6, minute=38)+datetime.timedelta(seconds=7*60*60);  #UTC 7 hours ahead 



print "sunriseTime UTC = %s" % (sunriseTimeUTC.strftime("%Y-%m-%d %H:%M:%S"));
sunriseTimePST = sunriseTimeUTC - datetime.timedelta(seconds=7*60*60);
print "sunriseTime PST = %s" % sunriseTimePST.strftime("%Y-%m-%d %H:%M:%S");

# assume we start the day Due East.

# calculate where we should be pointing at this time.

minutesAfterSunriseDT = datetime.datetime.now() - sunriseTimeUTC ;
minutesAfterSunrise = minutesAfterSunriseDT.seconds/60;
while (minutesAfterSunrise < 0):
	# still dark
	delay(60.0)
	minutesAfterSunriseDT = datetime.datetime.now() - sunriseTimeUTC ;
	minutesAfterSunrise = minutesAfterSunriseDT.seconds/60;
	print "minutes before sunrise = %i" % (-minutesAfterSunrise);

print "minutes after sunrise = %i" % (minutesAfterSunrise);


initialSteps = minutesAfterSunrise * 0.236/0.70;
print "initialSteps = %i" % (initialSteps);
stepper.step(initialSteps);

currentSteps = initialSteps;
currentDegrees = currentSteps * 0.7
numberOfMinutesLeftInDay = 751 - minutesAfterSunrise;

for x in range (0, numberOfMinutesLeftInDay):

	if ((x % 6) == 0):
		# advance two steps every 6 minutes 
		nowPST = datetime.datetime.now() - datetime.timedelta(seconds=7*60*60);

		print "two step at currentTime PST = %s" % nowPST.strftime("%Y-%m-%d %H:%M:%S");
		stepper.step(2)
		currentSteps = currentSteps + 2
		currentDegrees = currentSteps * 0.7

	
	# every minute, take samples
	print "Data saved:"+ time.strftime("%Y-%m-%d %H:%M:%S")

	SunTrackerDatabase.writeDataToDatabase(currentSteps, currentDegrees, disp, i2cMux, adc, SunAirPlus0, SunAirPlus1);
 	#time.sleep(1.0);	# delay 1 seconds
 	time.sleep(60);	# delay 60 seconds

 

The Python code reads the amount of solar power created by both solar panel systems and then stores the data in a MySQL database on the Raspberry Pi for later graphing and analysis.

All the Sun Tracker code is here on github https://github.com/switchdoclabs/SDL_Pi_SunTrackerTest.

We ran the test for three days on March 30, March 31 and April 1st.

 

Coming in Part 3 – The Results

Next we will look at the results from our three day trial.