|
1 | | -# Python Arduino Command API |
2 | | - |
3 | | -> © 2012-2013 Tristan A. Hearn <[email protected]> |
4 | | -> under the MIT License |
5 | | -
|
6 | | -Based in part on the Python Arduino Prototyping API by Akash Manohar (https://github.com/HashNuke/Python-Arduino-Prototyping-API/). |
7 | | - |
8 | | -The Python Arduino Command API is a light-weight Python package for communicating with Arduino microcontroller boards. It is written |
9 | | -using a custom protocol, similar to Firmata (http://firmata.org/wiki/Main_Page). This allows a user to quickly protoype programs |
10 | | -for Arduino or to simply read and control harware connected to an Arduino from a host computer, without having to reload sketches onto an Arduino board. |
11 | | - |
12 | | -Method names within the Python Arduino Command API are designed to be as close as possible to their Arduino programming language counterparts. |
13 | | - |
14 | | -## Usage example |
15 | | -```python |
16 | | -#!/usr/bin/env python |
17 | | -""" |
18 | | - Blinks an LED on digital pin 13 |
19 | | - in 1 second intervals |
20 | | -""" |
21 | | - |
22 | | -from arduino import Arduino |
23 | | -import time |
24 | | - |
25 | | -board = Arduino('9600') |
26 | | - |
27 | | -whileTrue: |
28 | | - board.digitalWrite(13, "LOW") |
29 | | - time.sleep(1) |
30 | | - board.digitalWrite(13, "HIGH") |
31 | | - time.sleep(1) |
32 | | -``` |
33 | | -Python-Arduino-Command-API |
34 | | -For more examples, see arduino/examples.py. This file contains methods which replicate |
35 | | -the functionality of many Arduino demo sketches. |
36 | | - |
37 | | -#### Requirements: |
38 | | -* Python 2.3 or higher (Python 3.x not yet tested) |
39 | | -* PySerial |
40 | | -* Arduino compatible microcontroller with at least 14KB of memory |
41 | | - |
42 | | -#### Setup: |
43 | | -1. Verify that your Arduino board communicates at the baud rate specified in the setup() function in prototype.ino. Change it if necessary. |
44 | | -1. Load the sketch prototype.ino onto your Arduino board. |
45 | | -2. Import the included arduino library into your python script. |
46 | | - |
47 | | - |
48 | | -## Classes |
49 | | -*Arduino(baud)* - Set up communication with currently connected and powered Arduino. |
50 | | -```python |
51 | | -board = Arduino("9600") #Example |
52 | | -``` |
53 | | -The device name / COM port of the connected Arduino will be auto-detected. If there are more than one Arduino boards connected, |
54 | | -the desired COM port can be also be passed as an optional argument: |
55 | | -```python |
56 | | -board = Arduino("9600", port="COM3") #Windows example |
57 | | -``` |
58 | | -```python |
59 | | -board = Arduino("9600", port="/dev/tty.usbmodemfa141") #OSX example |
60 | | -``` |
61 | | -A time-out for reading from the Arduino can also be specified as an optional argument: |
62 | | -```python |
63 | | -board = Arduino("9600", timeout=2) #Serial reading functions will wait for no more than 2 seconds |
64 | | -``` |
65 | | - |
66 | | -*SoftwareSerial()* - A class for handling software serial functionality. Is used internally by the Arduino class, and should not be called directly. |
67 | | - |
68 | | -## Methods |
69 | | - |
70 | | -**Digital I/O** |
71 | | - |
72 | | -**Arduino.digitalWrite(pin_number, state)* - turn digital pin on/off |
73 | | -**Arduino.digitalRead(pin_number)* - read state of a digital pin |
74 | | - |
75 | | -```python |
76 | | -#Digital read / write example |
77 | | -board.digitalWrite(13, "HIGH") #Set digital pin 13 voltage |
78 | | -state_1 = board.digitalRead(13) #Will return integer 1 |
79 | | -board.digitalWrite(13, "LOW") #Set digital pin 13 voltage |
80 | | -state_2 = board.digitalRead(13) #Will return integer 0 |
81 | | -``` |
82 | | - |
83 | | -**Arduino.pinMode(pin_number, io_mode)* - set pin I/O mode |
84 | | -**Arduino.pulseIn(pin_number, state)* - measures a pulse |
85 | | -**Arduino.pulseIn_set(pin_number, state)* - measures a pulse, with preconditioning |
86 | | - |
87 | | -```python |
88 | | -#Digital mode / pulse example |
89 | | -board.pinMode(7, "INPUT") #Set digital pin 7 mode to INPUT |
90 | | -duration = board.pulseIn(7, "HIGH") #Return pulse width measurement on pin 7 |
91 | | -``` |
92 | | - |
93 | | -**Analog I/O** |
94 | | - |
95 | | -**Arduino.analogRead(pin_number)* - returns the analog value |
96 | | -**Arduino.analogWrite(pin_number, value)* - sets the analog value |
97 | | - |
98 | | -```python |
99 | | -#Analog I/O examples |
100 | | -val=board.analogRead(5) #Read value on analog pin 5 (integer 0 to 1023) |
101 | | -val = val /4# scale to 0 - 255 |
102 | | -board.analogWrite(11) #Set analog value (PWM) based on analog measurement |
103 | | -``` |
104 | | - |
105 | | -**Software Serial Functionality** |
106 | | - |
107 | | -**Arduino.SoftwareSerial.begin(ss_rxPin,ss_txPin,ss_device_baud)* - initialize software serial device on |
108 | | -specified pins. |
109 | | -Only one sofware serial device can be used at a time. Existing software serial instance will |
110 | | -be be overwritten by calling this method, both in Python and on the arduino board. |
111 | | -**Arduino.SoftwareSerial.write(data)* - send data using the arduino 'write' function to the existing software serial connection. |
112 | | -**Arduino.SoftwareSerial.read()* - returns one byte from the existing software serial connection |
113 | | - |
114 | | -```python |
115 | | -#Software serial example |
116 | | -board.SoftwareSerial.begin(0,7,"19200") # Start software serial for transmit only (tx on pin 7) |
117 | | -board.SoftwareSerial.write(" test ") #Send some data |
118 | | -response_char = board.SoftwareSerial.read() #read response character |
119 | | -``` |
120 | | - |
121 | | -**Misc** |
122 | | - |
123 | | -**Arduino.close()* - closes serial connection to the Arduino. |
124 | | - |
125 | | -## To-do list: |
126 | | -* Expand software serial functionality (print() and println()) |
127 | | -* Add simple reset functionality that zeros out all pin values |
128 | | -* Add I2C / TWI function support (Arduino Wire.h commands) |
129 | | -* Add Servo support (Arduino Servo.h commands) |
130 | | -* Add tone() / noTone() squarewave generator support for piezo type speakers |
131 | | -* Include a wizard which generates 'prototype.ino' with selected serial baud rate and Arduino function support (to help reduce memory requirements). |
| 1 | +# Python Arduino Command API |
| 2 | + |
| 3 | +> © 2012-2013 Tristan A. Hearn <[email protected]> |
| 4 | +> under the MIT License |
| 5 | +
|
| 6 | +Based in part on the Python Arduino Prototyping API by Akash Manohar (https://github.com/HashNuke/Python-Arduino-Prototyping-API/). |
| 7 | + |
| 8 | +The Python Arduino Command API is a light-weight Python package for communicating with Arduino microcontroller boards. It is written |
| 9 | +using a custom protocol, similar to Firmata (http://firmata.org/wiki/Main_Page). This allows a user to quickly protoype programs |
| 10 | +for Arduino or to simply read and control harware connected to an Arduino from a host computer, without having to reload sketches onto an Arduino board. |
| 11 | + |
| 12 | +Method names within the Python Arduino Command API are designed to be as close as possible to their Arduino programming language counterparts. |
| 13 | + |
| 14 | +## Usage example |
| 15 | +```python |
| 16 | +#!/usr/bin/env python |
| 17 | +""" |
| 18 | + Blinks an LED on digital pin 13 |
| 19 | + in 1 second intervals |
| 20 | +""" |
| 21 | + |
| 22 | +from arduino import Arduino |
| 23 | +import time |
| 24 | + |
| 25 | +board = Arduino('9600') |
| 26 | + |
| 27 | +whileTrue: |
| 28 | + board.digitalWrite(13, "LOW") |
| 29 | + time.sleep(1) |
| 30 | + board.digitalWrite(13, "HIGH") |
| 31 | + time.sleep(1) |
| 32 | +``` |
| 33 | +Python-Arduino-Command-API |
| 34 | +For more examples, see arduino/examples.py. This file contains methods which replicate |
| 35 | +the functionality of many Arduino demo sketches. |
| 36 | + |
| 37 | +#### Requirements: |
| 38 | +* Python 2.3 or higher (Python 3.x not yet tested) |
| 39 | +* PySerial |
| 40 | +* Arduino compatible microcontroller with at least 14KB of memory |
| 41 | + |
| 42 | +#### Setup: |
| 43 | +1. Verify that your Arduino board communicates at the baud rate specified in the setup() function in prototype.ino. Change it if necessary. |
| 44 | +1. Load the sketch prototype.ino onto your Arduino board. |
| 45 | +2. Import the included arduino library into your python script. |
| 46 | + |
| 47 | + |
| 48 | +## Classes |
| 49 | +*Arduino(baud)* - Set up communication with currently connected and powered Arduino. |
| 50 | +```python |
| 51 | +board = Arduino("9600") #Example |
| 52 | +``` |
| 53 | +The device name / COM port of the connected Arduino will be auto-detected. If there are more than one Arduino boards connected, |
| 54 | +the desired COM port can be also be passed as an optional argument: |
| 55 | +```python |
| 56 | +board = Arduino("9600", port="COM3") #Windows example |
| 57 | +``` |
| 58 | +```python |
| 59 | +board = Arduino("9600", port="/dev/tty.usbmodemfa141") #OSX example |
| 60 | +``` |
| 61 | +A time-out for reading from the Arduino can also be specified as an optional argument: |
| 62 | +```python |
| 63 | +board = Arduino("9600", timeout=2) #Serial reading functions will wait for no more than 2 seconds |
| 64 | +``` |
| 65 | + |
| 66 | +*SoftwareSerial()* - A class for handling software serial functionality. Is used internally by the Arduino class, and should not be called directly. |
| 67 | + |
| 68 | +## Methods |
| 69 | + |
| 70 | +**Digital I/O** |
| 71 | + |
| 72 | +**Arduino.digitalWrite(pin_number, state)* - turn digital pin on/off |
| 73 | +**Arduino.digitalRead(pin_number)* - read state of a digital pin |
| 74 | + |
| 75 | +```python |
| 76 | +#Digital read / write example |
| 77 | +board.digitalWrite(13, "HIGH") #Set digital pin 13 voltage |
| 78 | +state_1 = board.digitalRead(13) #Will return integer 1 |
| 79 | +board.digitalWrite(13, "LOW") #Set digital pin 13 voltage |
| 80 | +state_2 = board.digitalRead(13) #Will return integer 0 |
| 81 | +``` |
| 82 | + |
| 83 | +**Arduino.pinMode(pin_number, io_mode)* - set pin I/O mode |
| 84 | +**Arduino.pulseIn(pin_number, state)* - measures a pulse |
| 85 | +**Arduino.pulseIn_set(pin_number, state)* - measures a pulse, with preconditioning |
| 86 | + |
| 87 | +```python |
| 88 | +#Digital mode / pulse example |
| 89 | +board.pinMode(7, "INPUT") #Set digital pin 7 mode to INPUT |
| 90 | +duration = board.pulseIn(7, "HIGH") #Return pulse width measurement on pin 7 |
| 91 | +``` |
| 92 | + |
| 93 | +**Analog I/O** |
| 94 | + |
| 95 | +**Arduino.analogRead(pin_number)* - returns the analog value |
| 96 | +**Arduino.analogWrite(pin_number, value)* - sets the analog value |
| 97 | + |
| 98 | +```python |
| 99 | +#Analog I/O examples |
| 100 | +val=board.analogRead(5) #Read value on analog pin 5 (integer 0 to 1023) |
| 101 | +val = val /4# scale to 0 - 255 |
| 102 | +board.analogWrite(11) #Set analog value (PWM) based on analog measurement |
| 103 | +``` |
| 104 | + |
| 105 | +**Servo Library Functionality** |
| 106 | +Support is included for up to 8 servos. |
| 107 | + |
| 108 | +**Arduino.Servo.attach(pin, min = 544, max = 2400)* - Create servo instance. Only 8 servos can be used at one time. |
| 109 | +**Arduino.Servo.read(pin)* - Returns the angle of the servo attached to the specified pin |
| 110 | +**Arduino.Servo.write(pin, angle)* - Move an attached servo on a pin to a specified angle |
| 111 | +**Arduino.Servo.writeMicroseconds(pin, uS)* - Write a value in microseconds to the servo on a specified pin |
| 112 | +**Arduino.Servo.detach(pin)* - Detaches the servo on the specified pin |
| 113 | + |
| 114 | +```python |
| 115 | +#Servo example |
| 116 | +board.Servo.attach(9) #declare servo on pin 9 |
| 117 | +board.Servo.attach(10) #declare servo on pin 10 |
| 118 | +board.Servo.write(9, 0) |
| 119 | +board.Servo.write(10, 180) |
| 120 | +angle_1 = board.Servo.read(9) # should be 0 |
| 121 | +angle_2 = board.Servo.read(10) # should be 180 |
| 122 | +board.Servo.detach(9) #free pin 9 |
| 123 | +board.Servo.detach(10) #free pin 10 |
| 124 | +``` |
| 125 | + |
| 126 | +**Software Serial Functionality** |
| 127 | + |
| 128 | +**Arduino.SoftwareSerial.begin(ss_rxPin,ss_txPin,ss_device_baud)* - initialize software serial device on |
| 129 | +specified pins. |
| 130 | +Only one sofware serial device can be used at a time. Existing software serial instance will |
| 131 | +be be overwritten by calling this method, both in Python and on the arduino board. |
| 132 | +**Arduino.SoftwareSerial.write(data)* - send data using the arduino 'write' function to the existing software serial connection. |
| 133 | +**Arduino.SoftwareSerial.read()* - returns one byte from the existing software serial connection |
| 134 | + |
| 135 | +```python |
| 136 | +#Software serial example |
| 137 | +board.SoftwareSerial.begin(0,7,"19200") # Start software serial for transmit only (tx on pin 7) |
| 138 | +board.SoftwareSerial.write(" test ") #Send some data |
| 139 | +response_char = board.SoftwareSerial.read() #read response character |
| 140 | +``` |
| 141 | + |
| 142 | +**Misc** |
| 143 | + |
| 144 | +**Arduino.close()* - closes serial connection to the Arduino. |
| 145 | + |
| 146 | +## To-do list: |
| 147 | +* Expand software serial functionality (print() and println()) |
| 148 | +* Add simple reset functionality that zeros out all pin values |
| 149 | +* Add I2C / TWI function support (Arduino Wire.h commands) |
| 150 | +* Add tone() / noTone() squarewave generator support for piezo type speakers |
| 151 | +* Include a wizard which generates 'prototype.ino' with selected serial baud rate and Arduino function support (to help reduce memory requirements). |
132 | 152 | * Multi-serial support for Arduino mega (Serial1.read(), etc) |
0 commit comments