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
- Arduino Uno microcontroller.
- A speaker (we are not going to use any audio amplifier to simplify our circuits, but please feel free to use one).
Hardware Instruction
- Connect the +pin of the speaker to Arduino Uno pin 9.
- Connect the other pin of the speaker to Arduino GND pin.
- Connect the Arduino Uno board to computer.
- Select “Arduino Uno” under Tools > Board.
- Make sure that the correct serial port is selected under Tools > Port.
- Upload the sketch using the source code provided.
- 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
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!