Chapter 8. Physical Computing in Python

We used JavaScript language to program BeagleBone untill now. Now we'll focus on another popular language for programming BeagleBone—Python. It is a general purpose, widely adopted, high level language. It is easy to do coding in Python. Similar to BoneScript, there is a Python library from Adafruit that can do GPIO/Analog IO for BeagleBone. There is also an alternative library available called PyBBIO, which strongly follows the Arduino style of coding. As Adafruit's library is very similar to the BoneScript we learned earlier, we will follow it to code in Python throughout the book.

In this chapter, we will do many physical computing exercises in Python that we did in earlier chapters using JavaScript/node.js. It is assumed that you have gone through the earlier chapters. A good knowledge of the theory from Chapter 2, Blinking Onboard LEDs up to Chapter 6, PWM – Writing Analog Information is essential to get the most out of this chapter.

This chapter will cover:

  • Python programming in BeagleBone
  • Program to blink external LEDs
  • Program to dance external LEDs
  • Program to read from push button
  • Program to print temperature
  • Program to check light intensity
  • Program to fade in and fade out an LED
  • Program to control a micro servo motor

Python programming in BeagleBone

This is the first chapter of this book that talks about BeagleBone programming with Python. Let's get introduced to the Python language and compare it with JavaScript.

Python language is easy to code. Code written in Python is more readable than other languages. It is a high level language, which means the programmer does not have to worry much about hardware details while coding. A big program in low-level language can be replaced by a small program in Python with the same functionality. Python is a popular language and has a large community. You can find more resources on the official Python website at: https://www.python.org/ and https://docs.python.org/2/.

There are some differences in JavaScript/Node.js and Python programming. Python is used in a wide range of applications besides the web. It follows synchronous execution. Unlike Node.js, Python is a blocking language. That means a Python interpreter waits until the current line execution completes before executing the next line. The sleeping function sleep() is available in the "time" library. There are many other differences. We will learn them eventually.

Adafruit BBIO library

Adafruit is an open source hardware company that sells electronic kits and parts. The official website is https://www.adafruit.com/. Most of the electronic components in this book are available on Adafruit. They have very good graphic tutorials available for kits, including BeagleBone. You can access them on: https://learn.adafruit.com/category/beaglebone. Besides this, they have written many open source applications and libraries for various kits. They have written a Python library to handle BeagleBone Input/Output. It is called Adafruit_BBIO. The source code for this library is available at: https://github.com/adafruit/adafruit-beaglebone-io-python. This library provides a set of Python tools to allow GPIO, Analog IO, I2C, and so on, access on BeagleBone. We are going to use it throughout the Python section.

This library comes pre-installed on the newer Debian distribution image available on beagleboard.org. You can check that by running the following command on the BeagleBone. Refer to Chapter 1, Cloud9 IDE to learn how you can get shell access on BeagleBone:

pip freeze | grep BBIO

This command should output something like Adafruit-BBIO==0.0.20. If it does not, you need to run the following commands to install it on Debian/Ubuntu on BeagleBone:

sudo ntpdate pool.ntp.org  ##sync time
sudo apt-get update
sudo apt-get install build-essential python-dev python-pip -y
sudo pip install Adafruit_BBIO

pip is a Python package manager just like npm for node. Alternatively, you can install the library manually from the GitHub source link given above.

Cloud9 IDE can also be used to do Python coding. It provides features like highlighting code, following indentation, and so on. But current version does not support debugging Python programs. To debug a Python program, you need to use the Python debugger pdb separately. The pdb is an interactive debugger similar to gdb debugger. We are not covering pdb in this book. Here is a link to a good tutorial to learn about using pdb: http://pymotw.com/2/pdb. In case of error, Python throws readable exceptions, for example. if you press Ctrl + C when the program is running, you will get a KeyboardInterrupt exception. Debugging readable exceptions is easier than errors in other languages. Alternatively, you can get shell access and use text editor like vim, and emacs to write Python programs instead of Cloud9.

Note

Please make sure you run programs with sudo privileges when running program from command line.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13