当前位置:网站首页>How to add music playback function to Arduino project
How to add music playback function to Arduino project
2022-07-06 12:20:00 【XlinliY. Zhang】
( The following tutorials are only for Arduino UNO、Arduino Nano Etc Atmel328P As the core development board )
One 、 The buzzer plays music
Speaking of SCM playing music , The first thing you think of should be a passive buzzer or horn .( Because the working principle of the passive buzzer is the same as that of the loudspeaker , Confuse here , But there are still differences in actual use ), Whether it's 51、Arduino、 Or is it STM32, At the beginning, I will use the buzzer to play music . Passive buzzers are widely used , Small to electronic greeting cards , toy , As big as the Conrad computer of the last century , All use passive buzzer to make sound or play music .
stay Arduino in , Often use the following functions to drive the buzzer :
Tone( Buzzer pin , The frequency of the output );// Output the specified frequency at the specified pin to make the buzzer sound
noTone( Buzzer pin ); // Stop the specified pin PWM Output , Keep the buzzer silent
For this, use the way of playing music :
The advantages are :
- The cost is low , Usually you only need a buzzer , For higher requirements and higher sound requirements , Just add a triode and a resistor , Add a diode for protection .
- The circuit design is simple ,
Disadvantages are :
- Occupy a lot of resources , You need a timer for output PWM, A certain space ( Inside RAM\ROM Or outside ROM)
- It takes up most of the system time , Carry out frequency switching, etc
- because Arduino No, DAC, The music played is barely audible , Square wave output , And analog output is still quite different
- For certain IO Direct drive buzzer 、 Speaker tutorial , For the development version, it is a bomb , Light burns IO, The most important thing is to take away the development version and sensors ( Related knowledge of inductance is involved here , Interested friends can watch B standing up Roz's new video about relays )
Two 、MP3 The module plays music
In today's , domestic MP3 Modules have become very popular, such as JQ6500、JQ8900 etc. , Use MP3 The module is connected to the MCU , You can simply play music , And you can control EQ、 The volume 、 Pause playing the inserted song at will 、 Query song name 、 Song duration, etc . His operation is like old-fashioned MP3, Just connect him to the computer , Just store the song .
stay Arduino in , The following function drivers are often used MP3 modular :
// Using serial port control
dat = Serial.read();
Serial.write(dat);
// IO Or single bus control
delay(ms);
delayMicroseconds(us);
digitalWrite(io, LOW);
digitalWrite(io, HIGH);
For this, use the way of playing music :
The advantages are :
- Occupy IO Less , One IO You can play hundreds of music
- Less occupied resources ,MP3 Module support IO Trigger 、 Single bus control 、 Serial port control, etc , Even manual switch can control , There is no need to use the expensive space of MCU to store audio files .
- Good audio playback effect , Compare buzzer , It's so different
- The volume is adjustable , There is no need to re burn the program to replace the audio file
- The circuit design is simple
Disadvantages are :
- Compare buzzer , The price is really a little expensive , One set basically 10 yuan
- More power consumption , Thanks to the special power amplifier and DSP、DAC Wait for the circuit , Power consumption will be greater than buzzer
3、 ... and 、 Play music with a decoder
Use the audio decoding module + Power amplifier + Storage module , This method STM32 Common in , It is not common in low-end microcontrollers , Use VS1053 + The memory card module performs real-time audio output , Then the amplifier amplifies and pushes the horn , This method is only applicable to high-end MCU , It not only requires high performance of MCU , Design the hardware circuit , As high as developer level .
stay Arduino in , Driving code in this way is complex , Different models of decoder drive in different ways , Therefore, reference codes are not listed here .
For this, use the way of playing music :
The advantages are :
- It not only supports audio decoding , It also supports audio coding , Speaking human is to support recording and broadcasting .
- The audio quality will be better than MP3 Good module .
- Support dual channel or even higher channel number .
Disadvantages are :
- Expensive , It requires the cooperation of multiple chips
- High requirements for MCU and developers
- The circuit design is complex
Four 、 summary
Above all
stay 2021 Use buzzer to play music , Not only trouble , You need to convert audio files into frequency files , It also wastes the expensive storage space of single chip microcomputer ( If you use a memory card , Memory cards also require memory card modules , The price will come up , Use EEPROM or flash, Developers also need to know how to burn such chips ), Risk burning the development version ,( Those who recommended this kind of tutorial didn't mean anything );
And use special driver chip and memory card to play audio , More significance lies in demonstration , Regardless of price or resource occupation , Or the requirements for developers' knowledge reserves , Are not optimal solutions ;
Use MP3 modular , The price is neither high nor low , The requirement for knowledge reserve is extremely low .( As long as you can pull up and down the MCU IO Level can be used , This is in the introductory tutorial blink I have already taught you )
5、 ... and 、 Generalization
Buzzer is suitable for playing prompt sound , Lowest price , The worst sound quality , Operation trouble
MP3 The module is suitable for playing long and short audio , The price is moderate , Medium sound quality , It's easy to operate
The audio decoder is suitable for playing high-quality or multi-channel audio , Expensive , The best sound quality , The operation is complicated
边栏推荐
- Minio文件下载问题——inputstream:closed
- imgcat使用心得
- C language, log print file name, function name, line number, date and time
- Pat 1097 duplication on a linked list (25 points)
- JS正则表达式基础知识学习
- Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
- Learning notes of JS variable scope and function
- Imgcat usage experience
- 数据库课程设计:高校教务管理系统(含代码)
- Mysql database interview questions
猜你喜欢
JS function promotion and declaration promotion of VaR variable
Esp8266 connect onenet (old mqtt mode)
Basic operations of databases and tables ----- modifying data tables
Classification, understanding and application of common methods of JS array
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
js 变量作用域和函数的学习笔记
JS变量类型以及常用类型转换
ES6语法总结--下篇(进阶篇 ES6~ES11)
Mysql database interview questions
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
随机推荐
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
嵌入式启动流程
(一)R语言入门指南——数据分析的第一步
Detailed explanation of truncate usage
open-mmlab labelImg mmdetection
ARM PC=PC+8 最便于理解的阐述
Time slice polling scheduling of RT thread threads
Dead loop in FreeRTOS task function
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Esp8266 connect onenet (old mqtt mode)
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Redis cache update strategy, cache penetration, avalanche, breakdown problems
JS變量類型以及常用類型轉換
基于Redis的分布式锁 以及 超详细的改进思路
The first simple case of GNN: Cora classification
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
1081 rational sum (20 points) points add up to total points
There are three iPhone se 2022 models in the Eurasian Economic Commission database
ES6语法总结--下篇(进阶篇 ES6~ES11)