(*) you can buy a complete Pan/Tilt platform with the servos or build your own.

Step 2: How PWM Works

The Raspberry Pi has no analog output, but we can simulate this, using a PWM (Pulse Width Modulation) approach. What we will do is to generate a digital signal with a fixed frequency, where we will change the pulse train width, what will be "translated" as an "average" output voltage level as shown below:

We can use this "average" voltage level to control a LED brightness for example:

Note that what matters here is not the frequency itself, but the "Duty Cycle", that is the relation between the time that the puls is "high" divided by the wave period. For example, suppose that we will generating a 50Hz pulse frequency on one of our Raspberry Pi GPIO. The period (p) will the inverse of frequency or 20ms (1/f). If we want that our LED with a "half" bright, we must have a Duty Cycle of 50%, that means a "pulse" that will be "High" for 10ms.

This principle will be very important for us, to control our servo position, once the "Duty Cycle" will define the servo position as shown below:

Servo

Step 3: Installing the Hw

The servos will be connected to an external 5V supply, having their data pin (in my case, their yellow wiring) connect to Raspberry Pi GPIO as below:

GPIO 17 ==> Tilt Servo

GPIO 27 ==> Pan Servo

Do not forget to connect the GNDs together ==> Raspberry Pi - Servos - External Power Supply)

You can have as an option, a resistor of 1K ohm between Raspberry Pi GPIO and Server data input pin. This would protect your RPi in case of a servo problem.

Step 4: Servos Calibration

The first thing to do it is to confirm the main characteristics of your servos. In my case, I am using a Power Pro SG90.

From its datasheet, we can consider:

Range: 180o

Power Supply: 4.8V (external 5VDC as a USB power supply works fine)

Working frequency: 50Hz (Period: 20 ms)

Pulse width: from 1ms to 2ms

In theory, the servo will be on its

Initial Position (0 degrees) when a pulse of 1ms is applied to its data terminal

Neutral Position (90 degrees) when a pulse of 1.5 ms is applied to its data terminal

Final Position (180 degrees) when a pulse of 2 ms is applied to its data terminal

To program a servo position using Python will be very important to know the correspondent "Duty Cycle" for the above positions, let's do some calculation:

Let's test the servos individually. For that, open your Raspberry terminal and launch your Python 3 shell editor as "sudo" (because of you should be a "super user" to handle with GPIOs) :

sudo python3

On Python Shell

>>>

Import the RPI.GPIO module and call it GPIO:

import RPi.GPIO as GPIO

Define which pin-numbering schemes you want to use (BCM or BOARD). I did this test with BOARD, so the pins that I used were the physical pins (GPIO 17 = Pin 11 and GPIO 27 Pin 13). Was easy for me to identify them and not make mistakes during the test (In the final program I will use BCM). Choose the one of your preference:

GPIO.setmode(GPIO.BOARD)

Define the servo pin that you are using:

tiltPin = 11

If Instead, you have used BCM scheme, the last 2 commands should be replaced by:

GPIO.setmode(GPIO.BCM)
tiltPin = 17

Now, we must specify that this pin will be an "output"

GPIO.setup(tiltPin, GPIO.OUT)

And, what will be the frequency generated on this pin, that for our servo will be 50Hz:

tilt = GPIO.PWM(tiltPin, 50)

Now, let's start generating a PWM signal on the pin with an initial duty cycle (we will keep it "0"):

tilt = start(0)

Now, you can enter different duty cycle values, observing the movement of your servo. Let's start with 2% and see what happens (we spect that the servo goes to "zero position"):

tilt.ChangeDutyCycle(2)

In my case, the servo went to zero position but when I changed the duty cycle to 3% i observed that the servo stayed in the same position, starting to move with duty cycles greater than 3%. So, 3% is my initial position (o degrees). The same happened with 10%, my servo went above this value, topping its end on 13%. So for this particular servo, the result was:

0 degree ==> duty cycle of 3%

90 degrees ==> duty cycle of 8%

180 degrees ==> duty cycle of 13%

After you finish your tests, you must stop the PWM and clean up the GPIOs:

tilt= stop()
GPIO.cleanup()

The above Terminal print screen shows the result for both of my servos (that has similar results). Your range can be different.

Step 5: Creating a Python Script

The PWM commands to be sent to our servo are in "duty cycles" as we saw on the last step. But usually, we must use "angle" in degrees as a parameter to control a servo. So, we must convert "angle" that is a more natural measurement to us in duty cycle as understandable by our Pi.

How to do it? Very simple! We know that duty cycle range goes from 3% to 13% and that this is equivalent to angles that will range from 0 to 180 degrees. Also, we know that those variations are linear, so we can construct a proportional schema as shown above. so, given an angle, we can have a correspondent duty cycle:

dutycycle = angle/18 + 3

Keep this formula. We will use it in the next code.

Let's create a Python script to execute the tests. Basically, we will repeat what we did before on Python Shell:

The core of above code is the function setServoAngle(servo, angle). This function receives as arguments, a servo GPIO number, and an angle value to where the servo must be positioned. Once the input of this function is "angle", we must convert it to duty cycle in percentage, using the formula developed before.

When the script is executed, you must enter as parameters, servo GPIO, and angle.

For example:

sudo python3 angleServoCtrl.py 17 45

The above command will position the servo connected on GPIO 17 with 45 degrees in "elevation". A similar command could be used for Pan Servo control (position to 45 degrees in "azimuth"):

When the script is executed, you must enter as parameters, Pan angle and Tilt angle. For example:

sudo python3 servoCtrl.py 45 120

The above command will position the Pan/Tilt mechanism with 45 degrees in "azimuth" (Pan angle) and 120 degrees of "elevation" (Tilt Angle). Note that if no parameters are entered, the default will be both, pan and tilt angles set up to 90 degrees.

Plastics Contest

Make it Glow Contest 2018

Optics Contest

8 Discussions

very interesting, thanx. I understood from several buyers that the pan/tilt unit you linked to has some issues: supposedly the servo' s do not fit properly and some handy work is needed. Nevertheless a good starting point