Python Arduino命令API是一個輕巧的Python庫,用於使用標準串行IO通過物理電線或無線電線從連接的計算機與Arduino微控制器板進行通信。它是使用類似於Firmata的自定義協議編寫的。
這允許用戶使用Python代碼快速為Arduino快速使用Protoype程序,或者簡單地讀取/控制/故障排除/實驗與Arduino Board連接的HARWARE,而無需重新編譯和重新加載草圖到板本身。
Python Arduino命令API中的方法名稱旨在盡可能接近其Arduino編程語言對應語
#!/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 )
從命令行中運行pip install arduino-python
,或從源目錄中運行python setup.py build install
以安裝此庫。
setup()
函數( prototype.ino
348行)中指定的baud速率進行通信。如有必要,在那裡更改。prototype.ino
。from Arduino import Arduino
添加到您的Python腳本中以與您的Arduino通信有關示例的集合,請參見examples.py
。該文件包含複製許多Arduino演示草圖功能的方法。
tests
目錄包含庫的一些基本測試。對於每個版本,很難期望廣泛的代碼覆蓋範圍,因為正面測試涉及實際連接並發出命令到Live Arduino,並託管了測試特定功能所需的任何硬件。但是,在合併到master
分支之前,至少應在此處維護並使用基本通信測試的核心。
安裝後,交互式測試可以從源目錄進行:
$ python tests/test_main.py
自動化測試可以從源目錄中運行:
$ python tests/test_arduino.py
Arduino(baud)
- 與當前連接和動力的Arduino進行通信。 board = Arduino ( "9600" ) #Example
連接的Arduino的設備名稱 / COM端口將被自動檢測。如果連接了一個以上的Arduino板,則可以作為可選參數傳遞所需的COM端口:
board = Arduino ( "9600" , port = "COM3" ) #Windows example
board = Arduino ( "9600" , port = "/dev/tty.usbmodemfa141" ) #OSX example
從Arduino閱讀的超時也可以指定為可選參數:
board = Arduino ( "9600" , timeout = 2 ) #Serial reading functions will
#wait for no more than 2 seconds
數字I/O。
Arduino.digitalWrite(pin_number, state)
打開/關閉數字別針Arduino.digitalRead(pin_number)
讀取數字別針的狀態 #Digital read / write example
board . digitalWrite ( 13 , "HIGH" ) #Set digital pin 13 voltage
state_1 = board . digitalRead ( 13 ) #Will return integer 1
board . digitalWrite ( 13 , "LOW" ) #Set digital pin 13 voltage
state_2 = board . digitalRead ( 13 ) #Will return integer 0
Arduino.pinMode(pin_number, io_mode)
設置引腳i/o模式Arduino.pulseIn(pin_number, state)
測量脈衝Arduino.pulseIn_set(pin_number, state)
測量脈衝,並進行預處理 #Digital mode / pulse example
board . pinMode ( 7 , "INPUT" ) #Set digital pin 7 mode to INPUT
duration = board . pulseIn ( 7 , "HIGH" ) #Return pulse width measurement on pin 7
模擬I/O。
Arduino.analogRead(pin_number)
返回模擬值Arduino.analogWrite(pin_number, value)
設置模擬值 #Analog I/O examples
val = board . analogRead ( 5 ) #Read value on analog pin 5 (integer 0 to 1023)
val = val / 4 # scale to 0 - 255
board . analogWrite ( 11 ) #Set analog value (PWM) based on analog measurement
換檔
Arduino.shiftIn(dataPin, clockPin, bitOrder)
移動一個字節並返回Arduino.shiftOut(dataPin, clockPin, bitOrder, value)
將給定的字節移出bitOrder
應該是"MSBFIRST"
或"LSBFIRST"
伺服庫功能支持包括多達8個伺服器。
Arduino.Servos.attach(pin, min=544, max=2400)
創建伺服實例。一次只能使用8個伺服器。Arduino.Servos.read(pin)
返回附加到指定引腳的伺服的角度Arduino.Servos.write(pin, angle)
將附著的伺服器移至指定角度Arduino.Servos.writeMicroseconds(pin, uS)
在指定的PIN上以微秒為單位寫一個值Arduino.Servos.detach(pin)
在指定的引腳上拆下伺服器 #Servo example
board . Servos . attach ( 9 ) #declare servo on pin 9
board . Servos . write ( 9 , 0 ) #move servo on pin 9 to 0 degrees
print board . Servos . read ( 9 ) # should be 0
board . Servos . detach ( 9 ) #free pin 9
軟件序列功能
Arduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud)
在指定的引腳上初始化軟件串行設備。一次只能使用一個SOFware串行設備。現有軟件串行實例將通過在Python和Arduino板上調用此方法來覆蓋。Arduino.SoftwareSerial.write(data)
使用arduino“寫入”函數將數據發送到現有軟件串行連接。Arduino.SoftwareSerial.read()
從現有軟件串行連接中返回一個字節 #Software serial example
board . SoftwareSerial . begin ( 0 , 7 , "19200" ) # Start software serial for transmit only (tx on pin 7)
board . SoftwareSerial . write ( " test " ) #Send some data
response_char = board . SoftwareSerial . read () #read response character
EEPROM
Arduino.EEPROM.read(address)
從EEPROM讀取字節Arduino.EEPROM.write(address, value)
將字節寫入EEPROMArduino.EEPROM.size()
返回EEPROM的大小 #EEPROM read and write examples
location = 42
value = 10 # 0-255(byte)
board . EEPROM . write ( location , 10 )
print ( board . EEPROM . read ( location ))
print ( 'EEPROM size {size}' . format ( size = board . EEPROM . size ()))
雜項
Arduino.close()
關閉與Arduino的串行連接。 print()
和println()
)Wire.h
命令)Serial1.read()
等)的多串行支持