Now that I had my BeagleBone Black setup as documented in my first post, I wanted to start building ROBOTS. This is where I wasted a lot of time and money because I really did not understand how the BeagleBone Black worked or what I really needed. So rather than diving right into building robots, lets spend a little time trying to understand how we can program the BeagleBone Black to control an external component like an LED (Light-Emitting Diode).
For the experiments in this post you will need a breadboard, an LED, three solder-less jumper cables and one 100 ohm resister. All of these parts come with Make’s Getting startedwith the BeagleBone Black kit that I mention in my Introduction post. The 100 ohm resister is the resister with the Brown-Black-Brown-(gold or silver) bands. If you want to find out the value of your resistors but you are not familiar with the resistor color codes, I would recommend using this digikeypage.
In this section we will take a look at our BeagleBone Black and explain how we would connect external components to it. If you look at this image from the BeagleBone Black’s documentation, you will see that our BeagleBoard Black has two expansion headers, they are the black connection strips on either side of the board.
If you look closely at the board itself, you will see that each expansion header is labeled with its name (P8 or P9). You will also notice that one end of the expansion header is labeled with a 1 and a 2 while the other end is labeled with a 45 and 46. These labels represent the pin numbers of the headers.
Now lets look at the Cape expansion headers image that is also from the BeagleBone Black documentation. This image shows you how each pin is labeled and also which pins connect to power and ground.
For our first LED projects, we will want to use one of the digital I/O pins because all we want to do is to turn the LED on or off. This next image, once again from the BeagleBone Black documentation, shows the possible digital I/O pins.
For our first LED experiment we will be using pins 1 and 3 on expansion header P9 to supply the ground and power respectively to our breadboard. We will also use pin 8 (labeled GPIO_67) on the P8 expansion header for our digital I/O that will control the LED.
Now lets connect our external components to our breadboard. Whenever you connect items to your BeagleBone black, it is a very good idea to disconnect the power first.
Using a solder-less jumper, connect the ground rail of your breadboard to pin 1 of the P9 expansion header. Then use another solder-less jumper to connect the power rail of your breadboard to pin 3 of the P9 expansion header. This will connect your breadboard to both power and ground.
Now lets add the LED to our breadboard. Connect the cathode end of the LED (shorter wire) to the ground rail of our breadboard and then connect the anode end of the LED (longer wire) to one of the rows on our breadboard.
Now take the 100 OHM resistor and connect one end to the row on the breadboard that the LED is connected to and the other end of the resistor to another row on the breadboard. Finally run a solder-less jumper from pin 8 of the P8 expansion header to the row that the 100 OHM resistor is connected too.
The connections should look similar to the following image but hopefully you were neater hooking up the wires then I was.
Power up and writing code:
I would recommend creating a directory to keep your python scripts in, this way they are not scattered throughout your system. In your scripts directory create a file called “ledon.py” and add the following code:
import Adafruit_BBIO.GPIO as GPIO
Now run the script with this command:
If everything is setup correctly, the led will light up for five seconds and then shutoff. Lets take a look at how this short script works. We start off by importing the Adafruit_BBIO.GPIO and time libraries that are needed. The next line sets up pin 8 on expansion header P8. GPIO.OUT defines that we want to write to this pin (output data). If we wanted to read the pin (as if we had a button connected), we would use GPIO.IN like this: GPIO.setup(“P8_8”,GPIO.IN). We then set pin 8 to GPIO.HIGH, which turns the LED on (GPIO.LOW would turn the LED off). Finally we wait five seconds and the script then ends which resets the pin turning the LED off.
If we set pin 8 to GPIO.HIGH to turn it on, then setting the pin to GPIO.LOW should turn the LED off. Using this assumption lets create a script that will cause the LED to blink on and off.
import Adafruit_BBIO.GPIO as GPIO
If you save and run this script, your LED should blink on and off indefinitely until you stop the script. From here you can experiment some more with your LED. I am wondering if I can use something like this next winter to control my Christmas lights. Maybe turn certain lights on or off at various time intervals.
Turning a LED on and off is a pretty cool first step but how about having the LED slowly get brighter. Can we do this? Yes we can. We can use one of the PWM pins for analog writes. The PWM pins are shown in this image that also comes with the BeagleBone Black documentation.
Power down your BeagleBone black and move the jumper from pin 8 of the P8 expansion header to pin 13. Then run this script:
import Adafruit_BBIO.PWM as PWM
for i in range(0, 100):
Your LED will slowly get brighter until it reaches full brightness and then shuts off when the script ends.
In my next post, I will show how we can read the state of a button with python and the Adafruit_BBIO library. In the mean time have fun playing with the LED.