The arduino is surely the little micro controller that could. you can do so much with it! We at instructables have been having so much fun with our arduinos, we wanted to show the instructables community how to do some of the basics!

This first tutorial will go over how to make your arduino make simple sounds, and turn your arduino into mini tone generator.

Step 1: Supplies

For this tutorial you will need the following

1 small 8 ohm speaker
1 arduino board
1 push button
1 10 k resistor
some solid core wire

For my purposes, i used the adafruit protoshield to help me lay it out my stuff!

Step 2: Playing a Melody

First to get you started, you should run the default melody program so you you know what to expect from the arduino.

You can get the melody example from the arduino IDE. But just in case here is the code:


Plays a melody

* 8-ohm speaker on digital pin 8

created 21 Jan 2010
modified 14 Oct 2010
by Tom Igoe

This example code is in the public domain.


#include "pitches.h"

// notes in the melody:
int melody[] = {

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };

void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
// stop the tone playing:

void loop() {
// no need to repeat the melody.

You'll also need a file that defines your tones, that should also be in the IDE but here it is. Just copy this text into a .h file in the same directory as your code:
* Public Constants

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

Just upload it and hook up your speaker like you see here in the picture, positive to pin 8, negative to ground, you you'll hear a little tune.

For more on the tone tutorial check out this: http://arduino.cc/en/Tutorial/Tone

Step 3: The Tone Function

Playing music in the arduino introduces a function known as tone(). This function controls your ability to play music. In the code it will look something like this:

tone(pin, frequency, duration)
tone(pin, frequency)

Where pin is the pin the speaker is hooked up to, frequency is the tone in hertz and duration is how long, in milliseconds.

If you use the 2nd convention, to stop the sound you must have a noTone() function.


In the previous step for the melody, you had to include the pitches.h file. This file just gives you a variable for every tuned note in hertz so you don't have to work out basic notes. This makes writing songs much easier!

Step 4: The Simple Keyboard

We will continue this tutorial by making a simple one key keyboard. This project basically combines the elements that we know from making tones and puts them with the ability to push a button to make sound.

Take a look at both the pictures and the fritzing example for the wiring.

Here is the code i whipped up. I basically took the button tutorial and added in the tone function, Don't forget your pitches.h (unless you want to calculate your tone by hertz):

/* Simple Tone Keyboard
Gregg Horton 2011
#include "pitches.h"
const int buttonPin = 2; // the number of the pushbutton pin
int note1 = NOTE_C4; // define note sound

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// sound tone
tone(8, note1);
else {
//turn off sound

one the code is loaded and the circuit is put together. you should be able to push the button and hear a C note. Take a look at this video for the example:

Step 5: Jam Out!

Now its time to jam out! If you want to add more notes, just add more buttons and more code. If you build your own arduino full keyboard post it in the comments.

Stay tuned for more Arduino Basics!

its no working on arduino uno .why?<br>
<p>Hello Instructables forum,</p><p><br>I need to trigger a VMA02 shield which would reproduce a sound, with a PIR sensor movement of Arduino.<br><br>With the provides codes,<br>I managed to activate both separately,<br>I mean, the PIR sensor activates a led,<br>and,<br>the VMA02 shield activates the recorded sounds through the bottoms.<br><br>//VMA02 code//<br>#include &lt;ISD1700.h&gt;<br>ISD1700 chip(10); // Initialize chipcorder with<br>// SS at Arduino's digital pin 10<br><br>int apc=0;<br>int vol=0; //volume 0=MAX, 7=min<br>int startAddr=0x10;<br>int endAddr =0x2DF;<br>int inInt=0;<br>void setup()<br>{<br>apc = apc | vol; //D0, D1, D2<br><br>//apc = apc | 0x8; //D3 comment to disable output monitor during record<br><br>apc = apc | 0x50; // D4&amp; D6 select MIC REC<br>//apc = apc | 0x00; // D4&amp; D6 select AnaIn REC<br>//apc = apc | 0x10; // D4&amp; D6 select MIC + AnaIn REC<br><br>apc = apc | 0x80; // D7 AUX ON, comment enable AUD<br><br>apc = apc | 0x100; // D8 SPK OFF, comment enable SPK<br><br>//apc = apc | 0x200; // D9 Analog OUT OFF, comment enable Analog OUT<br><br>//apc = apc | 0x400; // D10 vAlert OFF, comment enable vAlert<br><br>apc = apc | 0x800; // D11 EOM ON, comment disable EOM<br><br><br><br>Serial.begin(9600);<br>Serial.println(&quot;Sketch is starting up&quot;);<br>}<br>void loop()<br>{<br>char c;<br><br>if(Serial.available())<br>{<br>/* Power Up */<br>chip.pu();<br>c = Serial.read();<br>switch(c)<br>{<br>case 'A':<br>Serial.println(chip.rd_apc(), BIN);<br>break; <br>case 'Y':<br>chip.play();<br>break;<br>case 'P':<br>chip.stop();<br>delay(500);<br>break;<br>case 'E':<br>chip.erase();<br>delay(500);<br>break;<br>case 'R':<br>chip.rec();<br>break;<br>case 'F':<br>chip.fwd();<br>delay(500);<br>break;<br>case 'Z':<br>chip.g_erase();<br>delay(500);<br>break;<br>case 'I':<br>Serial.println(chip.devid(), BIN); <br>break;<br>case 'W': <br>Serial.println(apc, BIN);<br>chip.wr_apc2(apc); //<br>break;<br>case 'S': <br>Serial.println(chip.rd_status(), BIN);<br>break;<br>case '&gt;': <br>startAddr=SerialIn(); <br>Serial.print(&quot;startAddr: &quot;);<br>Serial.println(startAddr);<br>break; <br>case '&lt;': <br>endAddr=SerialIn(); <br>Serial.print(&quot;endAddr: &quot;);<br>Serial.println(endAddr);<br>break; <br>case 'y':<br>chip.set_play(startAddr,endAddr);<br>break;<br>case 'r':<br>//chip.set_erase(startAddr,endAddr);<br>//delay(500);<br>chip.set_rec(startAddr,endAddr);<br>break;<br>case 'e':<br>chip.set_erase(startAddr,endAddr);<br>delay(500);<br>break;<br><br>}<br>Serial.print(&quot;Status---&gt; &quot;);<br>Serial.print(chip.CMD_ERR()? &quot;CMD_ERR &quot;: &quot;OK &quot;);<br>Serial.print(chip.PU()? &quot;PU &quot;: &quot;NO PU &quot;);<br>Serial.print(chip.RDY()? &quot;RDY &quot;: &quot;Not_RDY &quot;);<br>Serial.print(chip.rd_status(), BIN);<br>Serial.println();<br>delay(1000);<br>}<br>}<br>int SerialIn(){<br>inInt=0;<br><br>while (Serial.available() &lt;= 0) <br>{<br>delay(300);<br>}<br>while (Serial.available()) {<br>// get the new byte:<br>char c = Serial.read(); <br>// add it to the inputString:<br>inInt = (inInt*10) + (c-48);<br>// if the incoming character is a newline, set a flag<br>// so the main loop can do something about it:<br>if (c == '\n') {<br>//stringComplete = true;<br>Serial.print(&quot;stringComplete &quot;);<br>} <br>}<br>//c = Serial.read()-48; <br>//mess(c);<br>return (inInt);<br><br>}<br>/*<br>void mess(int num){<br>Serial.print(&quot;num: &quot;);<br>Serial.println(num);<br>startAddr=(0x50*num)+0x10;<br>endAddr=(startAddr+0x50)-1;<br>Serial.print(&quot;startAddr: &quot;);<br>Serial.print(startAddr, HEX);<br>Serial.print(&quot; - endAddr: &quot;);<br>Serial.println(endAddr, HEX);<br><br>}<br>*/ <br><br><br><br>//PIR code//<br><br>/* <br>* //////////////////////////////////////////////////<br>* //making sense of the Parallax PIR sensor's output<br>* //////////////////////////////////////////////////<br>*<br>* Switches a LED according to the state of the sensors output pin.<br>* Determines the beginning and end of continuous motion sequences.<br>*<br>* @author: Kristian Gohlke / krigoo (_) gmail (_) com / <a href="http://krx.at/" rel="nofollow">http://krx.at</a><br>* @date: 3. September 2006 <br>*<br>* kr1 (cleft) 2006 <br>* released under a creative commons &quot;Attribution-NonCommercial-ShareAlike 2.0&quot; license<br>* <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/de/" rel="nofollow">http://creativecommons.org/licenses/by-nc-sa/2.0/de/</a><br>*<br>*<br>* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module. <br>* (<a href="http://www.parallax.com/detail.asp?product_id=555-28027" rel="nofollow">http://www.parallax.com/detail.asp?product_id=555-28027</a>)<br>*<br>* The sensor's output pin goes to HIGH if motion is present.<br>* However, even if motion is present it goes to LOW from time to time, <br>* which might give the impression no motion is present. <br>* This program deals with this issue by ignoring LOW-phases shorter than a given time, <br>* assuming continuous motion is present during these phases.<br>* <br>*/<br><br>/////////////////////////////<br>//VARS<br>//the time we give the sensor to calibrate (10-60 secs according to the datasheet)<br>int calibrationTime = 30; <br><br>//the time when the sensor outputs a low impulse<br>long unsigned int lowIn; <br><br>//the amount of milliseconds the sensor has to be low <br>//before we assume all motion has stopped<br>long unsigned int pause = 5000; <br><br>boolean lockLow = true;<br>boolean takeLowTime; <br><br>int pirPin = 3; //the digital pin connected to the PIR sensor's output<br>int ledPin = 13;<br><br><br>/////////////////////////////<br>//SETUP<br>void setup(){<br>Serial.begin(9600);<br>pinMode(pirPin, INPUT);<br>pinMode(ledPin, OUTPUT);<br>digitalWrite(pirPin, LOW);<br><br>//give the sensor some time to calibrate<br>Serial.print(&quot;calibrating sensor &quot;);<br>for(int i = 0; i &lt; calibrationTime; i++){<br>Serial.print(&quot;.&quot;);<br>delay(1000);<br>}<br>Serial.println(&quot; done&quot;);<br>Serial.println(&quot;SENSOR ACTIVE&quot;);<br>delay(50);<br>}<br><br>////////////////////////////<br>//LOOP<br>void loop(){<br><br>if(digitalRead(pirPin) == HIGH){<br>digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state<br>if(lockLow){ <br>//makes sure we wait for a transition to LOW before any further output is made:<br>lockLow = false; <br>Serial.println(&quot;---&quot;);<br>Serial.print(&quot;motion detected at &quot;);<br>Serial.print(millis()/1000);<br>Serial.println(&quot; sec&quot;); <br>delay(50);<br>} <br>takeLowTime = true;<br>}<br><br>if(digitalRead(pirPin) == LOW){ <br>digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state<br><br>if(takeLowTime){<br>lowIn = millis(); //save the time of the transition from high to LOW<br>takeLowTime = false; //make sure this is only done at the start of a LOW phase<br>}<br>//if the sensor is low for more than the given pause, <br>//we assume that no more motion is going to happen<br>if(!lockLow &amp;&amp; millis() - lowIn &gt; pause){ <br>//makes sure this block of code is only executed again after <br>//a new motion sequence has been detected<br>lockLow = true; <br>Serial.print(&quot;motion ended at &quot;); //output<br>Serial.print((millis() - pause)/1000);<br>Serial.println(&quot; sec&quot;);<br>delay(50);<br>}<br>}<br>}<br><br><br><br><br>I need to unify both mechanisms,<br>Activate a sound triggered by the PIR sensor.<br><br>If someone can help me,<br>I would appreciate it.<br><br>If you help me to solve it,<br>I will post here a link of our website,<br>in where you will find information such as videos, to explain a technological art exhibition we are developing.<br><br>We unveil the exhibition on 20th of January.<br><br>Kind regards.</p>
Hello!!!!!<br>I want a little help. I have been given a project. I have to implement arduino in my project in such a way that speakers can play sounds of different alphabets ,fruits, vegetables etc. Please tell me how to code it in arduino
<p>Would it be better if we use the piano key frequency formula -&gt; f(n) = 2 ^ (n - 49/12) * 440 Hz, instead of defining all tones?</p>
If you put a capacitor in series with the speaker it will be much louder.
<p>what type of capacitor?</p><p>how to set up it?</p><p>thanks..</p>
<p>OK - piece of cake and very very cool ... but one thing TOTALLY escapes me... How do you 'jam out' so 'radically'!?!? The hood, the matching glasses... the rockin mouth poses! Please add instructable on this as well please, otherwise this tutorial is just not complete. :)</p>
Where did you get the 8 ohm speaker? Thanks.
consider me tuned !
Can two tones be played at the same time, and what does the resistor do? Would like to be using square waves somehow.
Yes they can,kinda choppy,and I still don't know bout the resistor, this is what iv made with the tone function, will write it up soon, ordered some relay shields the other day ,but now iv used the board, doh.
having a problem with the Pitches.h file. i can get it to work when i use the absolute path, but i can't get the relative path to work. i put the Pitches.h in the same folder as the .ino and i'm using a windows machine. anyone know why the relative path isn't working?
Well cool !&nbsp;<br> I had to assign a path in the h include. But's that prob just because I'm a noob.<br> <br> <br> #include &quot;./Pitches.h&quot;<br> <br> // notes in the melody:<br> int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4 };<br> <br> // note durations: 4 = quarter note, 8 = eighth note, etc.:<br> int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };<br> <br> void setup() {<br> &nbsp; int speakerPin = 8;<br> &nbsp;<br> &nbsp; // iterate over the notes of the melody:<br> &nbsp; for ( int thisNote = 0; thisNote &lt; 8; thisNote++) {<br> <br> &nbsp;&nbsp;&nbsp; // to calculate the note duration, take one second<br> &nbsp;&nbsp;&nbsp; // divided by the note type.<br> &nbsp;&nbsp;&nbsp; //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.<br> &nbsp;&nbsp;&nbsp; int noteDuration = 700/noteDurations[thisNote];<br> &nbsp;&nbsp;&nbsp; tone( speakerPin, melody[thisNote], noteDuration );<br> <br> &nbsp;&nbsp;&nbsp; // to distinguish the notes, set a minimum time between them.<br> &nbsp;&nbsp;&nbsp; // the note's duration + 30% seems to work well:<br> &nbsp;&nbsp;&nbsp; int pauseBetweenNotes = noteDuration * 1.30;<br> &nbsp;&nbsp;&nbsp; delay( pauseBetweenNotes );<br> &nbsp;&nbsp;&nbsp; // stop the tone playing:<br> &nbsp;&nbsp;&nbsp; noTone( speakerPin );<br> &nbsp; }<br> }<br> <br> void loop() {<br> // no need to repeat the melody.<br> }
I saved the pitches defines as a .h file in the folder where the sound.ino went. <br>And included the .h file with a relative path. <br> <br>#include &quot;./Pitches.h&quot; <br> <br>That was the only way i could get it to work. <br>Thanks. <br> <br>
Would it be possible to make a sound recording and then have the speaker play the sound recording? I want my garduino to talk when he is hungry hehe.. I have a little 8ohm like the one used in this tutorial. Very cool tutorial.
I don't have any speakers, although i do have some piezo buzzers. Will one of these work instead of the speaker?<br><br>-TheWaddleWaaddle
I got my speaker from an old tv at goodwill. The TV cost 99cents and I got 2 speakers!
Yes, they will
We need some videos about this project !!!!
Yeah, we wanna hear!
Can this Arduino make sounds ala chiptunes using a fairly common SN76489? Thanks in advance
What waveform is the tone generated by the arduino?
sweet pics, dude. are those 3-D glasses?
no they r wayfarer(spicoli) shades. u can buy em in a mall at like zumiez. they r like $10
No they are 4D
Cool I'ble! I get the feeling that even I can do this. Thanks!
These pictures are too awesome!!
Thanks, this helped me out a lot!

About This Instructable




Bio: I'm a full stack web developer focusing on security and privacy.
More by frenzy:Cómo hacer una placa iluminada con teclas de un teclado Una funda para tu dispositivo, hecha de un traje de buceo Dead Drop inalámbrico 
Add instructable to: