The Python Arduino Command API is a light-weight Python library for communicating with Arduino microcontroller boards from a connected computer using standard serial IO, either over a physical wire or wirelessly. It is written using a custom protocol, similar to Firmata.
This allows a user to quickly protoype programs for Arduino using Python code, or to simply read/control/troubleshoot/experiment with harware connected to an Arduino board without ever having to recompile and reload sketches to the board itself.
Method names within the Python Arduino Command API are designed to be as close as possible to their Arduino programming language counterparts
#!/usr/bin/env python
"""
Blinks an LED on digital pin 13
in 1 second intervals
"""
from Arduino import Arduino
import time
board = Arduino('9600') #plugged in via USB, serial com at rate 9600
board.pinMode(13, "OUTPUT")
while True:
board.digitalWrite(13, "LOW")
time.sleep(1)
board.digitalWrite(13, "HIGH")
time.sleep(1)- Python 2.3 or higher (Python 3.x not yet tested, but would probably work)
- pyserial 2.6 or higher
- Any Arduino compatible microcontroller with at least 14KB of flash memory
Either run pip install arduino-python from a command line, or run python setup.py build install from the source directory to install this library.
- Verify that your Arduino board communicates at the baud rate specified in the
setup()function (line 348) inprototype.ino. Change it there if necessary. - Load the
prototype.inosketch onto your Arduino board, using the Arduino IDE. - Set up some kind of serial I/O communication between the Arduino board and your computer (via physical USB cable, bluetooth, xbee, etc + associated drivers)
- Add
from Arduino import Arduinointo your python script to communicate with your Arduino
For a collection of examples, see examples.py. This file contains methods which replicate
the functionality of many Arduino demo sketches.
The tests directory contains some basic tests for the library. Extensive code coverage is a bit difficult to expect for every release, since a positive test involves actually
connecting and issuing commands to a live Arduino, hosting any hardware
required to test a particular function. But a core of basic communication tests
should at least be maintained here and used before merging into the master branch.
After installation, the interactive tests can be run from the source directory:
$ python tests/test_main.pyAutomated tests can be run from the source directory with:
< 8899 div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ python tests/test_arduino.py">$ python tests/test_arduino.py