Raspberry Pi Pico使用CricuitPython---(2)
2021/12/15 9:16:57
本文主要是介绍Raspberry Pi Pico使用CricuitPython---(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在前面使用板载LED点亮实现了闪烁效果, 另外通过外接LED也可以实现LED 的闪烁.
接着实验点亮全彩 NeoPixel.
这个模块并不在默认的安装之内,需要在文件头部分加载包,
1. 加载包,点亮灯
首先在 LibrariesThe easiest way to program microcontrollershttps://circuitpython.org/libraries下载 合适的包 比如adafruit-circuitpython-bundle-7.x-mpy-20211214.zip
加压后用文件传输方式导入到 pico 的lib 文件夹下, 这里选择 NeoPixel.mpy
接下来就是连线, 将 VCC 接入 VBUS(40引脚), 将 GND 接入 GND (38引脚), 将数据线接入 GPIO中的任何一个, 比如 GPIO0, 数一下 neopixel 中有几颗灯.
''' NeoPixel example for Pico. Turns the NeoPixels red. REQUIRED HARDWARE: * RGB NeoPixel LEDs connected to pin GP0. ''' import board import neopixel # Update this to match the number of NeoPixel LEDs connected to your board. num_pixels = 30 pixels = neopixel.NeoPixel(board.GP0, num_pixels) pixels.brightness = 0.5 while True: pixels.fill((255, 0, 0))
然后, 在MU编辑器编程, 点亮所有的 LED. 这里选RGB颜色为红色 (255,0,0). 其中的
num_pixels是最大灯数量, 你可以不点亮其中的一些灯. pixels.brightness 则是定义灯的亮度百分比.
2. 彩虹效果
实现彩虹效果则需要 rainbowio 这个包
""" NeoPixel example for Pico. Displays a rainbow on the NeoPixels. REQUIRED HARDWARE: * RGB NeoPixel LEDs connected to pin GP0. """ import time import board from rainbowio import colorwheel import neopixel # Update this to match the number of NeoPixel LEDs connected to your board. num_pixels = 30 pixels = neopixel.NeoPixel(board.GP0, num_pixels, auto_write=False) pixels.brightness = 0.5 def rainbow(speed): for j in range(255): for i in range(num_pixels): pixel_index = (i * 256 // num_pixels) + j pixels[i] = colorwheel(pixel_index & 255) pixels.show() time.sleep(speed) while True: rainbow(0)
实际上colorwheel这个彩虹效果也可以从内建的 _pixelbuf
模块导入
from _pixelbuf import colorwheel pixels = neopixel.NeoPixel(board.GP0, num_pixels, auto_write=False) def rainbow(speed): for j in range(255): for i in range(num_pixels): pixel_index = (i * 256 // num_pixels) + j pixels[i] = colorwheel(pixel_index & 255) pixels.show() time.sleep(speed) while True: rainbow(0)
要想开机就运行这些代码, 把以上的程序保存在根目录下的code.py中即可实现开机运行.
这篇关于Raspberry Pi Pico使用CricuitPython---(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础
- 2024-09-2610 种方法写出更好的 Python 代码
- 2024-09-25Python编程基础详解
- 2024-09-25Python编程入门教程
- 2024-09-25从零开始使用Python构建LLaMA 3
- 2024-09-23Python中理解和使用树形结构的简单教程
- 2024-09-23Python 编程基础入门
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战