There’s a lot of excitement out there for the use of embedded Linux platforms in hobbyist
electronics projects and it’s no wonder.
With all the support out there for Linux, it makes doing some pretty complex things
pretty easy.
I’m going to show you how to get started with embedded Linux using one of these platforms,
the BeagleBone.
Many people who are getting started with microcontrollers like the Arduino start off by by making an
LED blink.
We’re going to do the same thing with the BeagleBone.
So let's get started.
On the included MicroSD card is the Angstrom distribution of Linux, which is customized
for the BeagleBone hardware.
Since this is a relatively new platform, you may want to go beagleboard.org/bone to make
sure you have the latest build of the distro and update as necessary.
You can work with your BeagleBone by plugging it into your computer via USB, ejecting its
disk image and connecting to the network interface that will appear.
Or you can plug your BeagleBone into your ethernet network, power it up, and find its
IP through your router settings.
I’ll do it that way.
Open up your terminal app and use SSH to connect to your BeagleBone’s IP as the user root.
Type "yes" when it asks if you want to continue connecting, which will only happen the first
time you connect.
There’s no password by default.
Now you’re logged in.
This is the command prompt for the BeagleBone.
Let’s hook up our LED now.
There are two sets of headers on the BeagleBone, labeled P8 and P9 and the pins are numbered
this way.
You’ll have to refer to the BeagleBone’s System Reference Manual for the default configuration
for these pins.
Keep in mind, that this configuration can be changed.
Look up "pinmuxing" for more information.
I’m going to connect the cathode side of an LED to ground and the anode side, through
a 220 ohm resistor, to pin 3 on header P8.
If you check the System Reference Manual this is gpio1_6.
The BeagleBone refers to each pin with a chip number followed by a pin number, up to 32
pins for each chip, labeled 0 through 31.
The first number refers to the GPIO chip, the second refers to the number of that pin.
To find the number we’ll be using to reference this pin in linux, multiply the chip number
by 32 and add the second number.
So for gpio1_6, we’ll be referring to this pin as 38.
One way to control these pins in Linux is through the file system, by reading and writing
special files.
Now, this makes it really easy to control the pins using any programming language, but
for now, we're just going to use the command line to control the pins.
First we have to “export” the pin we want to use.
This sets up the file system for that pin.
You type "echo 38 > /sys/class/gpio/export"
Type echo out to that pin’s direction file to set wether it’s an input or an output.
Then type echo 1 to that pin’s value file.
If the LED lights up, you know you’ve got it right.
Type echo 0 to that pin’s value file to set it low.
Be sure to also unexport the pin when you’re done with it.
Type echo 38 to /sys/class/gpio/unexport
Because of the flexibility of Linux, we can program in many different programming languages.
Now, I for one, had the most success programming in Python, but you could program in C, C++,
perl, you could use bash scripting, whatever you’re comfortable with.
Anyway, let’s move on and get this LED blinking.
In order to make it easier to access the pins, I wrote a simple Python module to handle the
files behind the scenes so that we can use a simple Arduino-like syntax for reading and
writing to them.
I’ll put a link to this module and the sample code in the description.
Create a new directory in your home directory by typing "mkdir ~/blinkLed" and then change
to that directory by typing "cd ~/blinkLED".
Download the Python module by using wget with the address of the file.
Create a new file by typing "nano blinkLed.py".
This will be our script.
First, let's import the module by typing "from mrbbio import *".
Then type "def setup():" This will run once at the beginning of our script.
on the next line, indent and then type pinMode P8.3 comma output, like this.
Keep in mind that unlike C, indenting your code does matter in Python.
On the next line, type "def loop():".
This is the code that will run repeatedly until we terminate the script.
Indent and type digitalWrite P8.3 comma HIGH to turn the LED on.
On the next line, type "delay(1000)" to wait for 1 second.
On the next line, type digitalWrite P8.3 comma LOW to turn the LED off.
And add another delay of 1,000 miliseconds.
At the end, type "run(setup, loop)", which tells the module to run the setup function
once, and the loop function repeatedly until the script terminates.
Type control+X to quit, be sure to say yes to save.
Back at the command prompt, type "python blinkled.py" and you should have a blinking LED.
To stop the script, type control+C and it will take care of unexporting the pins for
you.
Of course, using a Linux computer to blink an LED is definitely overkill, but think about
the possibilities.
You could have it run a dynamic web server, have a remote data logging project, or maybe
make a custom webcam application?
Whatever it is you do, be sure to let us know!
So long!