Friday, May 24, 2013

Example Serial Communication between an Arduino & a Beaglebone Black (Beaglebone Black Side)

This is the Beaglebone Black portion of the example of serial communication between an Arduino Uno and a Beaglebone Black.  Click here for the Arduino portion of this example.  As noted in the Arduino posting, this example uses an RFID card reader (as part of a larger project), but it is intended as an example of serial communication, not as a complete RFID application.


See the Arduino portion of this example for information on how to connect the Arduino Uno and the Beaglebone Black (including the logic level converter).

To configure the UART/serial port on the Beaglebone Black, see this post.

Python Code

For this code to work, you need to have  the PySerial package installed. Use the command opkg install python-pyserial to install it (assuming that you are running Ångström Linux).

This program reads the data sent from the Arduino via the serial connection.  The RFID card data from the Arduino begins with a ^ and ends with \r\n.  As the code stands for now, the Beaglebone Black responds to to the received data by sending the card number back with the status message OK and the current date and time. The application will ultimately need to check the RFID tag number against a database and record the use of the tag, but this example is really just intended to show an example of serial communication.

import serial
import time
import datetime
import re

serial = serial.Serial("/dev/ttyO1", baudrate=38400)

resp = ""

while True:
        while (serial.inWaiting() > 0):
                data =
                resp += data
                if "\r\n" in resp:
                        now =
                        timestamp = "%02d/%02d/%d %02d:%02d:%02d" % \
                        matchObj = re.match(r'\^([0-9A-F]+)\r\n', resp)
                        resp = ""
                        serial.write( + " OK: " + timestamp + "\n")


  1. Thanks for this tutorial. It has been very helpful.

    I am new to the BBB but have used the arduino a lot. I have a few questions Is the file extension .py or js? Can I just send commands in the console of either the BBB or the Arduino?
    Also, I get this error when I try to run the code in Cloud9.

    debugger listening on port 5977
    (function (exports, require, module, __filename, __dirname) { import serial
    SyntaxError: Unexpected reserved word
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Object.Module.runMain [as _onTimeout] (module.js:492:10)
    at Timer.list.ontimeout (timers.js:101:19)

    Any help would be appreciated.

    1. A Python file normally has a .py extension. I don't use Cloud9 or Javascript; I usually just log in using SSH to get to the command line on the Beaglebone Black. Occasionally I plug in the HDMI, a keyboard, and a mouse, but for most things, I find the text interface works just fine. You can use the basic but functional nano editor to edit the Python file on the BBB. For times when I want a better IDE or editor, I edit the code on my Windows machine and use WinSCP to transfer the file to the Beaglebone (and then run it from the command prompt). To be honest, I had almost forgotten about Cloud9.

  2. This comment has been removed by the author.

  3. I'm new to BBB but have experience with Arduino. Which is easiest to jump into - python, bonescript, or linux command line? Which have the most limitations?

    1. Hi James -

      I would recommend Python. I do my programming at the command line using nano as my main editor. I also use C and Java on the BBB. I've never gotten very far with Bonescript and haven't really had any need to learn it. Python is rather different in its syntax from C or Java, but you can start doing useful things with it quickly. There are many resources for it. I'm still using Python 2. Python 3 is available, but a number of libraries don't seem to have been updated yet and may not work with it.


  4. SIR I am new to bbb. i want serial data transmission .i installed debian os. what is the step should be followed in initial and send python code ..