2018-03-2809:20:44
arduino中有相应的库
1.安装pyFirmata包
pip install pyFirmata
在python2.7或python3.X下都可以执行。
pyFirmata包实现了python对Arduino进行控制的功能,它与Arduino之间通信遵循的是Firmata协议
如果pyton下运行from pyfirmata import Arduino不报错,证明安装成功
2.在Arduino IDE中为Arduino写入程序Standard Firmata.
from pyfirmata import Arduino, util import time board = Arduino('COM8') #Arduino所在串口是COM8,这是windows下的表示 while 1: board.digital[13].write(0) #向端口13写入0 time.sleep(1) board.digital[13].write(1) #向端口13写入1 time.sleep(1)
更新:驱动舵机源码:
from pyfirmata import Arduino, util import time board = Arduino('COM3') #Arduino所在串口是COM8,这是windows下的表示 while 1: """ board.digital[13].write(0) #向端口13写入0 time.sleep(1) board.digital[13].write(1) #向端口13写入1 time.sleep(1) """ board.servo_config(13,0,255,20) print("ceshi") time.sleep(1) board.servo_config(13, 0, 255, 255) time.sleep(1)