{"id":13781889,"url":"https://github.com/thearn/Python-Arduino-Command-API","last_synced_at":"2025-05-11T15:32:06.715Z","repository":{"id":4238884,"uuid":"5363094","full_name":"thearn/Python-Arduino-Command-API","owner":"thearn","description":"A Python library for communicating with Arduino microcontroller boards","archived":false,"fork":false,"pushed_at":"2025-05-02T14:25:44.000Z","size":140,"stargazers_count":420,"open_issues_count":9,"forks_count":152,"subscribers_count":56,"default_branch":"master","last_synced_at":"2025-05-02T15:38:29.626Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thearn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2012-08-10T00:32:40.000Z","updated_at":"2025-05-02T14:25:50.000Z","dependencies_parsed_at":"2023-07-05T19:18:01.757Z","dependency_job_id":null,"html_url":"https://github.com/thearn/Python-Arduino-Command-API","commit_stats":{"total_commits":121,"total_committers":11,"mean_commits":11.0,"dds":"0.38842975206611574","last_synced_commit":"610171b3ae153542aca42d354fbb26c32027f38f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearn%2FPython-Arduino-Command-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearn%2FPython-Arduino-Command-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearn%2FPython-Arduino-Command-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearn%2FPython-Arduino-Command-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thearn","download_url":"https://codeload.github.com/thearn/Python-Arduino-Command-API/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253588626,"owners_count":21932289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:30.463Z","updated_at":"2025-05-11T15:32:06.698Z","avatar_url":"https://github.com/thearn.png","language":"Python","readme":"# Arduino-Python3 Command API\n\nThis API is forked from the original [Python Arduino Command API](https://github.com/thearn/Python-Arduino-Command-API) to add support for Python 3.\n\nThe Arduino-Python3 Command API is a lightweight Python library for\ncommunicating with [Arduino microcontroller boards](http://www.arduino.cc/) from a connected computer using\nstandard serial IO, either over a physical wire\nor wirelessly. It is written using a custom protocol, similar to [Firmata](http://firmata.org/wiki/Main_Page).\n\nThis allows a user to quickly prototype programs for Arduino using Python code, or to\nsimply read/control/troubleshoot/experiment\nwith hardware connected to an Arduino board without ever having to recompile and reload sketches to the board itself.\n\nMethod names within the Arduino-Python3 Command API are designed to be as close\nas possible to their Arduino programming language counterparts\n\n## Simple usage example (LED blink)\n```python\n#!/usr/bin/env python\n\"\"\"\n Blinks an LED on digital pin 13\n in 1 second intervals\n\"\"\"\n\nfrom Arduino import Arduino\nimport time\n\nboard = Arduino() # plugged in via USB, serial com at rate 115200\nboard.pinMode(13, \"OUTPUT\")\n\nwhile True:\n    board.digitalWrite(13, \"LOW\")\n    time.sleep(1)\n    board.digitalWrite(13, \"HIGH\")\n    time.sleep(1)\n```\n\n## Requirements:\n- [Python](http://python.org/) 3.7 tested on Windows and macOS.\n- [pyserial](http://pyserial.sourceforge.net/) 2.6 or higher\n- Any [Arduino compatible microcontroller](https://www.sparkfun.com/categories/242) with at least 14KB of flash memory\n\n## Installation:\nEither run `pip install arduino-python3` from a command line, or run `python setup.py\nbuild install` from the source directory to install this library.\n\n## Setup:\n1. Verify that your Arduino board communicates at the baud rate specified in the\n`setup()` function (line 407) in `prototype.ino`. Change it there if necessary.\n2. Load the `prototype.ino` sketch onto your Arduino board, using the Arduino IDE.\n3. Set up some kind of serial I/O communication between the Arduino board and your computer (via physical USB cable,\nBluetooth, xbee, etc. + associated drivers)\n4. Add `from Arduino import Arduino` into your python script to communicate with your Arduino\n\nFor a collection of examples, see `examples.py`. This file contains methods which replicate\nthe functionality of many Arduino demo sketches.\n\n## Testing:\nThe `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\nconnecting and issuing commands to a live Arduino, hosting any hardware\nrequired to test a particular function. But a core of basic communication tests\nshould at least be maintained here and used before merging into the `master` branch.\n\nAfter installation, the interactive tests can be run from the source directory:\n```bash\n$ python tests/test_main.py\n```\n\nAutomated tests can be run from the source directory with:\n```bash\n$ python tests/test_arduino.py\n```\n\n## Classes\n- `Arduino(baud)` - Set up communication with currently connected and powered\nArduino.\n\n```python\nboard = Arduino(\"115200\") #Example\n```\n\nThe device name / COM port of the connected Arduino will be auto-detected.\nIf there are more than one Arduino boards connected,\nthe desired COM port can be also be passed as an optional argument:\n\n```python\nboard = Arduino(\"115200\", port=\"COM3\") #Windows example\n```\n```python\nboard = Arduino(\"115200\", port=\"/dev/tty.usbmodemfa141\") #OSX example\n```\n\nA time-out for reading from the Arduino can also be specified as an optional\nargument:\n\n```python\nboard = Arduino(\"115200\", timeout=2) #Serial reading functions will\n#wait for no more than 2 seconds\n```\n\n## Methods\n\n**Digital I/O**\n\n- `Arduino.digitalWrite(pin_number, state)` turn digital pin on/off\n- `Arduino.digitalRead(pin_number)` read state of a digital pin\n\n```python\n#Digital read / write example\nboard.digitalWrite(13, \"HIGH\") #Set digital pin 13 voltage\nstate_1 = board.digitalRead(13) #Will return integer 1\nboard.digitalWrite(13, \"LOW\") #Set digital pin 13 voltage\nstate_2 = board.digitalRead(13) #Will return integer 0\n```\n\n- `Arduino.pinMode(pin_number, io_mode)` set pin I/O mode\n- `Arduino.pulseIn(pin_number, state)` measures a pulse\n- `Arduino.pulseIn_set(pin_number, state)` measures a pulse, with preconditioning\n\n```python\n#Digital mode / pulse example\nboard.pinMode(7, \"INPUT\") #Set digital pin 7 mode to INPUT\nduration = board.pulseIn(7, \"HIGH\") #Return pulse width measurement on pin 7\n```\n\n**Analog I/O**\n\n- `Arduino.analogRead(pin_number)` returns the analog value\n- `Arduino.analogWrite(pin_number, value)` sets the analog value\n\n```python\n#Analog I/O examples\nval=board.analogRead(5) #Read value on analog pin 5 (integer 0 to 1023)\nval = val / 4 # scale to 0 - 255\nboard.analogWrite(11) #Set analog value (PWM) based on analog measurement\n```\n\n**Shift Register**\n\n- `Arduino.shiftIn(dataPin, clockPin, bitOrder)` shift a byte in and returns it\n- `Arduino.shiftOut(dataPin, clockPin, bitOrder, value)` shift the given byte out\n\n`bitOrder` should be either `\"MSBFIRST\"` or `\"LSBFIRST\"`\n\n**Servo Library Functionality**\nSupport is included for up to 8 servos.\n\n- `Arduino.Servos.attach(pin, min=544, max=2400)` Create servo instance. Only 8 servos can be used at one time.\n- `Arduino.Servos.read(pin)` Returns the angle of the servo attached to the specified pin\n- `Arduino.Servos.write(pin, angle)` Move an attached servo on a pin to a specified angle\n- `Arduino.Servos.writeMicroseconds(pin, uS)` Write a value in microseconds to the servo on a specified pin\n- `Arduino.Servos.detach(pin)` Detaches the servo on the specified pin\n\n```python\n#Servo example\nboard.Servos.attach(9) #declare servo on pin 9\nboard.Servos.write(9, 0) #move servo on pin 9 to 0 degrees\nprint board.Servos.read(9) # should be 0\nboard.Servos.detach(9) #free pin 9\n```\n\n**Software Serial Functionality**\n\n- `Arduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud)` initialize software serial device on\nspecified pins.\nOnly one software serial device can be used at a time. Existing software serial instance will\nbe overwritten by calling this method, both in Python and on the Arduino board.\n- `Arduino.SoftwareSerial.write(data)` send data using the Arduino 'write' function to the existing software\nserial connection.\n- `Arduino.SoftwareSerial.read()` returns one byte from the existing software serial connection\n\n```python\n#Software serial example\nboard.SoftwareSerial.begin(0, 7, \"19200\") # Start software serial for transmit only (tx on pin 7)\nboard.SoftwareSerial.write(\" test \") #Send some data\nresponse_char = board.SoftwareSerial.read() #read response character\n```\n\n**EEPROM**\n\n- `Arduino.EEPROM.read(address)` reads a byte from the EEPROM\n- `Arduino.EEPROM.write(address, value)` writes a byte to the EEPROM\n- `Arduino.EEPROM.size()` returns size of the EEPROM\n\n```python\n#EEPROM read and write examples\nlocation = 42\nvalue = 10 # 0-255(byte)\n\nboard.EEPROM.write(location, 10)\nprint(board.EEPROM.read(location))\nprint('EEPROM size {size}'.format(size=board.EEPROM.size()))\n```\n\n\n**Misc**\n\n- `Arduino.close()` closes serial connection to the Arduino.\n\n## To-do list:\n- Expand software serial functionality (`print()` and `println()`)\n- Add simple reset functionality that zeros out all pin values\n- Add I2C / TWI function support (Arduino `Wire.h` commands)\n- Include a wizard which generates 'prototype.ino' with selected serial baud rate and Arduino function support\n(to help reduce memory requirements).\n- Multi-serial support for Arduino mega (`Serial1.read()`, etc)\n","funding_links":[],"categories":["Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthearn%2FPython-Arduino-Command-API","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthearn%2FPython-Arduino-Command-API","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthearn%2FPython-Arduino-Command-API/lists"}