Arduino Tone Music: How to Play Musical Notes with Arduino

Published By

When we think of user interfaces for electronics, our thoughts generally go to a touchscreen, or perhaps an LED or two in very simple cases. Sometimes, however, audible feedback is best, whether as a tone that can be heard no matter where you’re facing, an alarm to get you up in the morning, or a simple robot greeting that beeps to say “hello.” Additionally, this signaling method can be used to accommodate those with visual impairments.

The good news is that from a hardware standpoint, producing a simple output with a piezo buzzer speaker (seen below) is as easy as hooking up an LED. Code is available in the Arduino IDE under Examples/Digital, where you’ll find four example sketches starting with “tone.” These programs call for the use of an 8-ohm speaker hooked up to a digital output pin, but if you can find that spec on a small piezo speaker, you can simply connect it to see if it works.

0418_arduino_music_4

 

Adding Arduino Tone Code

0418_arduino_music_2

 

For your first trial, load the simplest example program,toneMelody”, and hook up the speaker to ground and pin 8. If things are configured correctly, it will play a short song that you’ll recognize, then (conveniently) stop until you hit the reset button. The notes and duration of this program can be modified or integrated as an output method into your program, and this makes a good starting point for audio experimentation.

If you have several speakers available, you can play notes over several outputs using the toneMultipleexample, one speaker at a time. This would be useful for a machine or robot that needs to get a user’s attention in different places.

Arduino Music User Interface

Arduino Music User Interface

 

Making things more interesting, you can add a user interface in the form of an analog input. The “tonePitchFollower” example sets things up to do this easily, and is written to use a photoresistor as the input, with a 4.7 kilohm resistor functioning as a voltage divider. This same code can be used with a potentiometer, though you’ll want to change the analog input range from 400-1000 to 0-1024 to reflect the greater range of this component. When hooked up this way, you can adjust the frequency higher and lower by twisting, reminiscent of tuning a radio.

This, tone, however, will soon get annoying. If you’d like to add a cutoff when the potentiometer is turned nearly to zero, you can add a conditional statement to make things a little more pleasant.

Adding the Arduino Wave Shield

If you’d like to signal someone with the words stop” orpush the green buttonrather than simple beeps and bloops, then you might consider adding a Wave Shield. This device plugs into an Arduino Uno board from an SD card, and features a potentiometer so you can turn the volume up or down, as needed. You might also consider a voice recording module like the ISD1820 or similar that can record a short voice clip and play it back when signaled by a microcontroller or physical button. Finally, other dev boards can work with piezo speakers in a similar manner, so you’re not just restricted to Arduino, but can use an audible signal in a wide variety of situations. 

For more popular Arduino Products click here.

 

See related product

Wave Shield | 94

Adafruit Industries Development Kits and Tools View

 

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here