本帖最后由 alanlan86 于 2023-10-10 12:33 编辑
#综合任务报告
##一、背景
- 适逢2023的中秋+国庆,双节同庆!学校给家里的小孩布置了一个科技节的手工作业,由此,顺便动手把这个节日氛围灯手搓了一把!
###功能
- 构想了一下主要的功能有:
- A、可以根据光的强弱自动打开和调节亮度
- B、用I2C读取G-Sensor的数值,判断是否有“拍一拍”交互
- C、可以播放SD卡中存储的wav音乐文件
##二、硬件介绍
###A、硬件框图
###主控

##SD卡
- 外围有SD卡(卡座+TF卡),用于存储wav音乐文件;ESP32-S3与SD卡采用的SPI接口进行连接。

##音频DAC
- 以I2S接口的音频DAC芯片MAX98357,外接一个3W 4欧姆的喇叭,进行音乐播放;MAX98357A无需I2C对其寄存器进行配置,只需通过I2S将音频还原的数据进行传输即可,非常简便易用。

##传感器
- 以GSensor加速度传感器LIS3DH和模拟光感传感器模块进行环境检测和交互动作检测。LIS3DH是一颗非常常见通用的加速度传感器,通过I2C即可读取数据和控制。模拟的光感传感器,需要采用ESP32-S3的ADC通道进行环境光强度的采集。

##WS2812灯带
- 根据环境光情况和交互动作,对WS2812B进行RGB颜色调节或者节律控制。WS2812B是一个单线串行控制的LED驱动芯片,同时带LED灯珠,引出对于串行连接和控制非常方便好用。
##引脚连接
| ESP32 |模块 | 说明 |
| ------------ | ------------ | ------------ |
| D5 | BCLK |时钟信号 |
| D6 | LRC | 声道信号 |
| D9 | DIN | 数据信号 |
| A5 | SD_CS | SD卡片选 |
| SCK | SD_SCK | SD时钟线 |
| MO | SD_MOSI | SD MOSI |
| MI | SD_MISO | SD MISO |
| A0 | 光传感器Analog | 环境光传感器模拟输出 |
| SCL | LIS3DH SCL | I2C SCL |
| SDA | LIS3DH SDA | I2C SDA |
| D13 | WS2812B | 灯带控制 |
各个模块的电源引脚按照VCC 3.3V进行连接,其中WS2812B的电源按照5V进行连接。
###B、软件介绍
- 软件方面为了让代码编写架构更加清晰,采用了多任务方式进行编写。通过引入import asyncio 库,然后分音乐播放、环境光采集、动作检测以及灯控几个任务划分模块。
####SD卡控制
- 需要使用到circuitpython到的库有:
import storage
import adafruit_sdcard
- SD卡接口初始化以及挂载文件系统。实现FS文件系统挂载之后,对文件的读写,直接通过/sd进行访问即可。
SD_CS = board.A5
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
####I2S播放
- 音频的播放用到的库有:
import audiocore
import audiobusio
- 音频播放任务代码:
audio = audiobusio.I2SOut(bit_clock=board.D5, word_select=board.D6, data=board.D9)
async def AudioPlayer():
while True:
with open("/audio/wangfengxinlicui3.wav", "rb") as wave_file:
wav = audiocore.WaveFile(wave_file)
print("Playing wav file!")
audio.play(wav)
while audio.playing:
time.sleep(1)
pass
print("Done!")
####环境光检测
- 针对环境光检测通过调用import analogio 模拟IO ADC库,进行光强度检测。
async def lightbrighneess(controls):
while True:
controls.brightness = (analog_pin.value/61086)*100
pixel.brightness = (100 - controls.brightness)*1.0/100
await asyncio.sleep(3)
####动作检测
- 动作检测部分,通过import adafruit_lis3dh 库,读取LIS3DH的加速度值,然后针对3轴的绝对加速度是否大于1g,或求和大于1g判断是否有交互动作。
async def motion_detect(controls):
while True:
x, y, z = [
value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration
]
if ((abs(x + y + z)>1) or (abs(x) > 1.2) or (abs(y) > 1.2) or (abs(z) > 1.2)):
controls.touch = True
await asyncio.sleep(0.5)
####灯光控制
- 通过from rainbowio import colorwheel和import neopixel 两个库,可以很便捷地控制WS2812灯带的点亮效果。
async def blink(controls):
"""Blink animation on ."""
while True:
for color in range(255):
if (controls.brightness > 0):
if (controls.touch == False):
rainbow(controls.delay, False, color)
await asyncio.sleep(0.05)
else:
for i in range(0,49):
pixel<i> = (0, 0, 0)
await asyncio.sleep(0.5)
for i in range(0,49):
pixel<i> = (255, 255, 255)
await asyncio.sleep(2.5)
controls.touch = False
####主函数
async def main():
animation_controls = AnimationControls()
lightsensor_task = asyncio.create_task(lightbrighneess(animation_controls))
blink_task = asyncio.create_task(blink(animation_controls))
audio_task = asyncio.create_task(AudioPlayer())
motion_task = asyncio.create_task(motion_detect(animation_controls))
await asyncio.gather(blink_task, lightsensor_task, motion_task, audio_task)
asyncio.run(main())
#源码
#视频展示
播放器加载失败: 未检测到Flash Player,请到
安装
节日氛围灯
#必做任务帖汇总:
任务1:
https://bbs.eeworld.com.cn/thread-1254356-1-1.html
任务2:
https://bbs.eeworld.com.cn/thread-1254305-1-1.html
任务3:
https://bbs.eeworld.com.cn/thread-1255110-1-1.html
任务4:
https://bbs.eeworld.com.cn/thread-1254703-1-1.html
https://bbs.eeworld.com.cn/thread-1255865-1-1.html
https://bbs.eeworld.com.cn/thread-1255295-1-1.html
任务5:
https://bbs.eeworld.com.cn/thread-1256691-1-1.html
其他:
【得捷电子Follow me第2期】Adafruit ESP32-S3 TFT Feather选购,小心板载BME280!! - DigiKey得捷电子技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
https://bbs.eeworld.com.cn/thread-1251802-1-1.html
【得捷电子Follow me第2期】+开箱帖:期待的Adafruit ESP32-S3 TFT Feather到货啦.... - DigiKey得捷电子技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
https://bbs.eeworld.com.cn/thread-1253411-1-1.html
【得捷电子Follow me第2期】+初体验帖:CircuitPython上手 - DigiKey得捷电子技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
https://bbs.eeworld.com.cn/thread-1253511-1-1.html
【得捷电子Follow me第2期】+上手帖:CircuitPytho Bundle代码包使用 - DigiKey得捷电子技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
https://bbs.eeworld.com.cn/thread-1253570-1-1.html