当前位置:网站首页>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
边栏推荐
- ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
- ES6 grammar summary -- Part 2 (advanced part es6~es11)
- Whistle+switchyomega configure web proxy
- Redis based distributed ID generator
- Classification, understanding and application of common methods of JS array
- 基于Redis的分布式锁 以及 超详细的改进思路
- MySQL time, time zone, auto fill 0
- Several declarations about pointers [C language]
- Pytorch four commonly used optimizer tests
- Priority inversion and deadlock
猜你喜欢
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
STM32 how to locate the code segment that causes hard fault
ESP learning problem record
Analysis of charging architecture of glory magic 3pro
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
ESP8266使用arduino连接阿里云物联网
JS正则表达式基础知识学习
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
【ESP32学习-1】Arduino ESP32开发环境搭建
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
随机推荐
【ESP32学习-1】Arduino ESP32开发环境搭建
MySQL takes up too much memory solution
Who says that PT online schema change does not lock the table, or deadlock
VIM command line notes
关于Gateway中使用@Controller的问题
Kaggle competition two Sigma connect: rental listing inquiries
單片機藍牙無線燒錄
OSPF message details - LSA overview
高通&MTK&麒麟 手機平臺USB3.0方案對比
Reno7 60W super flash charging architecture
ESP8266使用arduino连接阿里云物联网
ESP学习问题记录
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
VSCode基础配置
Arduino gets the length of the array
JS正则表达式基础知识学习
open-mmlab labelImg mmdetection
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Detailed explanation of truncate usage
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503