用Python利用pyFirmata控制Arduino實現Blink

2018-03-2809:20:44python

arduino中有相應的庫git

1.安裝pyFirmata包windows

    pip install pyFirmatapython2.7

   在python2.7或python3.X下均可以執行。ui

   pyFirmata包實現了python對Arduino進行控制的功能,它與Arduino之間通訊遵循的是Firmata協議code

   若是pyton下運行from pyfirmata import Arduino不報錯,證實安裝成功blog

2.在Arduino IDE中爲Arduino寫入程序Standard Firmata.ip

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)
相關文章
相關標籤/搜索