本文主要介绍基于Arduino实现的音乐频谱显示,音乐频谱原理就是声音传感器(MIC)接收音频信号后通过FFT将时域信号转换成频域信号,再将音频信号频域分量分别显示在对应的LED点阵屏上,呈现出音乐随频律动的感觉,整体效果比较赏心悦目
完整代码见文末链接(含相关头文件)
(1)简介
首先介绍实现本文所需要的主要器件:
(1)Arduino开发板:全系列均可以,本文主要以UNO为例
(2)MAX7219 LED点阵模块
有关于该模块的详细介绍见:https://blog.csdn.net/u011816009/article/details/124309755
(3)声音传感器:要求能模拟量输出,理论上只要能输出模拟量的都可以,我是电子大楼随意几块钱买了个,也能正常使用
(2)接线图
接线图相对比较简单,按照下图示意接线即可
(3)程序代码
初始化配置
include "LedControl.h"
/* Led matrix - Max7219 Declared */
LedControl lc = LedControl(7, 6, 5, 1);
const int maxScale = 50;
/* Sensor - Max9812 Declared */
const int sensorPin = A4;
const int sampleWindow = 50; // 50ms = 20Hz
unsigned int sample;
主函数如下
void loop() {
startMillis = millis();
//peakToPeak = 0;
signalMax = 0;
signalMin = 255;
// Get data in 50ms
while (millis() - startMillis < sampleWindow) {
sample = analogRead(sensorPin);
if (sample < 255) {
if (sample > signalMax) {
signalMax = sample;
}
if (sample < signalMin) {
signalMin = sample;
}
}
// 20Hz - 64Hz - 125Hz - 250Hz - 500Hz - 1kHz (timeCycle = 1/F)(ms)
timeCycle = millis() - startMillis;
if (timeCycle == 1 || timeCycle == 2 || timeCycle == 4 || timeCycle == 8
|| timeCycle == 16 || timeCycle == 32 || timeCycle == 40 || timeCycle == 50) {
signalMaxBuff[index] = signalMax;
signalMinBuff[index] = signalMin;
index = (index + 1) % 8;
delay(1);
//Serial.println(timeCycle);
}
}
// Delete pointer to array
index = 0;
// Calculation after get samples
for (int i = 0; i < 8; i++) { // i = row (led matrix)
// sound level
peakToPeak[i] = signalMaxBuff[i] - signalMinBuff[i];
// Map 1v p-p level to the max scale of the display
displayPeak[i] = map(peakToPeak[i], 0, 254, 0, maxScale);
// Show to led matrix
displayLed(displayPeak[i], i);
// Led drop down
if (displayPeak[i] >= temp[i]) {
temp[i] = displayPeak[i];
}
else {
temp[i]--;
}
(4)视频效果展示
Arduino音乐频谱
(5)附完整代码(含所需头文件等)
百度网盘下载:
链接:https://pan.baidu.com/s/15uyKs5nhGCX9W46Yd7j4FA
提取码:c457
CSDN下载:
https://download.csdn.net/download/u011816009/87177570