Greetings, I'm rather new to arduino and I've been making a project using arduino mega 2560 and sounds, like a musical keyboard in arduino. The thing is: passive buzzers apparently can't return multiple sounds at the same time, even though there are more than one. Also, they only return beep sounds, and I would like to have more realistic sounds (like piano, flute, organ, etc). Which components could be used for this purpose? How could I program this? (Sorry for the bad english in advance, I'm brazilian)
Welcome to the Arduino world,
Most Arduino boards cannot do sound. What you might do is generate MIDI with the Arduino and control a midi device (keyboard) with it.
if you use the Tone library, then indeed only one pin can be used at the same time so you can't drive multiple buzzers simultaneously with the simple library.
You could be interested in Julian Ilett's video séries on his "Penny Organ"
now, doing piano, flute, organ etc is more complex. You could use a digital-to-analog converter (DAC) with sample playback from a microSD card (to store high-quality audio samples). For synthesis you would need a really fast arduino
You could also have a look at the Mozzi library for Arduino
here is a sample Waveshaping sound (from their web site)
It is possible using "something" to play mp3 files from an SD card where you can store "realistic" instrument sounds.
I don't know your needs, but just to give an example (I've never used it), this is a Sparkfun shield based on the VS1053B chip containing an SD card reader and an audio jack output:
It's not exactly a "musical keyboard," but it can be used to play individual notes from individual MP3 files, depending on the input.
But keep in mind that it's not polyphonic, as this would require "mixing" the MP3 sounds in real time; you can play one note at a time.
Yeah... I'm not an expert but the 1st problem is that the MEGA doesn't have DAC... No true analog output. To some extent it can be "faked" with PWM but it's not high quality.
If you don't know this analogWrite() on the "basic" Arduino's is PWM which works well for dimming an LED or controlling the speed of a motor but it's not really analog. PWM is actually better for light dimming or motor speed control but it's worse for analog audio.
...Maybe you should do some research about how synthesizers and modern MIDI keyboards are made.
I'm not sure how much processing power you need.
The earliest synthesizers used "waveforms" (sine waves, square waves, triangle waves, and sawtooth waves) along with analog filters and loudness-envelopes to get ADSR. They tended to sound like synthesizers ("computer music") rather than like real instruments. All of that MIGHT be possible with a "basic arduino" if you add a DAC. But doing the filtering digitally might not be possible...
In the 1980's FM synthesis was introduced and it can sound pretty close to real instruments. Microprocessors weren't that powerful in the 1980's so it should be "easy" today. But there may have been a lot of other digital and analog circuitry. (Some quick research tells me the Yamaha DX7 used a custom chip.)
Most modern synthesizers/keyboards use "samples". Basically short recordings of real instruments.
You can also get (or build) a MIDI keyboard/controller that sends MIDI messages to a computer which has the appropriate software and virtual instruments (software) which can generate the sound out of your soundcard. A MIDI controller is a lot easier to make than a full instrument that makes sounds.
Most modern keyboards are both instruments and MIDI controllers. You can play them stand-alone, or capture/record the MIDI messages on a computer (where you can edit or switch virtual instruments, etc., after recording) or you they can receive MIDI messages from the computer or another MIDI controller to play sounds.
Welcome to the forums.
If it's sound you want I'd actually go with a ESP32 it has I2S (a bus dedicated to sound)
Although it is a bit to learn. You can make virtually any sound here though it's not going to be HiFi from the ESP32. Its 'listenable'
You could also hook up a mini DF player (SD card reader and mp3 decoder) to the Arduino and it works great. You can play any sound you like in this manner as well, even record your voice, etc. the sound quality is 'decent' and it has a built in 3w amplifier.
I'd recommend the code here on the forums related to the dfplayer if you decide to use it.
Most of the code you see on YouTube is hot garbage and won't work at all, or if it does work it really stinks. ![]()
Have fun ![]()
I started an ADSR sketch, but only did the envelope shaping, none of the sound.
I've made some general research on the DF Mini (as I wanted to use samples), but apparently it has no polyphonic, although some projects online claim to be polyphonic, is it actually possible to use polyphony in the DF Mini with programming only? Or should I get other components? Appreciate it.
I was having a look already on speakers, I'm yet to find a usable one (for some reason I can't find one in the city I live, but anyways), but apparently they are too powerful to arduino and this could lead to a catastrophe, so I heard about these microamplifiers. I have no idea on how they work or anything about them, but apparently they help in the sound synthesis from the arduino to the speakers. Do you know anything about it? Appreciate it, and thanks for the video recommendation!
Well for the most part there are several different amplifiers, all of which you need to research based on your power supply, speakers you intend to use, and your MCU.
But let's assume you want a 5v amplifier from an I2S interface. (The Arduino doesn't have I2S though natively.)
I have a max98537 for I2S for esp32 and it's surprisingly loud!!
Let's say you want a general purpose amp for any project that's cost friendly sounds good and is easy to hook up:
The PAM8302 is a great class D amp for small speakers if you don't intend on using I2S. The sound is surprisingly clean.
My speakers for projects I got from old junk TVs or alarm clocks. 4 ohm 3 watts is what you want typically. Old LCD TVs like LG have really good mini speakers inside.
Partsexpress.com in Ohio USA sells speakers from everything (audiophiles to budget minded hobbyists). So you can find any speaker you want there.
I looked up mini speaker on a search there and Dayton Audio (a good brand) sells one for 0.75 USD
The DF mini is a board with a built in amplifier and plays back music in different file formats.
My thoughts were you could record your music then play it back on the DF. Although your goal here seems different.
As far as polyphonic goes that's over my head. I couldn't reliably make recommendations there.
But I have to say I am addicted to music with my Fiio HiRes DAP and AudioTechnica headphones so I do know when an amp sounds good and clear. ![]()