# Beaglebone Black LESSON 12: Control a Servo from Python Using PWM

This lesson will show how to use Python running on the Beaglebone Black to control the position of a servo. First, I am using a small servo that I have verified can be powered from the Beaglebone Black without drawing too much current. All servos are different, so if you are unsure of the current requirements of your servo, it is safest to power it from an external 5 Volt power source. You can still control it through the control line connected to the Beaglebone Black, just make sure the servo and Beaglebone have a common ground.

Most all servos have three wires; Power, Control, and Ground. For my servo, Control is Yellow, Power is Red, and Ground is Black. If you have a different servo, check the data sheet to see what colors are Control, Power and Ground on your servo. Note we are using pin P9_2 as ground, P9_7 as 5V power, and P9_14 as our control pin.

We will be controlling the position of the servo using PWM. We will have to play around with our individual servo to determine precisely what signal pulse width will result in the servo being in the full left position, and what pulse width will result in the servo being in the full right position.

For most servos, full left is somewhere around 1 milliseconds, and the pulse width that will give us full right position is about 2 milliseconds. These are ballpark numbers, and we will have to play around with things to find the exact values for our servo.

We will set up a 50 Hz PWM signal. A 50 Hz signal has a period of:

period = 1/frequency = .02 seconds = 20 miliseconds.

Hence, if we want to get to about the full left position we would want a duty cycle of about 5%. Because 20 milliseconds x .05 = 1 milliseconds. This one millisecond pulse width should put the servo about in the full left position. Similarly for the full right position, we would want a duty cycle of 10%, because that would give us a pulse width of 2 milliseconds, since:

PulseWidth = Period x DutyCycle

PulseWidth = 20 x .1 = 2 milliseconds.

We can use the following code to determine precisely for our servo what dutyCycle will give the precise full left and full right positions.

For my servo, running a 50 Hz PWM singnal, I find that a duty cycle of 2% puts it in the full left position and a duty cycle of 12% puts it in the full right position.

Now we would like to be able to just specify an angle we want and have it go to that angle. If we want an angle of 0 degrees we would apply a 2% duty cycle. This value is for my servo. You will have to play around with your servo and the code above to find what this number is for you. But for me, I have the point:

(0,2)

That is to say when I desire an angle of 0 on the servo, I should apply a dutyCycle of 2 to the PWM pin. Similarly, when I desire 180 degrees, I should apply a dutyCycle of 12. (Again, this number might vary for your servo). For my servo, I get the point:

(180, 12)

We can fit a line to these points, which would then allow us to calculate the dutyCycle for any desired angle. The slope from the two points above would be:

m=(y2-y1)/(x2-x1)=(12-2)/(180-0) = 10/180= 1/18

Using the point slope form of the line, we would get

y-y1 = m (x- x1)

y – 2 = 1/18( x – 0)

y= 1/18*x + 2

Now putting in our actual variable names we get:

dutyCycle = 1/18*desiredAngle + 2

You can develop the same type equation just using the values suitable for your servo from the experiment above.

Now we can use this code to smoothly move the servo to any desired position.

# Beaglebone Black LESSON 7: Create a Dimable LED Circuit with PWM in Python

In this lesson we will explore how to use the PWM commands we learned in the last lesson to control the brightness of LEDs in a circuit. (If you do not have a Beaglebone Black yet, you can pick one up HERE.) In order to proceed with this project, you should hook the following circuit up to your Beaglebone Black.

Note that we are using pins “P9_14” and “P9_22” which are good working PWM pins. Note the current limiting resistors are 330 ohm. Always connect the long leg of the LED towards the control voltage.

The video steps you through the code to control the LED brightness.

# Beaglebone Black LESSON 6: Control PWM Signals on Output Pins from Python

In Lesson 4 and Lesson 5 we showed how to do digital writes to the GPIO pins using Python. (If you have not picked up your Beaglebone Black Rev. C yet, you can get one HERE) With digital writes, we could generate an output of 3.3 volts or 0 volts. For many applications, we would like analog output, or the in between voltages. The Beaglebone Black, as with most microcontrollers, can not produce true analog output. However, for many applications, an analog output can be simulated by creating a fast on/off sequence where the analog value is simmulated by controlling the ratio of on time and off time. This technique is called Pulse Width Modulation, or more simply, PWM. Consider a 3.3 volt signal, which is turning on and off with a frequency of 50 Hz.  A 50 Hz signal has a Period of: Period=1/frequency=1/50=.02 seconds, or 20 milliseconds. If during that 20 millisecond period, the signal was “High” for 10 milliseconds, and “Low” for 10 milliseconds, the signal would act like a 1.65 volt analog signal. The output voltage therefor could be considered the rail voltage (3.3 volts) multiplied by the duty cycle (percentage of time the signal is high.

For the Beaglebone Black, only certain pins can be used for PWM signals.

In the chart above, the purple pins are suitable for PWM output. You can see there are 7 pins which can produce PWM signals. In this lesson we show you how to control those pins.

In order to control PWM signals, we are going to use Python and the Adafruid_BBIO Library. Recent versions of Beaglebone Black Rev. C are shipped with the library already part of the operating system. If you are getting errors indicating that you do not have the library, update your operating system to the latest Debian image for the Beaglebone Black.

In order to use PWM in Python, you must load the Adafruit Library. If you have the recent versions of Debian Wheezy for the Beaglebone black, the library will already be on your system. If you do not do an update and upgrade on your operating system.

To begin with, you will need to load the library.

Next up, you will need to start the PWM on the pin you are using. We will use pin “P8_13”. Remember you must use one of the purple colored pins on the chart above. We start the PWM with the following command:

This command puts a 1000 Hz signal (Period of 1 mSec) on pin P8_13, with a duty cycle of 25%. This should yield a simulated analog voltage of .84 volts.

We can change the duty cycle after this initial setup with the command:

This command would change the duty cycle to 90%, which would simulate a voltage of 3.3 * .9 =  2.97 volts.

You can also change the frequency of the signal using the command:

This would change the frequency to 100 Hz (Period of 10 mSec). Changing the frequency does not really affect the net result of PWM in most applications, although it does matter for many servo applications.

After you are done, you can stop the PWM with the command:

And always remember to clean up after yourself with:

Play around with the Python Program below. Connect a DVM to your Beaglebone Black, and measure the DC voltage at the output pin. The DVM should show your anticipated voltages.

Considering that the simulated analog voltage V=3.365 X Duty Cycle, how would modify the program above to ask the user for the Voltage he desires, and then calculate the duty cycle that would give that voltage. Your assignment is to modify the program above where the user inputs desired voltage, and DC is calculated. Use a DVM to check your results

# Raspberry Pi LESSON 28: Controlling a Servo on Raspberry Pi with Python

In this lesson we will show you how to precisely control a Servo using the Raspberry Pi. First, for the small servo I am using, I have verified that it is safe to drive from the 5 volt pin (physical pin 2) on the Raspberry Pi. It is possible to damage your Raspberry Pi by drawing too much current out of a pin. So, if you are not sure about the current requirements of your Servo, it is best to power it from a 5 Volt source other than a Raspberry Pi pin. You can still control it from the Raspberry Pi if you use a common ground, but just get the power (red wire) from an external source. For my small servo, I can safely power it from Raspberry Pi physical pin 2.

The second point is that to control the servo, you have to use Pulse Width Modulation. So, I STRONGLY recommend that you go through LESSON 27 if you have not already. Lesson 27 shows you how to use PWM on the GPIO pins on the Raspberry Pi. If you are up to speed on PWM, this lesson will go a lot easier.

So, with that out of the way, we are ready to hook up our servo. For my servo, the ground wire is Black, the Red wire is 5 volt, and the yellow wire is the control line. If you are using a different servo, you will need to read the instructions to see what the color code is for your three wires, but it is likely similar to mine. The sketch below shows how you should hook the servo up. Notice I have the 5V hooked to the Pi physical pin 2, the servo ground hooked to the Pi physical pin 9, and the servo control line hooked to the Pi physical pin 11.

Now with the Servo hooked up, we are ready to try and control it. For this example, we will work in the Python shell. To enter the python shell, open a Terminal window, and at the command prompt type:

\$sudo python

It is important to include sudo, as the Raspberry Pi only allows access to the GPIO pins to the superuser. Hence, you need to enter the python shell as a superuser. When you type the command above, you should be moved into the Python shell, and should see the python shell prompt of >>>.

We are now ready to control the servo. We must first import the RPi library. These first steps should be familiar if you did LESSON 27.

>>>import RPi.GPIO as GPIO

Now we need to tell the Pi what pin numbering scheme we want to use. I like to use the physical pin numbers (See LESSON 25 for a diagram). So, we need to issue the command:

>>>GPIO.setmode(GPIO.BOARD)

Now we need to tell the Pi that physical pin 11 will be an output pin:

>>>GPIO.setup(11,GPIO.OUT)

The servos position is controlled by the pulsewidth of a 50 Hz PWM signal. Hence, we need to turn the PWM sequence on at 50 Hz. Note that for a 50 Hz signal, the Period of the signal is 1/50=.02 seconds, or 20 milliseconds. Keep this Period in mind as we will come back to it later. We start by creating a PWM object on Pin 11 with a 50 Hz signal with the command:

>>>pwm=GPIO.PWM(11,50)

We can now start the pwm sequence by giving a command to specify the DutyCycle of the signal. Before we do this, we need to talk a little bit about how servos work. A typical servo wants to see a frequency of 50 Hz on the control line. The position it moves to depends on the pulse width of the signal. Most servos behave roughly as such, but you will need to tweak these numbers for your particular servo. Typically, the servo will go to the full left position when it sees a pulse width of 1 millisecond, it will go the middle position when it sees a pulse width of 1.5 millisecond, and it will go to the full right position when it sees a pulse width of 2 millisecond. Note however, that on the Raspberry Pi we do not specify a pulse width, but we specify a DutyCycle. So, we can use the following relationship:

DutyCycle =PulseWidth/Period

Remember that Period = 1/frequency, so:

DutyCycle = PulseWidth/(1/frequency) = PulseWidth * frequency

The PulseWidth that will give us a full left position is 1 milllisecond. We now calculate the applied DutyCycle to give us the desired position:

DutyCycle = PulseWidth*frequency=.001 *50 = .05 = 5%

So, for a 50 Hz signal, if we set the DutyCycle to 5, then we should see the servo move to the full left position. Similarly, if we set DutyCycle to 7.5, we should get the middle position, and if we set it to 10 we should be in the full right position. You can get all the intermediate positions by linearly scaling between 5 and 10. Note that these values will vary between brands, and between individual servos, so play around with your servo to get it calibrated. We are now ready to apply a command to position the servo. If we want the servo in the full left position, we should set the DutyCycle to 5%. We do that with the command:

>>>pwm.start(5)

This will start the PWM signal, and will set it at 5%. Remember, we already specified the 50 Hz signal when we created the pwm object in our earlier commands. Now if we want to change the position, we can change the DutyCycle. For example, if we want to go to the middle position, we want a DutyCycle of 7.5, which we can get with the command:

>>>pwm.ChangeDutyCycle(7.5)

Now if we want the full right position, we want a duty cycle of 10, which we would get with the command:

>>>pwm.ChangeDutyCycle(10)

Remember, it is not DutyCycle that actually controls servo position, it is PulseWidth. We are creating DutyCycles to give us the desired PulseWidth.

Now, play around with your particular servo and then find the specific DutyCycles that lead to full left and full right positions. For my servo, I find that full left is at DutyCycle=2, and full right is at DutyCycle=12. With these values, I can create a linear equation that will give me any angle I want between 0 and 180. This will make the Raspberry Pi behave much more like the simple and intuitive operation of the Arduino.

To do the linear equation I need two points. Well, I know that for a desired angle of 0, I should apply a DutyCycle of 2. This would be the point (0,2). Now I also know that for a desired angle of 180, I should apply a DutyCycle of 12. This would be the point (180,12). We now have two points and can calculate the equation of the line. (Remember, play with your servo . . . your numbers might be slightly different than mine, but the methodology below will work if you use your two points)

Remember slope of a line will be:

m=(y2-y1)/(x2-x1)=(12-2)/180-0)=10/180 = 1/18

We can now get the equation of the line using the point slope formula.

y-y1=m(x-x1)

y-2=1/18*(x-0)

y = 1/18*x + 2

Putting in our actual variables, we get

DutyCycle = 1/18* (DesiredAngle) + 2

Now to change to that position, we simply use the command:

pwm.ChangeDutyCycle(DutyCycle)

I hope this makes sense. Watch the video as I step you through it carefully. If the writeup above does not make sense, hopefully the video will clear things up.

# Raspberry Pi LESSON 27: Analog Voltages Using GPIO PWM in Python

If you remember our Arduino Lessons, you will recall that we could write analog voltages to the output pins with the ~ beside them. The truth is, though, we were not really writing analog voltages, we were just simulating analog voltages using pulse width modulation (PWM). The arduino was able to put out 5 volts. Hence, if you want to simulate a 2.5 volt signal, you could turn the pin on and off every quickly, timing things such that the pin was on half the time and off half the time. Similarly, if you wanted to simulate a 1 volt analog out, you would time things so that the 5 volt signal was on 20% of the time. For many applications, such as controlling LED brightness, this approach works very well. Arduino made it easy and transparent to the user to generate these analog-like output voltages using the analogWrite command.

This capability is also available on the Raspberry Pi GPIO pins. However, the implementation requires you to think in terms of a signal with a frequency and a duty cycle. Consider a signal with a frequency of 100 Hz. This signal would have a Period of 10 milliseconds. In other words. the signal repeats itself every 10 milliseconds. If the signal had a duty cycle of 100%, it would be “High” 100% of the time, and “Low” 0% of the time. If it had a duty cycle of 50% it would be high 50% of the time (.5X10 milliseconds= 5 milliseconds) and low 50% of the time (.5X10 milliseconds = 5 milliseconds). So, it would be high 5 milliseconds, and low 5 milliseconds for a total period of 10 milliseconds, which as we expect, if a frequency of 100 Hz. (Note that the Period of a signal = 1/frequency, and frequency = 1/Period)

Note on the Raspberry Pi, the output voltage is 3.3 volts as opposed to the 5 volt output on the Arduino. Hence, the Raspberry Pi can only simulate analog voltages between 0 and 3.3 volts. For this example, we will be playing with the following circuit again. Note we are using physical pin 9 as the ground and physical pin 11 as the power pin. See Lesson 25 below for a diagram of pin numbers on the Raspberry Pi.

OK, enough background, lets start playing with some code. On examples like this, I think it is easiest to operate from the Python Shell, as this allows us to observe the effects of our commands one at a time. To enter the Python Shell, type sudo python at the linux command line in a terminal window. The sudo is important as it allows you to enter the python shell as a superuser. Access to the GPIO pins requires superuser privileges. Also, remember that to exit the python shell and return to the Linux command prompt you enter Ctrl-d. So, type in sudo python to go to the python shell. You should see the >>> prompt indicating you are not in the python shell. The first thing you need to do is import the RPi library:

>>> import RPi.GPIO as GPIO

Now tell the Raspberry Pi which pin number scheme you want to use (See Lesson 25).  I prefer to use the physical pin numbering system as I find it easier to remember. To use the physical pen numbering system, you would enter this command:

>>> GPIO.setmode(GPIO.BOARD)

Note, if you prefer the BCM system, replace BOARD with BCM in the command above.

Now we need to tell the Pi that physical pin 11 will be an output. We can do that will the command:

>>> GPIO.setup(11,GPIO.OUT)

At this point we could write the pin high or low, but our objective here is to use PWM, so we need to do a few more things. First, we need to create a PWM object. I will call my object my_pwm. We will need to pass the parameters of the physical pin we want to use, and the frequency. I like to use 100 Hz, which gives us a period of 10 msec. The command we need for this is:

>>>my_pwm=GPIO.PWM(11,100)

Remember capitalization needs to be EXACT! Now to start the pwm we need to decide what DutyCyle we want. Remember, the DutyCycle is the percentage of the period that the signal will be high. If we wanted to approximate a 1.6 volt signal, we would note that 1.6 is about half of the 3.3 coming out of the Pi, so we would want a 50% duty cycle. The command for this would be:

>>>my_pwm.start(50)

When you type this command you should see the LED come on, if you have connected things correctly. It should be at about half brightness.

Now if you would like to change the brightness, just change the Duty Cycle. For example, if you wanted the LED very dim, you might set a 1% duty cycle. You could do this with the command:

>>>my_pwm.ChangeDutyCycle(1)

Similarly, if you wanted full brightness, you would want a 100% duty cycle, which you could get with the command:

>>>my_pwm.ChangeDutyCycle(100)

Again, please remember that the capitalization has to be exact. Now you can get any brightness you want by changing the duty cycle to anything between 0 and 100, inclusive.

Note you can also change the frequency of the PWM signal. Lets say you wanted a frequency of 1000 Hz. We could do this with the command:

>>>my_pwm.ChangeFrequency(1000)

Note that by doing this there is no perceptible change in LED brightness because you have not changed the relative on and off time of the signal. You are just going faster, but not impacting the fractional time the signal is on and off, hence the LED brightness does not change.

While in these examples we have done things from the control line, you can write python programs that will run the commands for you. For example, write a program that asks the user how bright he wants the LED, between 0 and 100, and then set it to that brightness by adjusting the Duty Cycle, as we did in the example above. Play around with different pins and different frequencies and values. Become familiar with these commands.

Now, finally, if you want to turn pwm off, you would use the command:

>>>my_pwm.stop()

Also, remember that you should always clean up after yourself, so at the bottom of your program, or before you exit the shell, always release your pins and clean up using the command:

>>>GPIO.cleanup()