Package: arduino-python3
Arduino-Python3 Command APIThis API is forked from the originalPython Arduino Command APIto add support for Python 3.The Arduino-Python3 Command API is a lightweight Python library for
communicating withArduino microcontroller boardsfrom a connected computer using
standard serial IO, either over a physical wire
or wirelessly. It is written using a custom protocol, similar toFirmata.This allows a user to quickly prototype programs for Arduino using Python code, or to
simply read/control/troubleshoot/experiment
with hardware connected to an Arduino board without ever having to recompile and reload sketches to the board itself.Method names within the Arduino-Python3 Command API are designed to be as close
as possible to their Arduino programming language counterpartsSimple usage example (LED blink)#!/usr/bin/env python"""Blinks an LED on digital pin 13in 1 second intervals"""fromArduinoimportArduinoimporttimeboard=Arduino()# plugged in via USB, serial com at rate 115200board.pinMode(13,"OUTPUT")whileTrue:board.digitalWrite(13,"LOW")time.sleep(1)board.digitalWrite(13,"HIGH")time.sleep(1)Requirements:Python3.7 tested on Windows and macOS.pyserial2.6 or higherAnyArduino compatible microcontrollerwith at least 14KB of flash memoryInstallation:Either runpip install arduino-python3from a command line, or runpython setup.py build installfrom the source directory to install this library.Setup:Verify that your Arduino board communicates at the baud rate specified in thesetup()function (line 407) inprototype.ino. Change it there if necessary.Load theprototype.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)Addfrom Arduino import Arduinointo your python script to communicate with your ArduinoFor a collection of examples, seeexamples.py. This file contains methods which replicate
the functionality of many Arduino demo sketches.Testing:Thetestsdirectory 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 themasterbranch.After installation, the interactive tests can be run from the source directory:$pythontests/test_main.pyAutomated tests can be run from the source directory with:$pythontests/test_arduino.pyClassesArduino(baud)- Set up communication with currently connected and powered
Arduino.board=Arduino("115200")#ExampleThe device name / COM port of the connected Arduino will be auto-detected.
If there are more than one Arduino boards connected,
the desired COM port can be also be passed as an optional argument:board=Arduino("115200",port="COM3")#Windows exampleboard=Arduino("115200",port="/dev/tty.usbmodemfa141")#OSX exampleA time-out for reading from the Arduino can also be specified as an optional
argument:board=Arduino("115200",timeout=2)#Serial reading functions will#wait for no more than 2 secondsMethodsDigital I/OArduino.digitalWrite(pin_number, state)turn digital pin on/offArduino.digitalRead(pin_number)read state of a digital pin#Digital read / write exampleboard.digitalWrite(13,"HIGH")#Set digital pin 13 voltagestate_1=board.digitalRead(13)#Will return integer 1board.digitalWrite(13,"LOW")#Set digital pin 13 voltagestate_2=board.digitalRead(13)#Will return integer 0Arduino.pinMode(pin_number, io_mode)set pin I/O modeArduino.pulseIn(pin_number, state)measures a pulseArduino.pulseIn_set(pin_number, state)measures a pulse, with preconditioning#Digital mode / pulse exampleboard.pinMode(7,"INPUT")#Set digital pin 7 mode to INPUTduration=board.pulseIn(7,"HIGH")#Return pulse width measurement on pin 7Analog I/OArduino.analogRead(pin_number)returns the analog valueArduino.analogWrite(pin_number, value)sets the analog value#Analog I/O examplesval=board.analogRead(5)#Read value on analog pin 5 (integer 0 to 1023)val=val/4# scale to 0 - 255board.analogWrite(11)#Set analog value (PWM) based on analog measurementShift RegisterArduino.shiftIn(dataPin, clockPin, bitOrder)shift a byte in and returns itArduino.shiftOut(dataPin, clockPin, bitOrder, value)shift the given byte outbitOrdershould be either"MSBFIRST"or"LSBFIRST"Servo Library FunctionalitySupport is included for up to 8 servos.Arduino.Servos.attach(pin, min=544, max=2400)Create servo instance. Only 8 servos can be used at one time.Arduino.Servos.read(pin)Returns the angle of the servo attached to the specified pinArduino.Servos.write(pin, angle)Move an attached servo on a pin to a specified angleArduino.Servos.writeMicroseconds(pin, uS)Write a value in microseconds to the servo on a specified pinArduino.Servos.detach(pin)Detaches the servo on the specified pin#Servo exampleboard.Servos.attach(9)#declare servo on pin 9board.Servos.write(9,0)#move servo on pin 9 to 0 degreesprintboard.Servos.read(9)# should be 0board.Servos.detach(9)#free pin 9Software Serial FunctionalityArduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud)initialize software serial device on
specified pins.
Only one software serial device can be used at a time. Existing software serial instance will
be overwritten by calling this method, both in Python and on the Arduino board.Arduino.SoftwareSerial.write(data)send data using the Arduino 'write' function to the existing software
serial connection.Arduino.SoftwareSerial.read()returns one byte from the existing software serial connection#Software serial exampleboard.SoftwareSerial.begin(0,7,"19200")# Start software serial for transmit only (tx on pin 7)board.SoftwareSerial.write(" test ")#Send some dataresponse_char=board.SoftwareSerial.read()#read response characterEEPROMArduino.EEPROM.read(address)reads a byte from the EEPROMArduino.EEPROM.write(address, value)writes a byte to the EEPROMArduino.EEPROM.size()returns size of the EEPROM#EEPROM read and write exampleslocation=42value=10# 0-255(byte)board.EEPROM.write(location,10)print(board.EEPROM.read(location))print('EEPROM size{size}'.format(size=board.EEPROM.size()))MiscArduino.close()closes serial connection to the Arduino.To-do list:Expand software serial functionality (print()andprintln())Add simple reset functionality that zeros out all pin valuesAdd I2C / TWI function support (ArduinoWire.hcommands)Include a wizard which generates 'prototype.ino' with selected serial baud rate and Arduino function support
(to help reduce memory requirements).Multi-serial support for Arduino mega (Serial1.read(), etc)