当前位置:网站首页>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
边栏推荐
- Classification, understanding and application of common methods of JS array
- Arm pc=pc+8 is the most understandable explanation
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
- ESP8266使用arduino连接阿里云物联网
- Bubble sort [C language]
- (一)R语言入门指南——数据分析的第一步
- RT thread API reference manual
- imgcat使用心得
- Characteristics, task status and startup of UCOS III
猜你喜欢
随机推荐
NRF24L01故障排查
Selective sorting and bubble sorting [C language]
I2C bus timing explanation
GCC compilation options
ESP8266连接onenet(旧版MQTT方式)
Knowledge summary of request
2022.2.12 resumption
js 变量作用域和函数的学习笔记
Priority inversion and deadlock
高通&MTK&麒麟 手機平臺USB3.0方案對比
【ESP32学习-1】Arduino ESP32开发环境搭建
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
@Autowired 和 @Resource 的区别
1081 rational sum (20 points) points add up to total points
Whistle+switchyomega configure web proxy
Analysis of charging architecture of glory magic 3pro
Générateur d'identification distribué basé sur redis
History object
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Redis based distributed ID generator