Adding sound to Arduino is fun, and interfacing speakers with Arduino is relatively simple. You can connect a speaker directly to the Arduino or connect a speaker through an amplifier circuit. In both cases, the Arduino can output digital audio from any GPIO. Whether or not an amplifier circuit is needed depends entirely on the speaker selected. An amplifier circuit is not necessary if the speaker is powerful enough to generate audible sounds.
Arduino cannot process audio files independently; It can even generate digital audio for many musical notes. An external audio decoding circuit is required to play audio files from a source such as WAV or MP3 files on an SD card. No additional circuitry is required except a speaker suitable for playing melody tones. In this project, we will play musical notes on Arduino and compose music for a song on Arduino.
Required components
- Arduino UNO x1
- Speaker 4Ω 50W x1
- Connecting wires/jumper wires
Circuit Connections
In this project, we are connecting a speaker directly to the Arduino. The speaker used in this project is a 3-inch 4 ohm 50 Watt speaker that produces enough audible sounds. The speaker has two terminals. Connect one terminal to the Arduino ground pin and the other terminal to any GPIO. Here the other terminal is connected to the D8 pin 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 of a GPIO can be used to play tones on a speaker or doorbell. The function allows you to play tones on just one pin at a time. It is not possible to generate tones lower than 31 Hz on Arduino. The function is known to interfere with the PWM output on pins D3 and D11, so it should be used with caution when using in conjunction with the PWM output. The function has the following prototypes.
tone (pin, frequency)
tone (pin, frequency, duration)
If the duration is not specified, the tone will be played until a call to the noTone function is made. If duration is set, the tone will end within the specified period in milliseconds.
Playing C major scale on Arduino
C major is a major scale based on C. It comprises the tones 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 Sá. These tones 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
Make the circuit connections and upload the following sketch to the Arduino.
The outline begins with defining constants for notes in the C major scale. Tones are stored in an array. Notes are generated by calling the tone function keeping half notes for each note. The tone duration is set to minimum for all pitches, i.e. 1000/2 = 500 milliseconds. In the setup function, pin 8 is configured as a digital output to generate square waves for tuning. Tones are finalized by calling the noTone function after all tones have been played. The following video demonstrates playing C major tones on the Arduino.
Playing more notes on Arduino
You can play many different notes for the same tone by changing the tone duration. All possible notes like whole note, half note, quarter note, eighth note, sixth note, etc. can be played for each tone by simply changing the tone duration. All tones were played in half notes in the example above for the C major scale. Send the following sketch to the Arduino, which plays the same tones as the C major scale but in different notes to produce music for the Star Wars movie theme.
The sketch plays the same notes as the C major scale, but with different notes or tone durations. This way, the Arduino played music for the Star Wars movie theme. The following video demonstrates how to play notes with note variation on Arduino.
Playing continuous audio frequencies
Arduino doesn't just play discrete musical notes. It can reproduce continuous frequencies like that of a police siren. The following sketch generates a continuously varying sound wave, rather than generating discrete musical notes.
The sketch continually changes the output frequency to generate sound for a police siren. The following video demonstrates how to play a police siren on Arduino.
Playing musical notes of a song on Arduino
Playing different tones on different notes can generate music for any song on Arduino. The following sketch generates music for a song on the Arduino by playing different tones on different notes.
The sketch above generates music for a song by playing many different tones on different notes, according to the song's piano score. Similarly, the music of any song can be synthesized on Arduino by playing individual notes. However, it is not possible to play piano chords on Arduino. Piano chords have more than one individual note played at the same time, and the tone function can play only one tone or note at a time. The following video demonstrates playing a song composed of single notes.
Conclusion
It is possible to generate a variety of melody tones on Arduino by playing different tones on different notes. This way, Arduino can synthesize any song, as long as it contains single notes.