GitHub - tino/pyFirmata: Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet. - GitHub - tino/pyFirmata: Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.
https://github.com/tino/pyFirmata简单说就是把 arduino 板子当作一个外设,由PC直接接管,不需要 Arduino编程。
实验1:
- # 第一个实验 闪灯 https://www.jianshu.com/p/cbe5eacc6a2b
- # 用 Arduino IDE 为 ArduinoUNO 写入官方自带例程 Firmata ---> Standard Firmata
- # python 安装所需的库 pip install pyfirmata
- # 下面代码复制进 python 并运行
-
- from pyfirmata import Arduino, util
- import time
- board = Arduino('COM3') # UNO 所使用的串口号
-
- while 1:
- board.digital[13].write(1)
- time.sleep(0.2)
- board.digital[13].write(0)
- time.sleep(0.2)
- board.digital[13].write(1)
- time.sleep(0.2)
- board.digital[13].write(0)
- time.sleep(0.2)
- board.digital[13].write(1)
- time.sleep(0.2)
- board.digital[13].write(0)
- time.sleep(1)
python 有很多开发环境选择,比如下图这个 jupyterlab

-------------------------------------------------------------------------------------------------------------------
实验2:
- # 呼吸灯 ,找个LED接数字11脚和地,临时试试不用电阻什么的,不会烧,
- # UNO板载LED是13,不支持PWM
-
- import time
- import pyfirmata
-
- delay = 0.1
- brightness = 0
- board = pyfirmata.Arduino("com11")
- led = board.get_pin('d:11:p')
-
- while True:
- # increase
- for i in range(0, 10):
- brightness = brightness + 0.1
- print ("Setting brightness to %s" % brightness)
- led.write(brightness)
- board.pass_time(delay)
-
- # decrease
- for i in range(0, 10):
- print ("Setting brightness to %s" % brightness)
- led.write(brightness)
- brightness = brightness - 0.1
- board.pass_time(delay)
其他实验后续测试