EsploraMusic

Make some music with the Esplora.

Music

This example shows how to play notes on the buzzer mounted on the Arduino Esplora. Buzzers can generate different audio signals in audible frequency ranges. A note is an audio signal at a specific frequency. If you know the frequency of the notes you want to play, you can start to compose a melody.

In this example, you move the slider to generate different notes, and play them by pressing the down push-button.

Hardware Required

  • Arduino Esplora

Circuit

Only your Arduino Esplora is needed for this example. Connect the Esplora to your computer with a USB cable.

slider to choose the note and push-button switch to play it
slider to choose the note and push-button switch to play it

Code

There are 13 notes stored inside an array. You can choose the note to play by changing the position of the slider.

The index number that selects the value in the array is mapped to the position of the linear potentiometer. This number is the frequency of the note to play. Imagine the length of the potentiometer in split into 13 sections. Moving the slider from the right to the left will change the note when it crosses into a different section. Each section corresponds to a spot in the array, and a value to play on the buzzer.

To play the note you press the down direction button; it will play as long as the button is held.

1/*
2
3 Esplora Music
4
5 This sketch turns the Esplora in a simple musical instrument.
6
7 Press the Switch 1 and move the slider to see how it works.
8
9 Created on 22 november 2012
10
11 By Enrico Gueli <enrico.gueli@gmail.com>
12
13 modified 22 Dec 2012
14
15 by Tom Igoe
16
17*/
18
19#include <Esplora.h>
20
21// these are the frequencies for the notes from middle C
22// to one octave above middle C:
23
24const int note[] = {
25
26 262, // C
27
28 277, // C#
29
30 294, // D
31
32 311, // D#
33
34 330, // E
35
36 349, // F
37
38 370, // F#
39
40 392, // G
41
42 415, // G#
43
44 440, // A
45
46 466, // A#
47
48 494, // B
49
50 523 // C next octave
51};
52
53void setup() {
54}
55
56void loop() {
57
58 // read the button labeled SWITCH_DOWN. If it's low,
59
60 // then play a note:
61
62 if (Esplora.readButton(SWITCH_DOWN) == LOW) {
63
64 int slider = Esplora.readSlider();
65
66 // use map() to map the slider's range to the
67
68 // range of notes you have:
69
70 byte thisNote = map(slider, 0, 1023, 0, 13);
71
72 // play the note corresponding to the slider's position:
73
74 Esplora.tone(note[thisNote]);
75
76 } else {
77
78 // if the button isn't pressed, turn the note off:
79
80 Esplora.noTone();
81
82 }
83}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.