PivotPi is our new hardware to control servo motors. Its design is made elegant and compact to fit the Raspberry Pi Zero. PivotPi can also be connected to any of our Robots GoPiGo, GrovePi and BrickPi. It can drive upto 8 Servo motors and has 2 I2C ports to accommodate daisy chaining of other I2C devices. This tutorial will demonstrate a fun project on Waving Flags to get started with PivotPi.
Preparing the Flag
In order to make the flag stand on the Servo motor, insert a stick into the straw of the Flag and reduce its width as shown in the image to make it fit on the servo motor.
- Connect the PivotPi with the Raspberry Pi as shown in the image below.
- Connect the Servo motors to Channel 1 and Channel 2 of PivotPi such that the brown connector from the Servo motor is at the top when seen from the channel number as shown in the image below.
- Now connect the 6V battery supply to the 6v socket of the PivotPi and and also power the Raspberry Pi with a micro USB cable. Having done this you can see the Power LEDs and the Servo LEDs glow as shown in the image below.
This completes the Hardware setup and your PivotPi is ready to be programmed!
Software and Code
We use the Raspbian for Robots image on the Pi. You can buy an SD Card with Raspbian for Robots on it, or you can download it for free and install it using our directions here.
In this tutorial we are going to use the python libraries to program the PivotPi to control the Servo motors.
To get the libraries clone our repository using:
sudo git clone https://www.github.com/DexterInd/PivotPi.git
Open a new file using a nano editor:
sudo nano flag.py
And type the following code
# To make the code compatible with python3 from __future__ import print_function from __future__ import division from builtins import input import time import pivotpi try: pivotpi = pivotpi.PivotPi(0x40, 60) #PivotPi I2C address and PWM frequency except: print("PivotPi not found - quitting") exit(-1) print('Moving servos on channel 1-2, press Ctrl-C to quit...') while True: for i in range (0,180,10): # increasing the Pulse width in steps of 10 Microseconds for j in range(2): # Channels 1 and 2 pivotpi.angle(j, i) # Setting the Servo position between 0 and 180 degrees. time.sleep(0.05)
Save the code by pressing ctrl+x and y.
Run the code using:
sudo python flag.py
See your Flag waving!!!
You can learn more about the technical specifications of the PivotPi here.