Arduino Firmata + PyFirmata

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)

其他实验后续测试

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值