Getting started with pySerial

You learned about the Firmata protocol in the previous section. This is an easy and quick way to start working with Arduino. Although the Firmata protocol helps you to develop complex applications from your computer without modifying the Arduino sketch, we are not ready to start coding these applications.

The first step towards writing these complex applications is to provide an interface between your programming environment and the Arduino via a serial port. In this book, you will be required to establish a connection between the Python interpreter and Arduino for every project that we develop.

Writing your own library, which includes implementation of functions and specifications to enable communication on a serial protocol, is an inconvenient and time consuming process. We are going to avoid that by using an open source, well maintained Python library called pySerial.

The pySerial library enables communication with Arduino by encapsulating the access for the serial port. This module provides access to the serial port settings through Python properties and allows you to configure the serial port directly through the interpreter. pySerial will be the bridge for any future communication between the Python and Arduino. Let's start by installing pySerial.

Installing pySerial

We installed the package manager Setuptools in Chapter 1, Getting Started with Python and Arduino. If you have skipped that chapter and are not sure about it, then please go through that section. If you already know how to install and configure Python library packages, skip these installation steps.

From this stage, we are going to use only pip-based installation commands due to their obvious advantages that were described in Chapter 1, Getting Started with Python and Arduino:

  1. Open a terminal or command prompt and execute the following command:
    > pip install pyserial
    

    The Windows operating system does not require administrator-level user access to execute the command, but you should have root privileges to install Python packages in Unix-based operating systems, as follows:

    $ sudo pip install pyserial
    

    If you want to install the pySerial library from source, download the archive from http://pypi.python.org/pypi/pyserial, unpack it, and from the pySerial directory, run the following command:

    $ sudo python setup.py install
    
  2. If Python and Setuptools are installed properly, you should see the following output at the command line after the installation is complete:
    .
    .
    Processing dependencies for pyserial
    Finished processing dependencies for pyserial
    

    This means that you have successfully installed the pySerial library and you are good to go to the next section.

  3. Now, to check whether or not pySerial is successfully installed, start your Python interpreter and import the pySerial library using the following command:
    >>> import serial
    

Playing with a pySerial example

Your Arduino board has the Firmata sketch StandardFirmata from the previous example. To play with pySerial, we are not going to use the Firmata protocol anymore. Instead, we are going to use another simple Arduino sketch that implements serial communication that can be captured on the Python interpreter.

Sticking with the promise of not performing any coding for the Arduino sketch, let's select an example sketch from the Arduino IDE:

  1. As displayed in the following screenshot, navigate to File | Examples | 01. Basics | DigitalReadSerial.
    Playing with a pySerial example
  2. Compile and upload the program to the Arduino board using the same method that was described earlier. Select the appropriate serial port on which your Arduino is connected and make a note of it. As you can see in the sketch, this simple Arduino code transmits the status of digital pin 2 that is on the serial port with a baud rate of 9600 bps.
  3. Without disconnecting the Arduino board from your computer, open the Python interpreter. Then, execute the following commands on the Python interpreter. Make sure that you replace /dev/ttyACM0 with the port name that you noted down earlier:
    >>> import serial
    >>> s = serial.Serial('/dev/ttyACM0',9600)
    >>> while True:
     print s.readline()
    
  4. On execution, you should get repeated 0 values in the Python interpreter. Press Ctrl + C to terminate this code. As you can see, the Arduino code will keep sending messages due to the loop function that was used in the sketch. We don't have anything connected to pin 2, and because of this, we are getting the status 0, that is, Low.
  5. If you know what you are doing, you can connect any digital sensor to pin 2 and run the script again to see the changed status.

In the preceding Python script, the serial.Serial method interfaces and opens the specified serial port, while the readline() method reads each line from this interface, terminated with \n, that is, the newline character.

Note

The newline character is a special character that signifies the end of a line of text. It is also known as End of Line (EOL) or Line feed + Carriage Return (LF + CR). Learn more about the newline character at http://en.wikipedia.org/wiki/Newline.