Sun Tracker: In Scratch, Python, and PSoC Creator

Hi Folks,

This project tries to find the brightest light source in the room, using a solar panel and a pan/tilt bracket with servos. It will sweep the solar panel around and then look at the brightest light source. This could be extended to actively track the light source after acquiring it. I got this solar panel from amazon, but any 5V one will do.

I’m going to be making this project in Python, Scratch, and PSoC Creator to better illustrate how to move between them, and to show what some of the differences are.

The wiring is the same for all 3 versions. Plug the servos into the PiSoC servo header. The solar panel goes into the analog port (black to GND, red to P[3]0), so we can measure the voltage from it. In PSoC Creator you’ll be able to choose any port for servos and analog. In Python and Scratch Servos and PWM are Port 2, and Analog is Port 3.


First up is a simple Scratch project. There are a few comments explaining the most important parts of the program.

  • First we start up our servos and set them to initial angles.
  • Then we have a forever block that makes the rest of the code run in an endless loop. We move the servo in 5 degree increments, from 5 degrees until we hit 180 degrees. We use the wait block to add a delay so that the servo can get in position before we issue the next command.
  • Each time the servo moves we record the voltage and see if it was higher than the last recorded voltage, while also recording the servo’s angle at that voltage. We keep doing this until we hit 180 degrees.
  • We then move the servo to the angle that corresponds to the highest voltage, which should be our light source.
  • Finally, we display the light source’s angle and use the stop all block to end the program, which will also turn off the servos.



Next is a simple Python script. This script works nearly the same as the Scratch version, with a few key differences.

  • Note the lines try except KeyboardInterrupt. This is needed to turn off the servos if you exit the program early with ctrl-c.
  • We move in one degree increments and have a wait of only 0.01 seconds. Python runs a little bit faster than Scratch which lets us have smoother movement of the Servos.
import sys
from pisoc import *
from time import sleep

#Read analog voltage on P[3]0
voltmeter = AnalogPin(0)

#servos can move 0 to 180 degrees.
Pan = Servo(0, max_angle = 180)   # You could choose any max angle, such as 5 to correspond to voltage input
Tilt = Servo(1, max_angle = 180) 

last_voltage = 0
max_voltage = 0

Pan.Start()#Start up the servos. Don't forget to stop them at the end!
Pan.SetAngle(5) #Set initial angles
best_angle = Pan.ReadAngle()

#Read voltage then rotate until it finds max
	while True:
		voltage = voltmeter.ReadVolts()
		if voltage > max_voltage+0.01:
			max_voltage = voltage
			best_angle = Pan.ReadAngle()

		#Keep sweeping until we hit 180
		if Pan.ReadAngle() < 180:
			Pan.SetAngle(Pan.ReadAngle() + 1) #Move 1 degree
			print ("Highest Voltage is %s"%max_voltage)
			print ("Best Angle is %s"%best_angle)
			Pan.SetAngle(best_angle+15) #We're at 180, go back to best angle
			sleep(2) #Wait for servos to get into position
			Pan.Stop() #Stop the servos

		print ("Voltage is %s"%voltage)
		print("Angle is %s"%Pan.ReadAngle())
		sleep(0.01) #wait for the servo to get into position before continuing

#Stop the servos if you exit the program early
except KeyboardInterrupt:
	print "exiting pisoc"

PSoC Creator

Coming Soon!

Comments (4)

  1. kowthar (reply)

    April 26, 2017 at 9:11 am

    thanks for sharing this use full project i have one question, i need to design solar tracker (pcp design ) by using psoc creator so could you tell me the way i can start it or do you have a solar tracker design you used psoc creator ?..


    1. Robert Barron (reply)

      April 26, 2017 at 9:14 pm

      The psoc program should be quite simple. You can control servos using PWM or just find a community made servo component. You can use an ADC component to monitor the voltage of a solar panel. The theory of operation and overall program structure would be the same as the python version. What I do in the python version is simply sweep in one direction while recording values, then move it to the recorded brightest (highest voltage) spot. Although I would recommend two solar panels next to each other, or even three in a triangular pattern so that you can actively move the servo towards the brightest point in both the x and y directions.

      Sorry for not posting the project myself yet, I’ve been quite busy!

  2. kowsar (reply)

    May 11, 2017 at 9:40 am

    thank you so much for your answer . just i have one question which is could you tell me the code of solar panel , i found the code of stepper motor but i could not find the solar panel code please help me i really need it .

    best regards

    1. Robert Barron (reply)

      May 16, 2017 at 8:02 pm

      There is no specific code for the solar panel. You can monitor its voltage output using an ADC component, which will give you an idea of how much light is hitting it. There are a few example projects built into psoc creator that will show you how to use the ADCs. A photodiode would also work for this purpose.

Leave a Reply

Your email address will not be published.

Published on: 7 May 2016
Posted by: Robert Barron
Discussion: 4 Comments