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 P0), 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 PiSoC("PC") #Read analog voltage on P0 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! Tilt.Start() Pan.SetAngle(5) #Set initial angles Tilt.SetAngle(60) best_angle = Pan.ReadAngle() #Read voltage then rotate until it finds max try: 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 else: 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 Tilt.Stop() sys.exit(1) 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" Pan.Stop() Tilt.Stop()