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.

Beaglebone Black Servo
Control of Servo From Beaglebone Black.

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 11: Dimable LED with Buttons from Python

In LESSON 10 we showed you how to create a dimable LED using the analog input from a potentiometer. In this lesson we will create a dimable LED using digital buttons. We will say we want a press of the top button to make the LED brighter, and a press of the bottom button to make the LED dimmer. In order to get going, you will need to build this circuit. If you do not already have a Beaglebone Black, you can get one HERE.

Push Buttons LED Circuit
This Circuit Controls LED Brightness from Push Buttons

In this circuit make note that we are using two 1000 ohm resistors as pull down resistors on the push buttons. It is important that these resistors be at least 1000 Ohm each. Next, notice the current limiting resistor on the LED is 330 Ohm.  We establish a ground rail on the breadboard from pin P9_2 on the Beaglebone Black. We establish our 3.3 Volt rail on the breadboard from pin P9_4 on the Beaglebone. We will use P9_14 as the PWM pin to control the LED, and we will use pins P9_23 and P9_27 as our digital input pins.

We will want a press of the top button to increase brightness and a pres of the bottom button to decrease brightness. As we discussed in Lesson 10, we want to insrease and decrease PWM signal exponentially, as this will allow the eye to perceive a smooth and linear increase in brightness.

If we want the LED to go from full off to full brightness in 10 steps, we need an equation to relate Duty Cycle to BP. BP will be a variable that will keep track of where we are. If we press the up button we increment BP by 1. If we press the down button, we decrements BP by 1. We want to start with BP=0, and the LED full off. This would be the point:

(BP,DutyCycle) = (0,0)

When the button has been pressed 10 times, we want a DutyCycle of 100%. This would be the point:

(BP,DutyCycle) = (10,100)

We now need to fit an exponential curve through these two points.

DutyCycle = C^(BP) -B

We need to figure out what the constants C and B need to be. Note DutyCycle and BP are our variables . . . they are like X and Y. We can plug our first point in and solve for B.

0 = C^0 – B

Anything raised to 0 equals 1, so the equation becomes

0 = 1 – B

B=1

Now substitute B into our equation and we get:

DutyCycle = C^(BP) -1

Now put in our second point to calculate the constant C.

100 = C^10 – 1

101 = C^10

C = tenth root of 101 = 1.5864

So, our final equation to calculate Duty Cycle is:

DutyCycle = 1.5864^(BP) – 1

With this equation we are not ready to develop our code. The video will step you through the code line by line.

Beaglebone Black LESSON 10: Dimable LED using Potentiometer

In this lesson we will create a dimable LED. We will read an analog voltage from a potentiometer, and use that to set the brightness on an LED. In order to proceed with this lesson, you will need to connect the following circuit:

Dimable LED
Potentiometer Reading is Used to Set LED Brightness

Note we are using P9_32 as the reference voltage on the voltage divider, we are using P9_34 as the reference ground, and we are using P9_33 as the analog sense pin. We also using P9_14 as the PWM output pin. Note the current limiting resistor in series with the LED is 330 Ohm.

The object of this circuit is to read the value of the potentiometer and then to use that to set the brightness on the LED. We know that the value we read from the potentiometer will be between 0 and 1. We know that what we can control on the PWM pin is the duty cycle of the 3.3 volt signal. We know that when the potentiometer reads 0, we want a 0% duty cycle on the PWM pin, which would have the LED off. This is our first point:

(0,0)

We also know that when we read 1 from the potentiometer, we want to apply a duty cycle of 100%, or have the LED be full bright. This is our second point:

(1,100)

If we created an equation for the line between these two points, we could calculate the duty cycle that should be applied based on the potentiometer reading. The problem with this is the way our eye perceives changes in brightness. We perceive exponential changes, so if we connected the two points with a linear relationship we would see lots of change at the low end of the scale, but as we continued to move the potentiometer, the brightness would appear to saturate. In order to have a nice smooth transition from full dim to full bright as the potentiometer is moved from left to right, we need to fit an exponential curve between the two points above. We want the LED to be off when the pot is full left, and full bright when the pot is fully to the right. We could use the following exponential equation:

Duty Cycle = C^(Analog Read) – B

This should do the trick, but we need to figure out what the constants C and B need to be. We do this by first plugging in the first point (0,0) from above:

0 = C^0 – B

Anything raised to 0 power is 1, so we have:

0= 1 – B

So B = 1. There, we have our first constant. We use this, and our second point to find C.

100 = C^1 – 1

101=C^1

C=101

Now we have everything we need to calculate the Duty Cycle from the value we read from the potentiometer. The final equation is:

Duty Cycle = 101^(Analog Read) – 1

Exponential
This Chart Shows how to Map Duty Cycle onto Analog Read

Note that this relationship has the desired properties. When we read a 0 from the potentiometer, we apply a Duty Cycle of 0% to the PWM pin, and the LED is off. When we read a 1 from the potentiometer, we apply a Duty Cycle of 100% to the LED and it is full bright. The exponential shape of the curve between these two points ensures that we will perceive a smooth increase in brightness as we turn the potentiometer up. Math works! It would be very hard to do this by trial and error.

We are now ready to begin developing our code. The video lesson explains the code line-by-line, and we are using commands we learned in the last few lessons.

The code works very well, and produces a very smooth transition from fully off to fully bright.

Beaglebone Black LESSON 9: Reading Analog Inputs from Python

If you went through our series of lesson on the Raspberry Pi, you will remember that we found the major limitation of the Pi is that it has no analog input pins. Luckily, the Beaglebone Black as a number of analog input pins, so we can greatly expand the scope of projects we can do.  The pinout below shows the pins that are available on the Beaglebone Black for analog input. (If you do not already have your Beaglebone Black, you can pick one up HERE.)

Beaglebone Black Pinout
Default Pin Configuration for the Beaglebone Black Rev. C.

You can see the blue shaded pins in the diagram above are for analog input.

A couple of very important points. These pins are designed to read analog voltages between 0 and 1.8 volts. Applying voltages above 1.8 volts can burn out the pin, or even smoke the Beaglebone. Hence, as you set up voltage divider circuits you must ensure they have a rail of 1.8 Volts, to ensure that the analog in pins will never see more than 1.8 Volts. Luckily, the Beaglebone provides a handy 1.8 Volt reference signal on pin 32  (on P9 header). Always use pin 32 as your reference rail when working with analog inputs. Similarly, you should use pin 34 (on P9 header) as your reference ground on your analog input circuits.

To demonstrate how to do analog reads, we will set up a simple voltage divider using a potentiometer. Go ahead and hook up your circuit as follows:

Potentiometer
A Simple Voltage Divider Using a Potentiometer

Note we are using P9_32 as the reference voltage on the voltage divider, we are using P9_34 as the reference ground, and we are using P9_33 as the analog sense pin.

With this circuit hooked up we are ready to develop some code. In the attached video we take you through this program step-by-step to show you how you can make analog readings from the potentiometer using python.

 Note the analog read returns a number between 0 and 1, which is proportional to the applied voltage. Hence to convert to actual voltage, we multiply this read value by 1.8 Volts.

Beaglebone LESSON 8: Read Button State from Python


In this tutorial we will see how to read digital values from the GPIO pins. We will be doing digital reads, which means we will be limited to “HIGH” or “LOW” readings. This is a 3.3 volt system, so we need to make sure that the “HIGH” applied signal is 3.3 volts.

Our pinout from LESSON 1 shows which pins are suitable for digital reads.

Beaglebone Black Pinout
Default Pin Configuration for the Beaglebone Black Rev. C.

It is the green GPIO pins which we can use for digital reads. In this lesson we will demonstrate the digital read technique using a simple two button circuit. In order to complete this lesson, you should go ahead and build this circuit.

Beaglebone Button
Example of Simple Beaglebone Black Button Circuit

Notice we are using Pin 1 on Header P9 as the ground and Pins 11 and 13 on header P9 s the input pins. Also note the pulldown resistors are 1000 Ohm. It is important to use at least this much resistance. If you do not have 1,000 Ohm resistors, using something larger NOT something smaller.

Once you have the circuit set up we are ready to begin programming.

First up, you need to import the GPIO library. If you have the latest version of Debian Wheezy, you should have the library on your system. If you do not have it you will need to update and upgrade your operating system. To load the library, you will use the python command:

We now need to configure out pins P9_11 and P9_13 as inputs. We do this with the command:

Now to read the state of the buttons, we would use the commands:

state1 will be True if the top button is pushed, and False if it is not being pushed. Similarly, state2 will be True when the button is being pushed, and False when it is not being pushed.

We can bring these concepts together to make the following program. Play around with the program and see what all you can make it do.