E780d78f c5a5 4c95 a218 09e53905da52


Play a song with the Arduino Tone Sequencer #NationOfMakers

by pjd

Published on June 16, 2016

Making noise with the tone() function is the "Hello World" of Arduino sound.

This project goes a little bit further and plays a song. The song is stored as a sequence of notes where a note is a pitch (frequency) and duration (time in milliseconds). The two include files that go along with the main sketch define symbolic note names (e.g., NOTE_C3 is middle C) and note durations (e.g., QUARTER is a quarter note). The song -- a sequence of notes -- is stored in a two dimensional array.

Synth people and other musicians call this kind of application a "sequencer" because it plays the sequence of notes as an endless loop. In fact, if you have the Synth Kit, you might want to send the audio signal to an envelope module, filter, etc. I'm expanding the basic sketch and using it for experiments in synthesis.

This is a good beginning project because the hardware is simple and the code is short. I'll cover the most basic information here, but please see http://sandsoftwaresound.net/littlebits-arduino-tone-sequencer/ for all the grungy details. Thanks.


Duration: A few hours

How To Make It

STEP 1 : Build the hardware

Assemble the hardware components as shown in the picture. The fork, two dimmer and button modules to the left of the Arduino are optional. The sketch does not use them. They are part of the Arduino Coding Kit and I decided to throw them in. The power module is required. You don't absolutely need the dimmer module between pin D5 and the Synth Speaker. I just find it easier to set the volume using the dimmer instead of the little trim pot on the Synth Speaker module. (Big clumsy hands of mine!) It's OK to leave out the volume dimmer.

STEP 2 : Configure the hardware

The sketch sends the audio through Arduino pin D5. Set the PWM/Analog to PWM if you want loud and nasty. Set the PWM/Analog switch to Analog if you want a softer, quieter tone. I suggest starting with a low volume espeically if you set the PWM/Analog switch to PWM. It will be very loud and it will be nasty. (Pure square waves are kind of nasty.)

STEP 3 : Download the code (copy and paste)

There are three code files: ToneTest.ino, ToneFreq.h and ToneNote.h. The main sketch is ToneTest.ino. (The INO file extension tells the Arduino IDE that this is a sketch.) The sketch includes two other source files. The note names (pitches) are defined in ToneFreq.h and the note durations are defined in ToneNote.h. All three files must be in the same IDE project directory named "ToneTest".

STEP 4 : Compile the code

The littleBits site has some terrific getting started articles about downloading the Arduino IDE, compiling code, and so forth. Here is a link http://discuss.littlebits.cc/t/getting-started-with-arduino/109 to a short article on getting started with Arduino. Open the ToneTest.ino sketch in the Arduino IDE and click the verify (compile) button. Please don't forget that the main sketch needs the include files.

STEP 5 : Upload the sketch to the Arduino

Click the IDE's upload button. The IDE should send the compiled sketch to the Arduino. If you run into issues, then check the article about troubleshooting your code: http://discuss.littlebits.cc/t/introduction-to-arduino-programming-5-troubleshooting-your-code/22284

STEP 6 : Turn on the power and turn up the music

Turn on the power. Wait a few moments while the sketch initializes. (The runtime system needs to load up the sequence array.) Slowly turn up the volume. You should hear the song. Jazz fans should recognize the sequence that I coded. One small warning: The Arduino tone() function has trouble playing low notes. Notes below B3 (NOTE_B3, 123 Hz) are glitchy and may not be heard.

STEP 7 : Try a song of your own

Change the notes in the sequence array. You will also need to change the number of notes in the array (i.e., the array size) and you should also change the index of the first note to be played (START).

STEP 8 : Take a look at the code and study its design

This is a good way to learn about code and coding. The setup() function initializes the program variables and starts the first note playing. The loop() function checks to see if the note has finished and chooses the next note to be played. The loop starts at the beginning of the sequence array when it reaches the end of the array. The delay() at the top of the loop is a one millisecond time delay. The loop counts down from the expected duration and chooses a new note to be played when the duration expires (i.e., the count reaches zero). Hey, hey, always have fun!

Related Projects


Jumping off the idea of animatronics, students in the Kennedy-Longfellow Afterschool Coding club created littleBits "Animal-tronic...


V8 with a 90-degree V angle.

Bathtub Level Monitoring

SMT Waterbit - Where Water and Electronics Mix. http://youtu.be/d8HV8KpQky4Vote for the WaterBit so you can do the similar project...