Arduino Basics: Making Sound




About: I'm a full stack web developer focusing on security and privacy.

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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:

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!

Microcontroller Contest

Participated in the
Microcontroller Contest

1 Person Made This Project!


  • Instrument Contest

    Instrument Contest
  • Make it Glow Contest

    Make it Glow Contest
  • STEM Contest

    STEM Contest

25 Discussions


7 months ago

How did you calculated the resistance required ?


2 years ago

its no working on arduino uno .why?


3 years ago

Hello Instructables forum,

I need to trigger a VMA02 shield which would reproduce a sound, with a PIR sensor movement of Arduino.

With the provides codes,
I managed to activate both separately,
I mean, the PIR sensor activates a led,
the VMA02 shield activates the recorded sounds through the bottoms.

//VMA02 code//
#include <ISD1700.h>
ISD1700 chip(10); // Initialize chipcorder with
// SS at Arduino's digital pin 10

int apc=0;
int vol=0; //volume 0=MAX, 7=min
int startAddr=0x10;
int endAddr =0x2DF;
int inInt=0;
void setup()
apc = apc | vol; //D0, D1, D2

//apc = apc | 0x8; //D3 comment to disable output monitor during record

apc = apc | 0x50; // D4& D6 select MIC REC
//apc = apc | 0x00; // D4& D6 select AnaIn REC
//apc = apc | 0x10; // D4& D6 select MIC + AnaIn REC

apc = apc | 0x80; // D7 AUX ON, comment enable AUD

apc = apc | 0x100; // D8 SPK OFF, comment enable SPK

//apc = apc | 0x200; // D9 Analog OUT OFF, comment enable Analog OUT

//apc = apc | 0x400; // D10 vAlert OFF, comment enable vAlert

apc = apc | 0x800; // D11 EOM ON, comment disable EOM

Serial.println("Sketch is starting up");
void loop()
char c;

/* Power Up */
c =;
case 'A':
Serial.println(chip.rd_apc(), BIN);
case 'Y':;
case 'P':
case 'E':
case 'R':
case 'F':
case 'Z':
case 'I':
Serial.println(chip.devid(), BIN);
case 'W':
Serial.println(apc, BIN);
chip.wr_apc2(apc); //
case 'S':
Serial.println(chip.rd_status(), BIN);
case '>':
Serial.print("startAddr: ");
case '<':
Serial.print("endAddr: ");
case 'y':
case 'r':
case 'e':

Serial.print("Status---> ");
Serial.print(chip.CMD_ERR()? "CMD_ERR ": "OK ");
Serial.print(chip.PU()? "PU ": "NO PU ");
Serial.print(chip.RDY()? "RDY ": "Not_RDY ");
Serial.print(chip.rd_status(), BIN);
int SerialIn(){

while (Serial.available() <= 0)
while (Serial.available()) {
// get the new byte:
char c =;
// add it to the inputString:
inInt = (inInt*10) + (c-48);
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (c == '\n') {
//stringComplete = true;
Serial.print("stringComplete ");
//c =;
return (inInt);

void mess(int num){
Serial.print("num: ");
Serial.print("startAddr: ");
Serial.print(startAddr, HEX);
Serial.print(" - endAddr: ");
Serial.println(endAddr, HEX);


//PIR code//

* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
* @author: Kristian Gohlke / krigoo (_) gmail (_) com /
* @date: 3. September 2006
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;

void setup(){
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.println(" done");
Serial.println("SENSOR ACTIVE");

void loop(){

if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.print("motion detected at ");
Serial.println(" sec");
takeLowTime = true;

if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");

I need to unify both mechanisms,
Activate a sound triggered by the PIR sensor.

If someone can help me,
I would appreciate it.

If you help me to solve it,
I will post here a link of our website,
in where you will find information such as videos, to explain a technological art exhibition we are developing.

We unveil the exhibition on 20th of January.

Kind regards.

Ali Hassan Raza 1

3 years ago

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


5 years ago on Step 2

Would it be better if we use the piano key frequency formula -> f(n) = 2 ^ (n - 49/12) * 440 Hz, instead of defining all tones?


Reply 6 years ago on Introduction

Can two tones be played at the same time, and what does the resistor do? Would like to be using square waves somehow.


Reply 6 years ago on Introduction

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.


6 years ago on Introduction

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?


6 years ago on Step 5

Well cool ! 
I had to assign a path in the h include. But's that prob just because I'm a noob.

#include "./Pitches.h"

// notes in the melody:
int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4 };

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

void setup() {
  int speakerPin = 8;
  // 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 = 700/noteDurations[thisNote];
    tone( speakerPin, 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;
    delay( pauseBetweenNotes );
    // stop the tone playing:
    noTone( speakerPin );

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


6 years ago on Step 2

I saved the pitches defines as a .h file in the folder where the sound.ino went.
And included the .h file with a relative path.

#include "./Pitches.h"

That was the only way i could get it to work.


7 years ago on Introduction

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?


2 replies