本文主要是介绍物联网开发113 - Micropython ESP32 C3连接PAJ7620手势传感器控制WS2812彩灯,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、目的
这一节我们来学习如何使用合宙ESP32 C3,连接0.96寸ssd1306屏幕、PAJ7620手势传感和WS2812 5050 RGB LED 全彩驱动彩灯,进行一个小实验。
二、环境
ESP32 C3开发板(MicroPython v1.19.1 on 2022-06-18)+ I2C 0.96寸ssd1306屏幕模块+ PAJ7620手势传感器 + WS2812 5050 RGB LED 全彩驱动彩灯 + 几根杜邦线 + Win10商业版
ESP32 C3和各个模块接线方法:
三、示例代码
examples.py
from machine import Pin,I2C
from neopixel import NeoPixel
from paj7260 import PAJ7260
from ssd1306 import SSD1306_I2C
from ufont import BMFont
import timei2c = I2C(0,scl = Pin(5),sda = Pin(4),freq = 400_000)
oled = SSD1306_I2C(128,64,i2c,0X3C)
ws = NeoPixel(Pin(8,Pin.OUT),64)
paj = PAJ7260(i2c)font = BMFont("fonts/unifont-14-12888-16.v3.bmf")#print("%#X"%(i2c.scan()[1]))def Hxd():#从暗到鲜艳for i in range(0,255,1):ws.fill((i,i,i))ws.write()time.sleep(0.01)#从鲜艳到暗for i in range(255,-1,-1):ws.fill((i,i,i))ws.write()time.sleep(0.01)ws.fill((0,0,0))ws.write()time.sleep(1)def WS2812B():Get = paj.return_gesture()code = [[19,26,33,34,35,36,37,38,42,51], #右0[20,29,33,34,35,36,37,38,45,52], #左1[11,18,19,20,25,27,29,35,43,51], #上2[11,19,27,33,35,37,42,43,44,51], #下3[11,18,20,25,29,35,42,44,49,53], #前4[09,13,18,20,27,33,37,42,44,51], #后5[17,21,26,28,35,42,44,49,53,53], #波6]if Get != 0:print("%d"%(Get))#右到左if Get == 4:ws.fill((0,0,0))ws.write()for i in range(len(code[0])):ws[code[0][i]]=(0,200,0)ws.write()#左到右 elif Get == 3:ws.fill((0,0,0))ws.write()for i in range(len(code[1])):ws[code[1][i]]=(0,200,0)ws.write()#上 elif Get == 5:ws.fill((0,0,0))ws.write()for i in range(len(code[2])):ws[code[2][i]]=(0,200,0)ws.write()oled.clear()font.text(oled,"向上",24,24,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True,auto_wrap=True)#下 elif Get == 6:ws.fill((0,0,0))ws.write()for i in range(len(code[3])):ws[code[3][i]]=(0,200,0)ws.write()oled.clear()font.text(oled,"向下",24,24,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True,auto_wrap=True)#前 elif Get == 1:ws.fill((0,0,0))ws.write()for i in range(len(code[4])):ws[code[4][i]]=(0,200,0)ws.write()#后 elif Get == 2:ws.fill((0,0,0))ws.write()for i in range(len(code[5])):ws[code[5][i]]=(0,200,0)ws.write()#波浪 elif Get == 9:ws.fill((0,0,0))ws.write()for i in range(len(code[6])):ws[code[6][i]]=(0,200,0)ws.write()oled.clear()font.text(oled,"挥手",24,24,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True,auto_wrap=True)time.sleep(1)else:ws.fill((10,0,10))ws.write()def main():font.text(oled,"手势传感器",24,24,color=1,font_size=16,reverse=False,clear=False,show=True,half_char=True,auto_wrap=True)Hxd()while True:WS2812B()if __name__ == "__main__":main()
演示效果:
四、WS2812驱动
Micropython集成自带的,无需另外导入。
from neopixel import NeoPixel
五、ssd1306和字库文件
请查看这个文章获取:
物联网开发107 - Micropython ESP32 C3接DS1307时钟模块在SSD1306屏幕上显示时间_魔都飘雪的博客-CSDN博客Micropython ESP32 C3连接DS1307时钟模块在SSD1306屏幕上显示时间https://blog.csdn.net/zhusongziye/article/details/130535482?spm=1001.2014.3001.5501六、WS2812 5050 RGB LED 全彩驱动彩灯 正方形开发板购买
https://item.taobao.com/item.htm?spm=a1z09.2.0.0.2f582e8d4Jxt4i&id=605669316457&_u=mp01rch2294https://item.taobao.com/item.htm?spm=a1z09.2.0.0.2f582e8d4Jxt4i&id=605669316457&_u=mp01rch2294
这篇关于物联网开发113 - Micropython ESP32 C3连接PAJ7620手势传感器控制WS2812彩灯的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!