In this lesson we show you how to boot up your Beaglebone Black, and how to connect via the Putty SSH terminal client, and how to remotely run the graphical desktop on your PC. If you do not already have your Beaglebone Black, you can pick up one HERE.
The Beaglebone black has only one USB connection, so I find the easiest way to work with it is to just connect by a remote desktop, instead of trying to connect screen, keyboard, and mouse directly to the Beaglebone. This lesson will get you started so you can get connected and talking to your Beaglebone.
In this lesson we are ready to bring together a lot of what we learned in earlier lessons. We will create dimable LEDs which will respond to two buttons. If one is pressed the LED will gradually grow dimmer. If the other is pressed, the LED will gradually grow brighter. This will require us to use our skills in using GPIO inputs, pullup resistors, GPIO outputs, and PWM.
For convenience we will use the same circuit we used in LESSON 30, shown below. Also, if you want to follow along with these lessons, you can buy the gear you need HERE.
This Circuit Controls two LED from Push Buttons Using the Raspberry Pi
The objective of this circuit is that we want the LEDs to grow brighter each time the right button is pushed, and we want them to grow dimmer each time to left button is pushed.
The video above steps through and explains this code.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fromtimeimportsleep# Library will let us put in delays
importRPi.GPIO asGPIO# Import the RPi Library for GPIO pin control
GPIO.setmode(GPIO.BOARD)# We want to use the physical pin number scheme
button1=16# Give intuitive names to our pins
button2=12
LED1=22
LED2=18
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Button 1 is an input, and activate pullup resisrot
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Button 2 is an input, and activate pullup resistor
GPIO.setup(LED1,GPIO.OUT)# LED1 will be an output pin
GPIO.setup(LED2,GPIO.OUT)# LED2 will be an output pin
pwm1=GPIO.PWM(LED1,1000)# We need to activate PWM on LED1 so we can dim, use 1000 Hz
pwm2=GPIO.PWM(LED2,1000)# We need to activate PWM on LED2 so we can dim, use 1000 Hz
pwm1.start(0)# Start PWM at 0% duty cycle (off)
pwm2.start(0)# Start PWM at 0% duty cycle (off)
bright=1# Set initial brightness to 1%
while(1):# Loop Forever
ifGPIO.input(button1)==0:#If left button is pressed
print"Button 1 was Pressed"# Notify User
bright=bright/2.# Set brightness to half
pwm1.ChangeDutyCycle(bright)# Apply new brightness
pwm2.ChangeDutyCycle(bright)# Apply new brightness
sleep(.25)# Briefly Pause
print"New Brightness is: ",bright# Notify User of Brightness
ifGPIO.input(button2)==0:# If button 2 is pressed
print"Button 2 was Pressed"# Notify User
bright=bright*2# Double Brightness
ifbright>100:# Keep Brightness at or below 100%
bright=100
print"You are at Full Bright"
pwm1.ChangeDutyCycle(bright)# Apply new brightness
pwm2.ChangeDutyCycle(bright)# Apply new brightness
sleep(.25)# Pause
print"New Brightness is: ",bright#Notify User of Brightness
In this lesson we will show how you can control LED’s from push buttons. In order to get started, you will want to expand the circuit we built in LESSON 29 to include two LEDs. The schematic below shows how you will want to hook things up (Also, remember you can see the Raspberry Pi pinout in LESSON 25). Also, as we have mentioned before, if you want to follow along with us in these lessons you can get a kit that has all the gear you need HERE.
This Circuit Controls two LED from Push Buttons Using the Raspberry Pi
In the video lesson, we take you through the code step-by-step. We use the techniques learned in LESSON 29 to detect if a button has been pushed. We introduce two new variables, BS1 and BS2, so indicate the state of the LED’s. A BS1=False means the LED1 is off. A BS1=True means the LED is on. This concept allows us to determine whether we should turn the LED on or off when the button is pushed. Basically, we want to put it in the opposite state when a button is pushed. The code is below. The video shows how it works.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fromtimeimportsleep# Import sleep Library
importRPi.GPIO asGPIO# Import GPIO Library
GPIO.setmode(GPIO.BOARD)# Use Physical Pin Numbering Scheme
button1=16# Button 1 is connected to physical pin 16
button2=12# Button 2 is connected to physical pin 12
LED1=22# LED 1 is connected to physical pin 22
LED2=18# LED 2 is connected to physical pin 18
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Make button1 an input, Activate Pull UP Resistor
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Make button 2 an input, Activate Pull Up Resistor
GPIO.setup(LED1,GPIO.OUT,)# Make LED 1 an Output
GPIO.setup(LED2,GPIO.OUT)# Make LED 2 an Output
BS1=False# Set Flag BS1 to indicate LED is initially off
BS2=False# Set Flag BS2 to indicate LED is initially off
while(1):# Create an infinite Loop
ifGPIO.input(button1)==0:# Look for button 1 press
print"Button 1 Was Pressed:"
ifBS1==False:# If the LED is off
GPIO.output(LED1,True)# turn it on
BS1=True# Set Flag to show LED1 is now On
sleep(.5)# Delay
else:# If the LED is on
GPIO.output(LED1,False)# Turn LED off
BS1=False# Set Flag to show LED1 is now Off
sleep(.5)
ifGPIO.input(button2)==0:#Repeat above for LED 2 and button 2
We are now ready to learn how to “read” values from the Raspberry Pi GPIO pins. In order to demonstrate this, we will show a simple example using buttons. If you ordered the Raspberry Pi kit we recommend, you already have everything you need, or you can pick your kit up HERE. To start with, you need to put together a simple circuit that connects two push buttons to your Raspberry Pi. Connect according to this schematic.
Simple Circuit Connecting Two Push Buttons to the Raspberry Pi
Note that one leg of each button is connected to the ground rail on the breadboard, that is connected to the Pi ground at physical pin 6. Then we connect the left leg of the left button to physical pin 16, and the left leg of the right button to physical pin 12.
In order to read the state of these buttons, that is, whether they are being pressed or not, we need to write a python program. To begin with we must import GPIO library and specify that we want to
Python
1
2
3
fromtimeimportsleep# Import time library
importRPi.GPIO asGPIO# Import RPi Library
GPIO.setmode(GPIO.BOARD)# Specify We Want to reference Physical pins
Now we are ready to set the pin modes on the pins we are using. We are using pins 12 and 16. We will set up variables so that we can reference the pins by descriptive variables.
Python
1
2
3
4
button1=16# Descriptive Variable for pin 16
button2=12# Descriptive Variable for pin 12
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Set button1 as input and Activate pull up resistor
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Set button2 as input and Activate pull up resisor
Note in our GPIO.setup commands, we are not just defining the pins as inputs, we are also activating pullup resistors with
Python
1
pull_up_down=GPIO.PUD_UP
With this command, the raspberry pi places a pullup resistor between the designated pin and the 3.3 V rail. This means that if we simply read the pin, we will read a “1”, “True”, or “High”, since the pin will see the rail through the pullup resistor. If we connect the pin to ground by pressing a button or switch, the pin will then read a “0”, “False” or “Low” because it will be a straight connection to ground, and as current flows through the pullup resistor, the 3.3 Volts will drop across the pullup resistor. Hence, the pin sees 0 volts.
The result is that with the pullup resistor activated, the pin will always report a “1” until something connects the pin to ground, and then it will read a “0”. This configuration should work for most things, but if you are getting unpredictable results which can result from electrical noise, then try using external pullup resistors.
Now we are ready to read the values from the pins.
Arduino
1
2
3
4
5
6
7
8
while(1):# Create an infinite loop
ifGPIO.input(button1)==0:# button1 will report 0 if it is pressed
print"Button 1 Pressed"
sleep(.1)# delay
ifGPIO.input(button2)==0:# button 2 will report 0 if it is presses
sleep(.1)
print"Button 2 Pressed"
Notice that we read from the pin using the GPIO.input command. Also note that for reliable results you need to usually put a small delay in your code. This will help debounce the button, and will also give more stable results.
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.
Servo Connected to Raspberry Pi GPIO Pins
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:
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.
Making The World a Better Place One High Tech Project at a Time. Enjoy!
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok