Introducing Digital Electronics with the BeagleBone - Covering the Basics - BeagleBone For Dummies (2015)

BeagleBone For Dummies (2015)

Part II

Covering the Basics

Chapter 6

Introducing Digital Electronics with the BeagleBone

In This Chapter

Finding out about the tools of the trade for electronic projects

Discovering several electrical components for your circuits

Getting acquainted with the GPIO pins

Controlling the BeagleBone’s Output and Input pins by writing into and reading from files

Figuring out what PWM is and how you can employ it using the BeagleBone

Augmenting the BeagleBone’s capabilities with capes

This chapter is where working with the BeagleBone starts to get cool. If you’ve worked through the chapters consecutively, you understand how to use Linux and play around with the command-line prompt, and you have a grasp of the basics of electronics. For the remainder of this book, you use this combined knowledge to unlock the BeagleBone’s full potential.

Besides the outstanding computational power that the BeagleBone offers, it also boasts many capabilities for interfacing with electrical devices and components. There are two sets of headers along its edges that host 46 pins each, allowing you to easily — yet awesomely — create electronics projects. All these pins apparently are the same, but they can be programmed to do a wide range of tasks. Reading from sensors, lighting up light-emitting diodes (LEDs), and wiring up motors are some of the possible applications. This chapter introduces you to the various tools necessary to start building electronic circuits; explains the general purpose input/output (GPIO) mode of the BeagleBone’s pins; illustrates how to use these pins in GPIO mode; and explores the use of capes, which are plug-in boards that expand the BeagleBone’s capabilities.

Looking for the Right Tools

This section describes the most important tools you need to start creating prototypes of your projects. After you have a diagram and the necessary components, you need to know how to connect the whole thing — easy to do with a breadboard, jumper wires, and a pair of needle-nose pliers.


Simply put, the breadboard (see Figure 6-1) is what allows you to create prototypes so that you can test and experiment with them without making permanent connections that you can’t undo. With a breadboard, you can reuse the components of your circuit without any kind of setback.

Figure 6-1: Breadboard.

What makes the breadboard so special? Beneath all the holes in it are copper tracks (see Figure 6-2), which enable you to create metallic connections for current to pass through without committing to more permanent solutions (such as soldering).

There are many sizes and types of breadboards, but the concept is always the same: If you plug a wire into one of the holes in a breadboard, you connect the wire to a copper track that keeps all the holes in its row connected.

The copper tracks are normally covered by a plastic coating. If you’re looking at a breadboard and can’t see the copper, trust us when we say that it would look the same as Figure 6-2. Don’t try removing the plastic coating to check it out for yourself!

Figure 6-2: The copper tracks of a breadboard.

To better understand the connections within the board, refer to Figure 6-1. On the long sides of the board are two lines of holes separated from the central holes. On most boards, these holes are delimited by a red (+) and a blue (-) line, which suggests that you use them as a positive power source (+) and a negative power source (-), respectively. The copper tracks beneath those holes are horizontal and are broken at the center. If you connect the positive side of a 9V battery to one of those tracks, any electrical component leading to the same track will be fed with 9V.

Some breadboards don’t have the red and blue lines, whereas others don’t exhibit the break at the center of the horizontal lines, but their layout is still exactly the same as in Figure 6-1.

At the center of the breadboard are shorter lines in parallel with the short side of the board. The tracks there are vertical and are also broken at the center. For components with multiple legs, such as integrated circuits (IC) and pushbuttons, this trench in the center prevents the legs on one side from connecting to the other. (Some tiny breadboards have only the vertical copper tracks.)

In a sense, the horizontal and vertical lines are the same things — copper tracks — so you can connect things however you want, such as connecting the battery to a vertical line. Conventions exist for a reason, however; they’re often guidelines to the easiest way to accomplish a task and also ways to keep you organized.

Thus, we advise you to follow this convention: Horizontal is for power and ground; vertical is for everything else.

Figure 6-3 shows how to connect a circuit on a breadboard by using a 9V battery, a pushbutton, a 470Ω resistor, and a light-emitting diode (LED).

Figure 6-3: A pushbutton circuit with an LED and a 9V battery.

Components in the world of electronics are very, very tiny. Using needle-nose pliers (see Figure 6-4) to plug your resistors, LEDs and ICs into your breadboard can save you lots of time and protect your sanity, since using human hands alone often becomes a headache — especially as the breadboard becomes clustered.

Figure 6-4: Needle-nose pliers.

For a typical LED, 220Ω to 470Ω usually is good enough to protect the LED from burning up without reducing its brightness too much, even if different LEDs differ slightly in terms of necessary current and voltage. Naturally, you can also calculate the necessary resistor value for a LED using Ohm’s Law (see Chapter 5), but there’s no need to do so for most LEDs.

Jumper wires

Jumper wires (which are also known as jump wires or simply jumpers) are usually used with a breadboard because they’re easy to plug into holes. These wires consist of copper, an insulator so that the electric signal is protected, and a connector. Figure 6-5 shows a collection of jumper wires.

Figure 6-5: An assortment of jumper wires.

Depending on the type of connector on the ends of the wires, jumper wires can be female/female, female/male, or male/male. Male connectors are exposed, unshielded electrical terminals that can be easily inserted into a receptacle, such as the BeagleBone, the breadboard, or a female connector to ensure a robust electrical connection. Since you will be prototyping on a breadboard, for the remainder of this book only male-male jumper wires are necessary.

Jumper wires are definitely the easiest way to establish communication among your LEDs, sensors, resistors, and other electrical components and the BeagleBone. There’s no need to cut, trim, or bend them; they’re ready to use from the moment you get them!

There are several more tools used in circuit design, but there isn’t space in this book to cover them all. The tools presented in this chapter are all that you need to start having some fun with the BeagleBone. You can read about several other tools that are useful for an electronics enthusiast at

Essential Components and Parts

Although thousands upon thousands of electrical components make up many projects, a few could be called essential because of the frequency at which they’re employed in circuits. They’re also good candidates for playing around with on the BeagleBone and learning how to properly use the board, and we recommend them that you get them as soon as possible. You can see plenty of them in Figure 6-6.

Figure 6-6: Essential components for understanding basic electronics.

To get through the remainder of this book, you should have these components:

· Resistors: Resistors are extremely cheap, and you can buy lots of them at a time. The values you’ll use the most are 220Ω , 470Ω, 1k Ω, and 10k Ω, but buying an assorted pack of plenty of resistor values is definitely the best idea.

· LEDs: Get a few of them, in various colors!

· RGB LEDs: These LEDs differ slightly from the previous ones due to the fact that their colors can vary.

· Pushbuttons: Pushbuttons are the best way to add controllability to your projects. You use them for when you want stuff to happen only when you press the button.

· Switches: Similarly to pushbuttons, switches are used to control a circuit’s behavior. Unlike pushbuttons, the switch stays in the same position until you toggle it again, whereas you have to keep pressing a pushbutton to engage it.

· Buzzers: Buzzers are very simple electronic devices that output sound when a voltage is applied to them.

· 9V battery adapter: For projects where you need to use a 9V battery, you require an adapter to connect the battery’s poles to a breadboard.

· Variable resistors (potentiometers): We recommend that you get at least a 2K and a 10K potentiometer.

· Servomotor: Servomotors are usually tiny, slow-rotating motors that require a small amount of current to do their jobs, which makes them ideal to use with development boards such as the BeagleBone.

· Servo horn: Servo horns are small plastic components that are attached to your servomotor to suit different needs.

Although you can use other kinds of motors with the BeagleBone, such as DC motors, they require external circuitry to amplify the low current that’s drawn from the BeagleBone’s pins.

Most online electronics shops feature starter kits, which may be the easiest way to get launched into circuit design. Purchasing a starter kit usually ensures that you get a good deal, because buying all the components separately is more expensive. You can read more about starter kits in Chapter 18.

The components we have just talked about are essential to get started, and will be the basic building blocks of many of your circuits. This book features projects that use some more advanced components that are used for specific applications — we will tell you to get these when the time comes.

Getting to Know the GPIO

GPIO stands for general purpose input/output, which sums up what pins in this mode can do quite well: They can be either inputs or outputs for the vast majority of applications.

In the digital world of electronics, electricity happens in a binary way. In that sense, GPIO pins are either HIGH or LOW. In HIGH state, the pin is connected to 3.3 volts (V). In LOW state, the pin is connected to ground.

Saying that a pin is HIGH or LOW is equivalent to saying that it’s on or off. In computer science, these two states are often called true and false or 1 and 0. You use these designations to control the GPIOs. HIGH and LOW, dear reader, sum up the world of digital electronics. Whatever you do in your computer is translated into a set of HIGH and LOW voltages along its hardware to carry out the task you requested. It’s beautiful how such complex systems can be designed through such a simple concept. The following sections give you a glimpse of what you can do with just HIGH and LOW.

Understanding How GPIOs Work

In Chapter 4, we say that for the Linux operating system (OS), everything is a file. We weren’t exaggerating with the term everything: files are also how you control the GPIOs: by reading from and writing into files in the BeagleBone’s file system.

Chapters 7 through 11 show you how to control the GPIOs in a simpler yet more abstract way by using libraries for the BoneScript and Python programming languages. But it’s important, useful, and insightful to understand how to control the GPIOs via a more direct approach: manipulating those files directly from the command-line prompt.

If you’re acquainted with programming, you should know that you can control the GPIOs with any language that allows you to open, read, and write into files, such as C.

To work along with the examples in the following sections, make sure that you have the following items on hand:

· Breadboard

· Jumper wires

· 220 Ω or 470 Ω and 10K Ω resistors

· Pushbutton or toggle switch

· LEDs

Viewing the GPIO pins layout

Figure 6-7 shows the default GPIO pins. Besides those pins, the figure shows eight ground (GND) pins, two 3.3V pins, and four 5V pins; virtually, you can use these pins as power sources. Other pins labeled in the figure are useful in later chapters.

Figure 6-7: The layout of the GPIO pins on the BeagleBone.

The pins that aren’t labeled in the figure are, by default, in a mode different from GPIO. Some of those pins can be put in GPIO mode, up to a total of 65 possible digital inputs and outputs. Doing so isn’t really necessary, though, as there are already quite a lot of pins set in GPIO mode by default.

The BeagleBone’s pins are rated at 3.3V, which means that connecting 5V to them could be extremely hazardous to your board — to the point of blowing up the processor and permanently ruining the board. You need to be extremely careful whenever you need 5V for some components in your circuit and make sure that 5V never reaches any of the BeagleBone’s pins.

If you’re curious about the complete capabilities of the expansive headers, visit for more information.

Setting GPIOs as Outputs

Often, the best way to get acquainted with a new working platform is to light up and turn off an LED on command. To do so, you set a GPIO as output; you want to control the state of a component.

Wiring the circuit for an LED

First, you set up the circuitry. Use the following steps to wire your circuit as shown in Figure 6-8. For the locations of the pins used in the following steps, refer to Figure 6-7.

1. Turn off the BeagleBone.

Before plugging things into the BeagleBone, it’s generally a good idea to shut it down (as described in Chapter 4) and remove the power source from it.

2. Power up the breadboard.

Using a jumper wire, connect the BeagleBone’s 3.3V source — pins 3 or 4 on header P9 — to the breadboard’s positive track.

3. Set up your ground.

Connect the BeagleBone’s GND pin — for example, pins 1 and 2 on both headers — to the breadboard’s negative track.

4. Connect a GPIO pin to the board.

This example uses GPIO 40 — pin 14 on the P9 header. Use a jumper to connect it to a vertical row on your breadboard.

5. Connect a resistor.

Without a resistor, an LED burns up easily. A 220 Ω or 470 Ω resistor should drop enough voltage without reducing the LED’s brightness too much. Connect the resistor to the jumper you pulled from pin 14, effectively connecting the resistor to GPIO 40.

6. Connect the LED.

Connect the LED’s negative leg — the cathode, which is usually the shorter leg — to the breadboard’s negative track where you connected ground in Step 3. Connect the positive leg — the anode — to the resistor.

The circuit you’ve just built is similar to the one used in Chapter 5 to explain the concepts of circuit design. There’s one small difference, though: The power comes from GPIO 40 rather than a battery, which you turn on and off by writing into the command prompt.

Adafruit and Fritzing

Throughout this book, you see circuits similar to the one in Figure 6-8. They were drawn using Adafruit’s Fritzing library. Fritzing is an open source software that makes it easy to draw circuits. Adafruit is a company that sells electronics kits and components. Adafruit is also well-known for creating learning guides and contributing to open source projects, and Adafruit designed the BeagleBone Black part for the Fritzing software.

Figure 6-8: Wiring up an LED on a breadboard.

In Step 2, you connect the BeagleBone’s 3.3V pin to the breadboard. In reality, for this specific project, making that connection serves no purpose. It’s generally good practice, however, to always have the horizontal tracks on your breadboard powered with a constant voltage and with a circuit ground. If you were to connect the resistor to the positive rail on your breadboard, the LED would light up, but you’d have no control over it. Feel free to try it out!

Controlling the GPIO

Because pin 14 is already a GPIO pin by default, you can set it as output. After you’ve done that, you can easily control whether you want the LED to be on or off by setting the pin to HIGH or LOW, respectively.

You need to be logged in as the root user to access the GPIOs. If you’re currently logged in as debian, you can easily change to root as follows:

debian@beaglebone:~$ sudo su

On the command line, after connecting to your BeagleBone (using your preferred method, as described in Chapter 2), change to the gpio directory with the following command:

root@beaglebone:~# cd /sys/class/gpio

If you list the contents of this directory, you can see that gpio40 isn’t there:

root@beaglebone:/sys/class/gpio# ls
export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

You have to export it first by writing in the export file, which creates a folder containing files that can be altered to control the pin’s state. On the command line, type the following:

root@beaglebone:/sys/class/gpio# echo 40 > export
root@beaglebone:/sys/class/gpio# ls
export gpio40 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

To control the pin’s state, change to the newly created gpio40 directory:

root@beaglebone:/sys/class/gpio# cd gpio40
root@beaglebone:/sys/class/gpio/gpio40# ls
active_LOW direction edge power subsystem uevent value

The direction file defines whether this GPIO pin functions as an input or output pin. Because you want to control its state by writing into it, your pin is supposed to be an output:

root@beaglebone:/sys/class/gpio/gpio40# echo out > direction

The value file holds the value of the GPIO: HIGH (1) or LOW (0). Thus, to turn the LED on, enter the following command:

root@beaglebone:/sys/class/gpio/gpio40# echo 1 > value

Now your LED should be on, as shown in Figure 6-9.

Figure 6-9: You turn on the LED by typing in files in the BeagleBone’s file system.

If the LED seems dim, try a lower resistance value. We recommend that you not go lower than 220Ω, though.

To turn it off, use the following command:

root@beaglebone:/sys/class/gpio/gpio40# echo 0 > value

When you’re done with a pin, it’s often a good idea to unexport it so that it becomes available for different purposes. You unexport the pin by writing into the unexport file. The following succession of commands unexports gpio40 and shows that its directory has been eliminated.

root@beaglebone:/sys/class/gpio/gpio40# cd ..
root@beaglebone:/sys/class/gpio# ls
export gpio40 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport
root@beaglebone:/sys/class/gpio# echo 40 > unexport
root@beaglebone:/sys/class/gpio# ls
export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

If you’re successful in controlling the LED, you may have just taken your first big step into digital electronics. Although lighting up an LED may not seem like much, the concept behind it is pretty much the same as controlling a motor, a buzzer, or an LCD screen!

Feel free to play around with the other GPIOs shown in Figure 6-7 earlier in this chapter.

Setting GPIOs as Inputs

The procedure for setting a GPIO pin as input is very similar to the technique in the “Setting GPIOs as outputs” section earlier in this chapter. The key difference is that you read from the value file rather than writing in it. To verify this difference, you need to build the circuit shown in Figure 6-9. Grab a pushbutton and follow these steps:

Wiring the circuit for a pushbutton

To wire up a pushbutton to the BeagleBone, follow these steps and refer to the diagram for the circuit in Figure 6-10. For the locations of the pins referred to in the following steps, refer to Figure 6-7 earlier in this chapter.

If you still have the circuit from the preceding example, you can skip Step 2.

1. Turn off the BeagleBone.

Remove the power source and shut down the BeagleBoard through the command line, as described in Chapter 4.

2. Power up the breadboard, and set up the ground.

Using jumper wires, you can connect pin 1 or 2 of either header to the negative track — these pins provide ground — and connect pin 3 or 4 of header P9 to the positive track — these provide 3.3V.

The BeagleBone pins aren’t 5V tolerant! Be careful to connect pin 3 or 4 of header P9, not pins 5, 6, 7, and 8.

3. Connect a GPIO pin to the breadboard.

You can use any of the pins that are labeled as GPIOs. This example uses GPIO 45, which is pin 11 of header P8. Connect it to a vertical row on the breadboard through the use of a jumper.

4. Place your pushbutton on the breadboard.

If you’re using a pushbutton, you should place it at the center of the breadboard to separate the pairs of legs.

5. Connect one of the pushbutton’s legs to the positive rail.

Use a jumper to establish this connection.

6. Connect the other leg to the input pin.

Connect it to the jumper that comes from the BeagleBone pin of the GPIO that you are using — pin 11 of header P8 in this example.

7. Connect a pull-down resistor.

A pull-down resistor is a resistor used to prevent the existence of a short circuit when the pushbutton is closed. Connect it from the ground track to the leg of the pushbutton that connects to the input pin. A 10K Ω resistor should do the job.

When the pushbutton is closed (which is the same as saying that the pushbutton is pressed), having a pull-down resistor makes the current — which follows the path of least resistance, according to Ohm’s Law — go to the input pin rather than to the ground. Thus, there is a voltage reading at the input pin.

Figure 6-10: Wiring up a pushbutton on the breadboard.

Controlling the GPIO

When you’re done with the circuitry, you can move on to the command line. Start by exporting the pin that you use as an input by typing the following commands:

root@beaglebone:/sys/class/gpio# echo 45 > export
root@beaglebone:/sys/class/gpio# ls
export gpio45 gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

Then set the pin as input by writing into the direction file:

root@beaglebone:/sys/class/gpio# cd gpio45
root@beaglebone:/sys/class/gpio/gpio45# echo in > direction

If the button isn’t pressed, the 3.3V from the positive track of your breadboard has no way of reaching the GPIO pin, so if you read from the value file, you should get 0 because the pin is connected to ground:

root@beaglebone:/sys/class/gpio/gpio45# cat value

If you’re pressing the button, however, the value file should hold 1, indicating that the pin is in the HIGH state:

root@beaglebone:/sys/class/gpio/gpio45# cat value

Unexport the pin when you’re done:

root@beaglebone:/sys/class/gpio/gpio45# cd ..
root@beaglebone:/sys/class/gpio# echo 45 > unexport
root@beaglebone:/sys/class/gpio# ls
export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport

By itself, reading the state of a button (pressed or not pressed, HIGH or LOW) doesn’t sound very exciting. Throughout this book, though, you use input pins to control outputs. You instruct your BeagleBone to read from a pin and then follow directions such as “If it’s HIGH, do <something>; if it’s LOW, do <something else>.”

Setting GPIOs as PWM

Digital electronics are weird. How can everything be programmed with a fixed binary (HIGH and LOW) set of values? Suppose that you want to create a circuit to sound a buzzer. Are you stuck with either an ear-deafening sound or no sound at all? Is there no middle level?

Scientifically speaking, the answer is “Nope, no middle level.” Your voltage output is always either 3.3V or 0V, and that’s what you’re stuck with. In reality, though, techniques are available that enable you to produce “fake” mid-level voltages. That’s where pulse-width modulation (PWM) comes in.

PWM is a digital electronics technique that relies on the fact that instantaneity is a lie. There are no instantaneous phenomena in life — only very, very fast phenomena.

Practically speaking, if you alternate an LED’s voltage between LOW (0V) and HIGH (3.3V) very, very fast, the human eye — which is a slower system than the rate of this change — won’t be able to keep up with the speed at which the LED goes on and off. Instead, it sees some mid-level brightness.

The same applies to buzzers and servo motors: If you set the frequency at which the PWM changes from LOW to HIGH to very high, they won’t be able to keep up with this rate of change. Because their speed/volume doesn’t change instantaneously according to the voltage, they instead sound or rotate at a level somewhere in the middle.

PWM is a way to create a fake analog output from your BeagleBone. A good analogy to distinguish analog from digital is to think of lamps. A lamp that simply allows you to turn it on or off is a digital system; a lamp that features a brightness-selection dial is an analog system.

You have ways to produce real analog outputs from the BeagleBone, but doing so requires external components called digital-to-analog converters (DACs), which are seldom necessary except for specific applications. Analog inputs are often needed for projects, however, so the BeagleBone features internal analog-to-digital converters (ADCs), which we cover in Chapter 9.

Specifically, when you use PWM, you’re doing several things (see Figure 6-11):

· Generating a square wave that changes between LOW and HIGH at a very high frequency. Its period — the time it takes until it repeats itself — is very short.

· Setting its duty cycle. The duty cycle is the fraction of the period at which the pin stays HIGH. A duty cycle of 50 percent results in an LED at 50 percent brightness.

Figure 6-11: How PWM works.

Wiring the circuit for a brightness-regulated LED

To illustrate how you can set a GPIO as PWM, grab an LED, a resistor and a few jumpers, and build the same circuit shown in the section “Setting GPIOs as Outputs” and Figure 6-8 earlier in this chapter. The circuit is the same, but pin 14 on header P9 is configured quite differently.

Controlling the GPIO

On the command line, follow these steps:

1. Activate pwm:

root@beaglebone:~# echo am33xx_pwm > /sys/devices/bone_capemgr.9/slots

Your bone_capemgr.# directory may have a number different from 9. Check ahead by using ls, or simply press Tab after typing bone to autocomplete the directory name.

2. Set P9_14 as pwm:

root@beaglebone:~# echo bone_pwm_P9_14 > /sys/devices/bone_capemgr.9/slots

As in the earlier examples, you can access a directory with a couple of files that you can write in so you can control pin P9_14.

3. Change to the /sys/devices/ocp.3/pwm_test_P9_14.15 directory and list it:

root@beaglebone:~# cd /sys/devices/ocp.3/pwm_test_P9_14.15
root@beaglebone:/sys/devices/ocp.3/pwm_test_P9_14.15# ls
driver duty modalias period polarity power run subsystem uevent

The ocp.# folder may also have a different number, and pwm_test_P9_14.15 may or may not have.15. Again, autocompletion is your friend: Simply press Tab after typing ocp and after typing pwm.

4. Change the polarity to 0:

echo 0 > polarity

Having the polarity set to 1 implies thinking in reverse. A higher duty cycle means lower voltage (brightness for the LED’s case), which is why it’s preferable to set it to 0. It’s more intuitive: A higher duty cycle means higher voltage.

5. Define the period in nanoseconds:

echo 1000 > period

6. Define the duty cycle in nanoseconds:

echo 500 > duty

Play around with different periods/duty cycles to get the gist of using GPIOs as PWM! In the values used as examples, the LED should be at 50 percent brightness. Try changing the duty cycle to other values, and notice how the brightness of the LED changes:

echo 250 > duty
echo 100 > duty
echo 750 > duty
echo 900 > duty

The values of period and duty by themselves don’t hold much meaning. What matters is the relative proportion of the duty toward the period. A period of 1,000 nanoseconds with a duty cycle of 500 nanoseconds would have the same brightness as a 2,000 period and 1,000 duty. The duty cycle is 50 percent of the period in both cases.

The PWM must always change between HIGH and LOW much faster than the system does. Thus, the value you set for period depends on the component you’re using. Checking the component’s datasheet can often help in this regard. If you can’t find any information, though, you can always try different values until you get the right one.

Trying out PWM with a buzzer and a servomotor

You can experiment with PWM by using a servo motor and/or a buzzer if you have one handy. The wiring of the buzzer is very similar to that of the LED in Figure 6-8, but it doesn’t require a resistor (see Figure 6-12).

Figure 6-12: Wiring a buzzer to control it with PWM.

The wiring of the servomotor is similar, but the servomotor has two wires that need to be connected to 3.3V (red) and ground (black), and the middle wire (usually yellow or orange) should be connected to the digital pin. You can use pin P9_3 or P9_4 as a constant 3.3V DC source. Figure 6-13 shows the circuit.

Figure 6-13: Wiring a servo to control it with PWM.

Play around with these three circuits by trying different period and duty values!

Creating shell scripts

A shell script is a way to memorize several lines of commands, in a sequential fashion, and use them whenever you please. Consider the example of lighting an LED. The full list of commands, from exporting to writing into the value of the GPIO, is as follows:

echo 40 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio40/direction
echo 1 > /sys/class/gpio/gpio40/value

You can simply put all these commands into a shell script that you can use for future reference. Using nano, create a .sh file as follows:

root@beaglebone:~# nano

To create a shell script, all you have to do is write the lines of commands, in the correct sequence, just as you did before. The first line should always be #!/bin/bash. Simply write this code in the nano text editor:

echo 40 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio40/direction
echo 1 > /sys/class/gpio/gpio40/value

Close nano by pressing Ctrl+X and then press Y to save. Your shell script is created! It isn’t ready to use yet, however, because it doesn’t have execute permission. You can change that situation with the chmod command:

root@beaglebone:~# chmod +x

Now you can go through the entire process of exporting gpio40, setting it as output, and turning it on by typing the following:

root@beaglebone:~# ./

The first line of a script is called a shebang and always starts as #!. If you don’t include this line in the script, you can still run it by typing bash <>. ./<>, however, won’t work.

You can create shell scripts for whatever you want. Feel free to experiment!

Adding Capes to the BeagleBone

Capes are plug-in boards that you can plug on top of the BeagleBone computer to add extended capabilities to it in a simple manner. Capes give you quick access to some easy-to-use systems such as LCD screens, GPS modules, and motor controllers. Some of these are introduced in this section; if you’re interested in checking out the enormous list of capes, visit

Some capes aren’t compatible with both the Original BeagleBone and the BeagleBone Black. Always check ahead or ask the manufacturer before making a purchase.

BeagleBone Proto Cape

The BeagleBone Proto Cape is a simple cape that fits atop your BeagleBone. It contains through-hold solder points and two 46-pin headers that connect directly to the BeagleBone. Its simplicity and accessibility make it a great workspace for creating prototypes after the breadboard phase.

Some Proto Capes, such as the ones from Tigal KG (see Figure 6-14), also exhibit extra useful circuitry such as LEDs and switches. Visit for more information.

Photo courtesy of Tigal

Figure 6-14: BeagleBone Proto Cape from Tigal.

BeagleBone Power Cape

You can use the Power Cape to supply power in a flexible way. When the Power Cape is plugged in to your BeagleBone, if the BeagleBone isn’t being fed by any other source (such as an USB cable), the Power Cape supplies the necessary power through its lithium battery. Conversely, if 4.5V or higher DC power is available from other sources, the lithium battery recharges, even if the BeagleBone is off.

The Power Cape provides two interesting features:

· An onboard power monitor (INA219), which allows you to monitor the voltage of the battery and the current it supplies.

· A scheduling feature that enables you to designate when to turn your BeagleBone on. You can schedule your BeagleBone to start feeding off the battery at a desired time, which may be interesting for automation projects.

You can visit for more information.

BeagleBone Motor Cape

Motor Capes make the whole deal of using motors much easier and straightforward. Because the BeagleBone’s GPIO pins can drive a current of only 6 milliamps (mA) maximum, they don’t have the kick to get powerful motors running without some external help.

By using a Motor Cape, you can drive up to eight DC brush motors going at 500 mA per motor — quite the power-up!

The vendors of this cape sell it with NXT connectors or screw terminal blocks. The latter type usually are the easiest to work with in most standard electrical projects.

Visit for more information.

BeagleBone mikroBUS Cape

The BeagleBone mikroBUS Cape (see Figure 6-15) is a plug-in board that enables you to connect up to four MikroElektronika Click Boards to the BeagleBone in the easiest way possible.

Photo courtesy of Tigal

Figure 6-15: BeagleBone mikroBUS Cape.

Click Boards are tiny plug-and-play devices that require minimal settings. The idea is simple: Create small boards that have the same pinout standard, which allows boards with completely different functionalities to be connected in the same fashion. A microSD slot, an MP3 board, and a Bluetooth module are all connected in the same way, and all of them can easily fit on the BeagleBone mikroBUS cape.

Currently, more than 70 Click Boards are available, and the list keeps growing. Visit for more information on this new concept.

BeagleBone GPS/GPRS Cape

As its name suggests, this cape adds GPS (Global Positioning System) and GPRS (General Packet Radio Service) functionality to the BeagleBone, allowing you to track your position and use the GPRS for straightforward machine-to-machine (M2M) communication.

It’s a good idea to buy an external antenna with this cape, because the board doesn’t feature one. Visit the manufacturer’s page at for more information.

BeagleBone LCD Cape

Plenty of LCD capes are available for the BeagleBone. These caps sit nicely atop your board while adding touchscreen capability to it. Resolution varies from 480x272 pixels to 800x480 pixels. Figure 6-16 shows one such cape.

With this cape, you can interact directly with the BeagleBone through touch and through the buttons located in positions that enable easy use. The cape also feature expansion connectors so that you can easily access the GPIO that are still free after the cape has been plugged in.

Visit for more information.

Photo courtesy of Tigal

Figure 6-16: BeagleBone Black 5.0 Display Cape from Chipsee.

BeagleBone HD Camera Cape

RadiumBoard’s HD Camera Cape provides an easy way to use your BeagleBone Black with a high-resolution mobile camera. This cape is divided into two parts: an expansion board that manages the data and a board with the necessary sensors for imaging and optics.

This cape isn’t compatible with the Original BeagleBone!

If you’re a camera enthusiast, a rundown of the cape’s key features may be important for you. Visit the following link for the details: