Playing Melodies Using Arduino

Introduction

Today we are going to tinker something easy and simple but interesting. We will be exploring something musical by taking advantage of microcontrollers capability to produce voltage signals. Our goal is to produce a melody using and Arduino Uno and a speaker.

The theory behind this is by generating a square wave signal and controlling its duration, we are essentially varying the voltage supplied to the speaker. With this we are going to use the Arduino Tone function.

As per Arduino Tone function documentation, a single pin can produce a frequency around
31Hz to 65,535 kHz. According to our scientist humans can hear frequency from 20 Hz to 20,000 Hz but we hear best on frequency from 1,000 Hz to 5,000 Hz. This is human speaking frequency.

As a caveat, I am not a musician or a musically inclined person so please pardon my limitations with regards to music especially with this tinkering. 🙂

Bill Of Materials

  1. Arduino Uno microcontroller.
  2. A speaker (we are not going to use any audio amplifier to simplify our circuits, but please feel free to use one).

Hardware Instruction

  1. Connect the +pin of the speaker to Arduino Uno pin 9.
  2. Connect the other pin of the speaker to Arduino GND pin.
  3. Connect the Arduino Uno board to computer.
  4. Select “Arduino Uno” under Tools > Board.
  5. Make sure that the correct serial port is selected under Tools > Port.
  6. Upload the sketch using the source code provided.
  7. Enjoy.

Schematic Diagram

Video Demonstration

Source Code

 1
 2/*
 3  Plays a melody
 4
 5  created 21 Jan 2010
 6  modified 30 Aug 2011
 7  by Tom Igoe
 8
 9  This example code is in the public domain.
10  http://www.arduino.cc/en/Tutorial/Tone
11*/
12
13#include "pitches.h"
14
15int melody[] = {
16  NOTE_E7, NOTE_E7, 0, NOTE_E7,
17  0, NOTE_C7, NOTE_E7, 0,
18  NOTE_G7, 0, 0,  0,
19  NOTE_G6, 0, 0, 0,
20
21  NOTE_C7, 0, 0, NOTE_G6,
22  0, 0, NOTE_E6, 0,
23  0, NOTE_A6, 0, NOTE_B6,
24  0, NOTE_AS6, NOTE_A6, 0,
25
26  NOTE_G6, NOTE_E7, NOTE_G7,
27  NOTE_A7, 0, NOTE_F7, NOTE_G7,
28  0, NOTE_E7, 0, NOTE_C7,
29  NOTE_D7, NOTE_B6, 0, 0,
30
31  NOTE_C7, 0, 0, NOTE_G6,
32  0, 0, NOTE_E6, 0,
33  0, NOTE_A6, 0, NOTE_B6,
34  0, NOTE_AS6, NOTE_A6, 0,
35
36  NOTE_G6, NOTE_E7, NOTE_G7,
37  NOTE_A7, 0, NOTE_F7, NOTE_G7,
38  0, NOTE_E7, 0, NOTE_C7,
39  NOTE_D7, NOTE_B6, 0, 0
40};
41
42//Mario main them tempo
43int noteDurations[] = {
44  12, 12, 12, 12,
45  12, 12, 12, 12,
46  12, 12, 12, 12,
47  12, 12, 12, 12,
48
49  12, 12, 12, 12,
50  12, 12, 12, 12,
51  12, 12, 12, 12,
52  12, 12, 12, 12,
53
54  9, 9, 9,
55  12, 12, 12, 12,
56  12, 12, 12, 12,
57  12, 12, 12, 12,
58
59  12, 12, 12, 12,
60  12, 12, 12, 12,
61  12, 12, 12, 12,
62  12, 12, 12, 12,
63
64  9, 9, 9,
65  12, 12, 12, 12,
66  12, 12, 12, 12,
67  12, 12, 12, 12,
68};
69
70
71
72void setup() {
73  // iterate over the notes of the melody:
74  for (int thisNote = 0; thisNote < 25; thisNote++) {
75
76    // to calculate the note duration, take one second divided by the note type.
77    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
78    int noteDuration = 1000 / noteDurations[thisNote];
79    tone(9, melody[thisNote], noteDuration);
80
81    // to distinguish the notes, set a minimum time between them.
82    // the note's duration + 30% seems to work well:
83    int pauseBetweenNotes = noteDuration * 1.30;
84 
85    delay(pauseBetweenNotes);
86    // stop the tone playing:
87    noTone(9);
88  }
89}
90
91void loop() {
92  // no need to repeat the melody.
93
94}

Call To Action

Please feel free to modify the source code to your liking. You may try to imitate your favorite melodies by changing the melody array and its respective noteDuration arrays.

If you find this lesson useful, please support my channel by Subscribing. Please click like and leave your comments and suggestions in the comment box.

Thank you and have a good day. Bye



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.