Arduino and Python
Python is a highly dynamic language with built in data structure making it a hybrid choice in building hardware-based applications. Using Python with Arduino productivity of projects can increase and it opens the door to a new world of possibilities.
Arduino boards can be programmed using official compilation software Arduino IDE however its not possible to program Arduino boards directly using Python from IDE. There are certain libraries and packages available that can program Arduino boards through Python.
Ways to Program Arduino Using Python
Python and Arduino are open-source development tools. Like Arduino Python also has community support. The Python community has developed many open-source libraries which can be installed on multiple platforms such as Arduino.
While working with hardware-based platforms like Arduino it’s very important to establish serial communication between Arduino boards and computers. Multiple Python libraries are available for this purpose. Some most useful libraries are:
How to Setup Python with Arduino
Before going into depth of Python libraries we need to setup Python and Arduino IDE, so code written in Python can be uploaded on Arduino boards.
- Python can be downloaded from the official site. Click here to download.
- Arduino IDE is available on Arduino official download store. To download IDE, click here.
The pip also known as a package installer for Python programming. It manages and installs all the required packages for Python programming. Pip is also needed to install Python libraries mentioned earlier.
To install pip, first download its file using:
Now, execute the installation command:
To update the pip, the below mentioned command can be used:
Python provides an open-source library known as pySerial. This library enables communication with the Arduino board by the serial port. The pySerial is a bridge between Arduino boards and Python programming.
To install pySerial type below command in cmd window:
Installing pyFirmata Python Package
The pyFirmata is used for serial communication between our Arduino boards and Python script. Without pyFirmata one cannot upload code to Arduino using Python.
To install pyFirmata with Python package type command below given command in command prompt window:
Uploading pyFirmata to Arduino
pyFirmata was developed for the Firmata protocol. It allows communication between microcontrollers and the host Pc where the sketch is written. Any software that uses serial ports for communication can use Firmata protocols to interface with hardware boards like Arduino UNO.
Firmata protocols can be tested on Arduino boards using the standard Arduino Firmata library. Follow the steps given below and upload the Firmata library into the Arduino board.
Make sure the Arduino is connected to COM port before uploading the sketch. Go to: File>Example>Firmata>StandardFirmata:
Code will open in a new window, click on the Upload button to upload the code to Arduino:
After successfully uploading a sketch. The Arduino board is ready to program using Python language.
Upload Python LED Program in Arduino Uno
We have just set up our Arduino board with Python to write the first Arduino program using Python. Here are a few steps that show how to write an LED blinking program in Python and upload it to the Arduino board.
Step 1: Open Python IDLE using the Windows search box option.
Step 2: New window will open showing the Python IDLE shell where we can write an Arduino program in the Python programming language.
Step 3: Press Ctrl+N or click File then New File.
Step 4: Make circuit first, connect the positive end of LED at pin 13 and negative or shorter leg of LED at GND pin.
Step 5: Now, copy and paste the given code in the Python IDLE:
We started code by importing our pyFirmata protocol in Python IDE to establish connection between Arduino and Python. After that, it declared the COM port at which Arduino is connected. Next using board.get_pin we declared the output pin at which the LED is connected. In the while section x.write(1) function will glow LED continuously.
Step 6: Once our circuit and Python code is ready, it’s time to upload our code in the Arduino board. First save Python code then Press F5 or go to : Run>Run Module to upload code in Arduino UNO.
The above Python code will keep the LED ON.
MicroPython is a subset of Python programming language that is optimized to build Python based code on microcontrollers. Arduino IDE directly does not support Python, however we can write MicroPython code in OpenMV editor. It is a programming platform to upload Python code on Arduino boards. To download OpenMV editor click here.
OpenMV editor interface:
Currently only four Arduino boards support MicroPython known as pyboard.
- Nano 33 BLE
- Nano 33 BLE Sense
- Nano RP2040 Connect
- Portenta H7
Platforms like Arduino are on rise due to easy-to-use hardware and software. Thanks to IoT based projects people want to interface Arduino boards with other languages and high-end circuits. Python is one of the dynamic languages receiving a lot of attention among the Arduino community. Python is directly yet not supported by the Arduino platform, but it can be programmed using Python open-source libraries and packages.