How to Program and Control an Arduino With Python

Posted on

Python has taken the coding world by storm. Alongside the rise of this new language, the DIY electronics scene has also flourished. Development boards and single board computers from companies like Arduino and Raspberry Pi have changed the way people create home brewed electronics. Wouldn’t it be awesome if you could program an Arduino with Python?

There is no better feeling than combining two cool things. Sadly, it is impossible to directly program an Arduino with Python, as the boards have no option for onboard interpretation of the language. What is possible however, is direct control over USB using a Python program.

This article will show you how to set up an Arduino UNO (though any Arduino compatible board can work here) to be programmed and controlled from the command line using Python programs. This tutorial is written for Windows 10, but also works for Mac and Linux. You could even use this workflow to control an Arduino directly from a Raspberry Pifor the ultimate double-down-DIY experience.

Setting Up Your Arduino for Python

For today’s project we will be using an Arduino Uno, along with the pyFirmata interface for Python. You can use almost any Arduino-compatible board for this, though at the time of writing only the Arduino Uno, Mega, Due and Nano are supported by the pyFfirmata interface. If you are already a Python guru, you can add your own board support to pyFirmata — be sure to update their GitHub if you do!

If you haven’t already, install the Arduino IDE. If you are completely new to the world of microcontrollers, our beginner’s guide to Arduino will help you get everything in place.

Connect your Arduino board, and open up the IDE. Make sure you have the correct board and port selected in the Tools menu. Load up the StandardFirmata example sketch and upload it to the board. This will allow you to control the Arduino directly so long as it is connected to the computer via USB. Provided the sketch uploads to your board without any errors, you are ready to move on.

Python and Command Line Control

We’ll use Python 3.4 to control our Arduino, as the module you will be installing specifies this as the latest compatible version. Any version before this should work fine, and later versions have been reported to work. You can download Python 3.4 for Windows 10 from the Python Software Foundation site. If you want to run multiple version of Python, our guide to Python virtual environments will be able to help you.

Prev1 of 4Next

Leave a Reply

Your email address will not be published. Required fields are marked *