In this lesson we show how to configure the GPIO pins on the Jetson Nano to produce PWM signals. We show how the PWM libraries can be run in python. The Jetson Nano can provide Pulse Width Modulation signals on two physical pins, pins 32 and 33. We take you through the step by step process in the video above.
In Lesson 7, we learned how we can get in-between voltages from the Arduino pins using the analogWrite command. Actually, this command only approximates analog voltages, and does not produce actual analog signals. It works by quickly turning the voltage to the pin on and off. For example, if you ask for 2.5 volts, it will quickly switch the pin on, with it on 50% of the time and off 50% of the time. Similarly, if you asked for 1 volt, it is really switching 5 volts on and off quickly. For this case, it would be on 20% of the time and off 80% of the time. This technique is called Pulse Width Modulation. In this video we show you the actual waveforms coming from the analogWrite command on an oscilloscope. If you want to follow this lesson at home, you can get the Arduino kit we are using HERE.
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.
dutyCycle=input("What Duty Cycle? ")
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:
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:
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.
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.
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
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.
print"Button 1 Pushed"
print"Button 2 Pushed"
Making The World a Better Place One High Tech Project at a Time. Enjoy!