Adding sound to Arduino is fun, and interfacing speakers with Arduino is relatively straightforward. You can directly attach a speaker to Arduino or connect a speaker through an amplifier circuit. In both cases, Arduino can output digital audio from any GPIO. Whether or not one needs an amplifier circuit depends entirely on the selected speaker. An amplifier circuit is not required if the speaker is powerful enough to generate hearable sounds.
Arduino cannot process audio files independently; it can still generate digital audio for many musical notes. An external audio decoding circuit is required to play audio files from a source like WAV or MP3 files on an SD card. No additional circuit is needed except for a suitable speaker for playing melody tones. In this project, we will play musical notes on Arduino and compose music for a song on Arduino.
Components required
- Arduino UNO x1
- Speaker 4Ω 50W x1
- Connecting wires/jumper wires
Circuit connections
In this project, we are interfacing a speaker directly to Arduino. The speaker used in this project is 4-ohm 50Watt 3-inch speaker that produces enough audible sounds. The speaker has two terminals. Connect one terminal to Arduino’s ground pin and the other terminal to any GPIO. Here, the other terminal is connected to pin D8 of Arduino UNO.
Tone() function
The tone() function is used to play musical tones on Arduino. The function generates a square wave of the specified frequency on a pin with a 50 percent duty cycle. The square wave from a GPIO can be used to play tones on a speaker or buzzer. The function allows playing tones at only one pin at a time. It is not possible to generate tones lower than 31Hz from Arduino. The function is known to interfere with PWM output at pins D3 and D11, so it must be used carefully when using along PWM output. The function has the following prototypes.
tone(pin, frequency)
tone(pin, frequency, duration)
If the duration is not specified, tone plays until a call to noTone() function is made. If the duration is set, the tone is terminated in the specified period in milliseconds.
Playing C Major scale on Arduino
C Major is a major scale based on C. It comprises pitches C, D, E, F, G, A, and B. In Hindustani vocal music, these tones are known as Sa, Re, Ga, Ma, Pa, Da, Ni, and Sa. These pitches have the following frequencies:
C4/Sa: 262
D4/Re: 294
E4/Ga: 330
F4/Ma: 349
G4/Pa: 392
A4/Da: 440
B4/Ni: 494
C5/Sa: 532
Get the circuit connections done and upload the following sketch to Arduino.
The sketch begins with defining constants for C major scale pitches. The pitches are stored in an array. The pitches are generated by calling the tone() function keeping half notes for each pitch. The tone duration is set to half note for all pitches, i.e., 1000/2 = 500 milliseconds. In the setup () function, pin 8 is set as digital output for generating square waves for the pitches. The tones are terminated by calling the noTone() function after all pitches are played. The following video demonstrates playing C major pitches on Arduino.
Playing more notes on Arduino
It is possible to play many different notes for the same pitch by changing the duration of the pitch. All possible notes, like full note, half note, quarter note, eighth note, 6th note, etc., can be played for each pitch by simply changing the duration of the pitch. All pitches were played at half note in the above example for C major scale. Upload the following sketch to Arduino, who plays the same C major scale pitches but at different notes to produce music for the Star Wars movie theme.
The sketch plays the same C major scale pitches but with different notes or tone durations. This way, Arduino played music for the Star Wars movie theme. The following video demonstrates playing pitches with note variation on Arduino.
Playing continuous audio frequencies
Arduino does not just play discreet musical notes. It can play continuous frequencies like that of a police siren. The following sketch generates a continuously varying sound wave rather than generating discreet musical notes.
The sketch continuously changes the output frequency to generate sound for a police siren. The following video demonstrates playing a police siren on Arduino.
Playing musical notes for a song on Arduino
Playing different pitches at different notes can generate music for any song on Arduino. The following sketch generates music for a song on Arduino by playing different pitches at different notes.
The above sketch generates music for a song by playing many different pitches at different notes as per the piano note sheet of the song. Similarly, music for any song can be synthesized on Arduino by playing individual notes. However, it is not possible to play piano chords on Arduino. The piano chords have more than one individual notes played at once, and the tone() function can play only one tone or note at a time. The following video demonstrates playing music for a song composed of single notes.
Conclusion
It is possible to generate a variety of melody tones on Arduino by playing different pitches at different notes. This way, Arduino can synthesize any music provided it comprises single notes.
You may also like:
Filed Under: Arduino Projects, Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.