Book Image

BeagleBone Essentials

By : Rodolfo Giometti
Book Image

BeagleBone Essentials

By: Rodolfo Giometti

Overview of this book

Table of Contents (18 chapters)
BeagleBone Essentials
About the Author
About the Reviewers


The BeagleBone Black is an embedded system that is able to run an embedded GNU/Linux distribution as well as a normal (and powerful) distribution, such as Debian or Ubuntu, and where the user can connect several external peripherals to it via two dedicated expansion connectors.

By having a powerful distribution capability with an easily expandable embedded board, the BeagleBone black system is a state-of-the-art device that allows you to build powerful and versatile monitoring and controlling applications.

Packed with real-world examples, this book will try to fill the gap by showing you how some peripherals can be connected to the BeagleBone Black, and how you can get access to them in order to develop your own monitoring and control systems.

After the introductory chapter about how to set up the board from scratch, we'll take a look at compilation and cross-compilation of both user space and kernel space applications with some basic debugging techniques. Next, we'll move to high-level applications, such as daemons, and high-level programming using scripting languages, such as PHP, Python, and Bash. Finally, we'll see how a system daemon works and how it can be implemented from scratch using several programming languages.

This book will also explain how several computer peripherals can be connected to the BeagleBone Black board. We'll present several kinds of devices, such as serial ports, USB host ports and devices, and SPI/I2C/1-Wire devices. For each peripheral, we'll see how it can be connected to the BeagleBone Black, and how the developer can get access to it from the user space, and in some circumstances we'll see some kernel code too.

The hardware is presented in a generic manner, so no BeagleBone Black capes are presented in this book! This is because each cape has tons of documentation on the Internet, and using a generic approach allows you to better understand how the hardware management works and how to replicate the examples on another GNU/Linux-based system with a little effort.

Accessing all peripherals and writing good monitoring and controlling programs can both be complex tasks; it's easy to make mistakes early in the development stage that leads to serious problems in production. Learning the best practices before starting your project will help you avoid these problems and ensure that your project will be a success.

What this book covers

Chapter 1, Installing the Developing System, shows you how to use the BeagleBone Black's on-board operating system and how to (re)install and set up a complete developing system, based on the Debian distribution, starting from zero.

Chapter 2, Managing the System Console, shows you how a serial console works, and how you can use it in order to control/monitor the system activities (that is, monitoring the kernel messages, managing the bootloader, and so on). At the end of this chapter, a special section will introduce you to some bootloader commands.

Chapter 3, Compiling versus Cross-compiling, show you all the compiling steps in both kernel and user space to easily add a new driver that is not included in the standard BeagleBone Black's kernel and/or to recompile a user-space tool, which is not included in the Debian distribution.

Chapter 4, Quick Programming with Scripts, shows you how to install and use some common scripting languages in the BeagleBone Black board, and then how to solve a real problem by writing the solution in each language, in order to show you the differences between the usage of these languages.

Chapter 5, Device Drivers, shows you a possible implementation of a very simple driver (by writing a proper kernel module), in order to show you some basics of the Linux kernel's internals.

Chapter 6, Serial Ports and TTY Devices, discusses the serial ports, which is one of the most important peripheral classes a computer can have. We'll see how we can manage them in a Linux system in order to use a real serial device.

Chapter 7, Universal Serial Bus – USB, shows you how the USB bus works, the different types of USB devices and how they are supported in the BeagleBone Black's kernel. Simple management examples of real USB device and USB gadget are present.

Chapter 8, Inter-integrated Circuit – I2C, shows you how the I2C bus works, giving you a panoramic view, as wide as possible, of the most commonly used I2C devices; we'll discuss an EEPROM, a DAC, and an ADC.

Chapter 9, Serial Peripheral Interface – SPI, shows you how the SPI bus works by presenting you a simple temperature sensor management, as a quite complex device, such as a mini LCD display.

Chapter 10, 1-Wire Bus – W1, shows you how the 1-Wire bus works by showing you how to get access to a temperature sensor.

Chapter 11, Useful System Daemons, discusses the Unix daemon by showing you how an embedded computer developer can use existing daemons or new written ones in several programming languages.

What you need for this book

The prerequisites required for efficient learning are mentioned in the following sections.

Software prerequisites

You should have a little knowledge of the software of a non-graphical text editor, such as vi, emacs, or nano. You can even connect an LCD display, a keyboard, and a mouse directly to the BeagleBone Black, and then use the graphical interface; in this book, we assume that you are able to do little modifications to the text files using a text-only editor.

The host computer, that is, the computer you will use to cross-compile the code and/or to manage the BeagleBone Black is assumed to run on a GNU/Linux-based distribution. My host PC is running an Ubuntu 14.04 LTS, but you can use a Debian-based system too with little modification, or you may use another GNU/Linux distribution but with a little effort from you, mainly regarding cross-compiling tools installation. Foreign systems, such as Windows, Mac OS, or similar ones, are not covered in this book because we should not use low technology systems to develop the code for high technology systems.

To know how a C compiler works and how to manage a Makefile could help, but don't worry, all examples start from the very beginning, so even a developer who is not skilled should be able do the job.

This book will present some kernel programming techniques but these cannot be taken as a kernel programming course. You need a proper book for such a topic! However, each example is well-documented and you will find several suggested resources.

Finally, let me add that knowing a visualization system could be useful, especially in case you wish to set up your own developing host computer.

Hardware prerequisites

In this book, all the code is developed for the BeagleBone Black board Revision C, but you can use an older revision without any issues; in fact, the code is portable, and it should work on other systems too!

The computer peripherals used in this book are discussed in the section where I got the hardware and where you can buy it; but of course, you can decide to surf the Internet in order to find a better and cheaper offer.

You should not have any difficulties connecting the hardware to the BeagleBone Black presented in this book, since the connections are very few and well documented. They don't require any particular hardware skills to be performed by you.

Who this book is for

If you are a developer who wants to learn how to use embedded machine learning capabilities and get access to a GNU/Linux device driver to collect data from a peripheral or to control a device, this is the book for you. If you have some hardware or electrical engineering experience and know the basics of C, Bash, and Python/PHP programming in a Unix environment but desire to learn more about it, then this book is for you.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Codes and command lines

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To get the preceding kernel messages, we can use both the dmesg and tail -f /var/log/kern.log commands."

A block of code is set as follows:

#include <stdio.h>

int main(int argc, char *argv[])
        printf("Hello World!\n");

        return 0;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

#include <stdio.h>

int main(int argc, char *argv[])
        printf("Hello World!\n");

        return 0;

Any command line input or output given on the BeagleBone Black is written as follows:

root@BeagleBone:~# make CFLAGS="-Wall -O2" helloworldcc -Wall -O2 helloworld.c -o helloworld

Any command line input or output given on my host computer as a non-privileged user is written as follows:

$ tail -f /var/log/kern.log

When I need to give a command as a privileged user (root) on my host computer the command line input or output is then written as follows:

# /etc/init.d/apache2 restart

The reader should notice that all privileged commands can be executed by a normal user too by using the sudo command with the form:

$ sudo <command>

So the preceding command can be executed by a normal user as:

$ sudo /etc/init.d/apache2 restart

Kernel and logging messages

On several GNU/Linux distribution a kernel message has the usual form:

Oct 27 10:41:56 hulk kernel: [46692.664196] usb 2-1.1: new high-speed USB device number 12 using ehci-pci

Which is a quite long line for this book, that's why the characters from the start of the lines till the point where the real information begin are dropped. So, in the preceding example, the lines output will be reported as follow:

usb 2-1.1: new high-speed USB device number 12 using ehci-pci

Long outputs, repeated or less important lines in a terminal are dropped by replacing them with three dots ... shown as follows:

output begin
output line 1
output line 2
output line 10
output end

File modifications

When the reader should modify a text file, I'm going to use the unified context diff format since this is a very efficient and compact way to represent a text modification. This format can be obtained by using the diff command with the -u option argument.

As a simple example, let's consider the following text into file1.old:

This is first line
This is the second line
This is the third line
This is the last line

Suppose we have to modify the third line as highlighted in the following snippet:

This is first line
This is the second line
This is the new third line modified by me
This is the last line

The reader can easily understand that reporting each time the whole file for a simple modification it's quite obscure and space consuming, however by using the unified context diff format the preceding modification can be written as follow:

$ diff -u file1.old
--- file1.old 2015-03-23 14:49:04.354377460 +0100
+++ 2015-03-23 14:51:57.450373836 +0100
@@ -1,6 +1,6 @@
 This is first line
 This is the second line
-This is the third line
+This is the new third line modified by me
 This is the last line

Now the modification is very clear and written in a compact form! It starts with a two-line header where the original file is preceded by --- and the new file is preceded by +++, then follows one or more change hunks that contain the line differences in the file. The preceding example has just one hunk where the unchanged lines are preceded by a space character, while the lines to be added are preceded by a + character and the lines to be removed are preceded by a - character.

Other conventions

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If it prints Hello World, then our code has been executed successfully!"


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

For this book, the example code can also be downloaded from the author's GitHub repository at URL

Just use the following command to get it at once:

$ git clone

The examples are grouped according to the chapters name so you can easily find the code during the reading of the book.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.