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()
等)的多串行支持