18 min read

In this article by Pratik Desai, the author of Python Programming for Arduino, we will cover the following topics:

  • Working with pyFirmata methods
  • Servomotor – moving the motor to a certain angle
  • The Button() widget – interfacing GUI with Arduino and LEDs

(For more resources related to this topic, see here.)

Working with pyFirmata methods

The pyFirmata package provides useful methods to bridge the gap between Python and Arduino’s Firmata protocol. Although these methods are described with specific examples, you can use them in various different ways. This section also provides detailed description of a few additional methods.

Setting up the Arduino board

To set up your Arduino board in a Python program using pyFirmata, you need to specifically follow the steps that we have written down. We have distributed the entire code that is required for the setup process into small code snippets in each step. While writing your code, you will have to carefully use the code snippets that are appropriate for your application. You can always refer to the example Python files containing the complete code. Before we go ahead, let’s first make sure that your Arduino board is equipped with the latest version of the StandardFirmata program and is connected to your computer:

  1. Depending upon the Arduino board that is being utilized, start by importing the appropriate pyFirmata classes to the Python code. Currently, the inbuilt pyFirmata classes only support the Arduino Uno and Arduino Mega boards:
    from pyfirmata import Arduino

    In case of Arduino Mega, use the following line of code:

    from pyfirmata import ArduinoMega
  2. Before we start executing any methods that is associated with handling pins, it is required to properly set the Arduino board. To perform this task, we have to first identify the USB port to which the Arduino board is connected and assign this location to a variable in the form of a string object. For Mac OS X, the port string should approximately look like this:
    port = '/dev/cu.usbmodemfa1331'

    For Windows, use the following string structure:

    port = 'COM3'

    In the case of the Linux operating system, use the following line of code:

    port = '/dev/ttyACM0'

    The port’s location might be different according to your computer configuration. You can identify the correct location of your Arduino USB port by using the Arduino IDE.

  3. Once you have imported the Arduino class and assigned the port to a variable object, it’s time to engage Arduino with pyFirmata and associate this relationship to another variable:
    board = Arduino(port)

    Similarly, for Arduino Mega, use this:

    board = ArduinoMega(port)
  4. The synchronization between the Arduino board and pyFirmata requires some time. Adding sleep time between the preceding assignment and the next set of instructions can help to avoid any issues that are related to serial port buffering. The easiest way to add sleep time is to use the inbuilt Python method, sleep(time):
    from time import sleep

    The sleep() method takes seconds as the parameter and a floating-point number can be used to provide the specific sleep time. For example, for 200 milliseconds, it will be sleep(0.2).

At this point, you have successfully synchronized your Arduino Uno or Arduino Mega board to the computer using pyFirmata. What if you want to use a different variant (other than Arduino Uno or ArduinoMega) of the Arduino board?

  • Any board layout in pyFirmata is defined as a dictionary object. The following is a sample of the dictionary object for the Arduino board:
    arduino = {
        'digital' : tuple(x for x in range(14)),
        'analog' : tuple(x for x in range(6)),
        'pwm' : (3, 5, 6, 9, 10, 11),
        'use_ports' : True,
        'disabled' : (0, 1) # Rx, Tx, Crystal
  • For your variant of the Arduino board, you have to first create a custom dictionary object. To create this object, you need to know the hardware layout of your board. For example, an Arduino Nano board has a layout similar to a regular Arduino board, but it has eight instead of six analog ports. Therefore, the preceding dictionary object can be customized as follows:
    nano = {
        'digital' : tuple(x for x in range(14)),
        'analog' : tuple(x for x in range(8)),
        'pwm' : (3, 5, 6, 9, 10, 11),
        'use_ports' : True,
        'disabled' : (0, 1) # Rx, Tx, Crystal
  • As you have already synchronized the Arduino board earlier, modify the layout of the board using the setup_layout(layout) method:

    This command will modify the default layout of the synchronized Arduino board to the Arduino Nano layout or any other variant for which you have customized the dictionary object.

Configuring Arduino pins

Once your Arduino board is synchronized, it is time to configure the digital and analog pins that are going to be used as part of your program. Arduino board has digital I/O pins and analog input pins that can be utilized to perform various operations. As we already know, some of these digital pins are also capable of PWM.

The direct method

Now before we start writing or reading any data to these pins, we have to first assign modes to these pins. In the Arduino sketch-based, we use the pinMode function, that is, pinMode(11, INPUT) for this operation. Similarly, in pyFirmata, this assignment operation is performed using the mode method on the board object as shown in the following code snippet:

from pyfirmata import Arduino
from pyfirmata import INPUT, OUTPUT, PWM
# Setting up Arduino board
port = '/dev/cu.usbmodemfa1331'
board = Arduino(port)
# Assigning modes to digital pins
board.digital[13].mode = OUTPUT
board.analog[0].mode = INPUT

The pyFirmata library includes classes for the INPUT and OUTPUT modes, which are required to be imported before you utilized them. The preceding example shows the delegation of digital pin 13 as an output and the analog pin 0 as an input. The mode method is performed on the variable assigned to the configured Arduino board using the digital[] and analog[] array index assignment.

The pyFirmata library also supports additional modes such as PWM and SERVO. The PWM mode is used to get analog results from digital pins, while SERVO mode helps a digital pin to set the angle of the shaft between 0 to 180 degrees. If you are using any of these modes, import their appropriate classes from the pyFirmata library. Once these classes are imported from the pyFirmata package, the modes for the appropriate pins can be assigned using the following lines of code:

board.digital[3].mode = PWM
board.digital[10].mode = SERVO

Assigning pin modes

The direct method of configuring pin is mostly used for a single line of execution calls. In a project containing a large code and complex logic, it is convenient to assign a pin with its role to a variable object. With an assignment like this, you can later utilize the assigned variable throughout the program for various actions, instead of calling the direct method every time you need to use that pin. In pyFirmata, this assignment can be performed using the get_pin(pin_def) method:

from pyfirmata import Arduino
port = '/dev/cu.usbmodemfa1311'
board = Arduino(port)
# pin mode assignment
ledPin = board.get_pin('d:13:o')

The get_pin() method lets you assign pin modes using the pin_def string parameter, ‘d:13:o’. The three components of pin_def are pin type, pin number, and pin mode separated by a colon (:) operator. The pin types ( analog and digital) are denoted with a and d respectively. The get_pin() method supports three modes, i for input, o for output, and p for PWM. In the previous code sample, ‘d:13:o’ specifies the digital pin 13 as an output. In another example, if you want to set up the analog pin 1 as an input, the parameter string will be ‘a:1:i’.

Working with pins

As you have configured your Arduino pins, it’s time to start performing actions using them. Two different types of methods are supported while working with pins: reporting methods and I/O operation methods.

Reporting data

When pins get configured in a program as analog input pins, they start sending input values to the serial port. If the program does not utilize this incoming data, the data starts getting buffered at the serial port and quickly overflows. The pyFirmata library provides the reporting and iterator methods to deal with this phenomenon.

The enable_reporting() method is used to set the input pin to start reporting. This method needs to be utilized before performing a reading operation on the pin:


Once the reading operation is complete, the pin can be set to disable reporting:


In the preceding example, we assumed that you have already set up the Arduino board and configured the mode of the analog pin 3 as INPUT.

The pyFirmata library also provides the Iterator() class to read and handle data over the serial port. While working with analog pins, we recommend that you start an iterator thread in the main loop to update the pin value to the latest one. If the iterator method is not used, the buffered data might overflow your serial port. This class is defined in the util module of the pyFirmata package and needs to be imported before it is utilized in the code:

from pyfirmata import Arduino, util
# Setting up the Arduino board
port = 'COM3'
board = Arduino(port)
# Start Iterator to avoid serial overflow
it = util.Iterator(board)

Manual operations

As we have configured the Arduino pins to suitable modes and their reporting characteristic, we can start monitoring them. The pyFirmata provides the write() and read() methods for the configured pins.

The write() method

The write() method is used to write a value to the pin. If the pin’s mode is set to OUTPUT, the value parameter is a Boolean, that is, 0 or 1:

board.digital[pin].mode = OUTPUT

If you have used an alternative method of assigning the pin’s mode, you can use the write() method as follows:

ledPin = board.get_pin('d:13:o')

In case of the PWM signal, the Arduino accepts a value between 0 and 255 that represents the length of the duty cycle between 0 and 100 percent. The PyFiramta library provides a simplified method to deal with the PWM values as instead of values between 0 and 255, as you can just provide a float value between 0 and 1.0. For example, if you want a 50 percent duty cycle (2.5V analog value), you can specify 0.5 with the write() method. The pyFirmata library will take care of the translation and send the appropriate value, that is, 127, to the Arduino board via the Firmata protocol:

board.digital[pin].mode = PWM

Similarly, for the indirect method of assignment, you can use code similar to the following one:

pwmPin = board.get_pin('d:13:p')

If you are using the SERVO mode, you need to provide the value in degrees between 0 and 180. Unfortunately, the SERVO mode is only applicable for direct assignment of the pins and will be available in future for indirect assignments:

board.digital[pin].mode = SERVO

The read() method

The read() method provides an output value at the specified Arduino pin. When the Iterator() class is being used, the value received using this method is the latest updated value at the serial port. When you read a digital pin, you can get only one of the two inputs, HIGH or LOW, which will translate to 1 or 0 in Python:


The analog pins of Arduino linearly translate the input voltages between 0 and +5V to 0 and 1023. However, in pyFirmata, the values between 0 and +5V are linearly translated into the float values of 0 and 1.0. For example, if the voltage at the analog pin is 1V, an Arduino program will measure a value somewhere around 204, but you will receive the float value as 0.2 while using pyFirmata’s read() method in Python.

Servomotor – moving the motor to certain angle

Servomotors are widely used electronic components in applications such as pan-tilt camera control, robotics arm, mobile robot movements, and so on where precise movement of the motor shaft is required. This precise control of the motor shaft is possible because of the position sensing decoder, which is an integral part of the servomotor assembly. A standard servomotor allows the angle of the shaft to be set between 0 and 180 degrees. The pyFirmata provides the SERVO mode that can be implemented on every digital pin. This prototyping exercise provides a template and guidelines to interface a servomotor with Python.


Typically, a servomotor has wires that are color-coded red, black and yellow, respectively to connect with the power, ground, and signal of the Arduino board. Connect the power and the ground of the servomotor to the 5V and the ground of the Arduino board. As displayed in the following diagram, connect the yellow signal wire to the digital pin 13:
Python Programming for Arduino

If you want to use any other digital pin, make sure that you change the pin number in the Python program in the next section. Once you have made the appropriate connections, let’s move on to the Python program.

The Python code

The Python file consisting this code is named servoCustomAngle.py and is located in the code bundle of this book, which can be downloaded from https://www.packtpub.com/books/content/support/19610. Open this file in your Python editor. Like other examples, the starting section of the program contains the code to import the libraries and set up the Arduino board:

from pyfirmata import Arduino, SERVO
from time import sleep
# Setting up the Arduino board
port = 'COM5'
board = Arduino(port)
# Need to give some time to pyFirmata and Arduino to synchronize

Now that you have Python ready to communicate with the Arduino board, let’s configure the digital pin that is going to be used to connect the servomotor to the Arduino board. We will complete this task by setting the mode of pin 13 to SERVO:

# Set mode of the pin 13 as SERVO
pin = 13
board.digital[pin].mode = SERVO

The setServoAngle(pin,angle) custom function takes the pins on which the servomotor is connected and the custom angle as input parameters. This function can be used as a part of various large projects that involve servos:

# Custom angle to set Servo motor angle
def setServoAngle(pin, angle):

In the main logic of this template, we want to incrementally move the motor shaft in one direction until it achieves the maximum achievable angle (180 degrees) and then move it back to the original position with the same incremental speed. In the while loop, we will ask the user to provide inputs to continue this routine, which will be captured using the raw_input() function. The user can enter character y to continue this routine or enter any other character to abort the loop:

# Testing the function by rotating motor in both direction
while True:
  for i in range(0, 180):
    setServoAngle(pin, i)
  for i in range(180, 1, -1):
    setServoAngle(pin, i)
  # Continue or break the testing process
  i = raw_input("Enter 'y' to continue or Enter to quit): ")
  if i == 'y':

While working with all these prototyping examples, we used the direct communication method by using digital and analog pins to connect the sensor with Arduino. Now, let’s get familiar with another widely used communication method between Arduino and the sensors. This is called I2C communication.

The Button() widget – interfacing GUI with Arduino and LEDs

Now that you have had your first hands-on experience in creating a Python graphical interface, let’s integrate Arduino with it. Python makes it easy to interface various heterogeneous packages within each other and that is what you are going to do. In the next coding exercise, we will use Tkinter and pyFirmata to make the GUI work with Arduino. In this exercise, we are going to use the Button() widget to control the LEDs interfaced with the Arduino board.

Before we jump to the exercises, let’s build the circuit that we will need for all upcoming programs. The following is a Fritzing diagram of the circuit where we use two different colored LEDs with pull up resistors. Connect these LEDs to digital pins 10 and 11 on your Arduino Uno board, as displayed in the following diagram:
Python Programming for Arduino

While working with the code provided in this section, you will have to replace the Arduino port that is used to define the board variable according to your operating system. Also, make sure that you provide the correct pin number in the code if you are planning to use any pins other than 10 and 11. For some exercises, you will have to use the PWM pins, so make sure that you have correct pins.

You can use the entire code snippet as a Python file and run it. But, this might not be possible in the upcoming exercises due to the length of the program and the complexity involved. For the Button() widget exercise, open the exampleButton.py file. The code contains three main components:

  • pyFirmata and Arduino configurations
  • Tkinter widget definitions for a button
  • The LED blink function that gets executed when you press the button

As you can see in the following code snippet, we have first imported libraries and initialized the Arduino board using the pyFirmata methods. For this exercise, we are only going to work with one LED and we have initialized only the ledPin variable for it:

import Tkinter
import pyfirmata
from time import sleep
port = '/dev/cu.usbmodemfa1331'
board = pyfirmata.Arduino(port)
ledPin = board.get_pin('d:11:o')

As we are using the pyFirmata library for all the exercises in this article, make sure that you have uploaded the latest version of the standard Firmata sketch on your Arduino board.

In the second part of the code, we have initialized the root Tkinter widget as top and provided a title string. We have also fixed the size of this window using the minsize() method. In order to get more familiar with the root widget, you can play around with the minimum and maximum size of the window:

top = Tkinter.Tk()
top.title("Blink LED using button")

The Button() widget is a standard Tkinter widget that is mostly used to obtain the manual, external input stimulus from the user. Like the Label() widget, the Button() widget can be used to display text or images. Unlike the Label() widget, it can be associated with actions or methods when it is pressed. When the button is pressed, Tkinter executes the methods or commands specified by the command option:

startButton = Tkinter.Button(top,

In this initialization, the function associated with the button is onStartButtonPress and the “Start” string is displayed as the title of the button. Similarly, the top object specifies the parent or the root widget. Once the button is instantiated, you will need to use the pack() method to make it available in the main window.

In the preceding lines of code, the onStartButonPress() function includes the scripts that are required to blink the LEDs and change the state of the button. A button state can have the state as NORMAL, ACTIVE, or DISABLED. If it is not specified, the default state of any button is NORMAL. The ACTIVE and DISABLED states are useful in applications when repeated pressing of the button needs to be avoided. After turning the LED on using the write(1) method, we will add a time delay of 5 seconds using the sleep(5) function before turning it off with the write(0) method:

def onStartButtonPress():
  # LED is on for fix amount of time specified below

At the end of the program, we will execute the mainloop() method to initiate the Tkinter loop. Until this function is executed, the main window won’t appear.

To run the code, make appropriate changes to the Arduino board variable and execute the program. The following screenshot with a button and title bar will appear as the output of the program. Clicking on the Start button will turn on the LED on the Arduino board for the specified time delay. Meanwhile, when the LED is on, you will not be able to click on the Start button again. Now, in this particular program, we haven’t provided sufficient code to safely disengage the Arduino board and it will be covered in upcoming exercises.

Python Programming for Arduino


In this article, we learned about the Python library pyFirmata to interface Arduino to your computer using the Firmata protocol. We build a prototype using pyFirmata and Arduino to control servomotor and also developed another one with GUI, based on the Tkinter library, to control LEDs.

Resources for Article:

Further resources on this subject:


Please enter your comment!
Please enter your name here