Simple keyboard using the tone() function

A three-key musical keyboard using force sensors and a piezo speaker.

This example shows how to use the tone() command to generate different pitches depending on which sensor is pressed.

Hardware Required

  • Arduino Board

  • 8 ohm speaker

  • 3 force sensing resistors

  • 3 10k ohm resistors

  • 100 ohm resistor

  • hook-up wires

  • breadboard

Circuit

Connect one terminal of your speaker to digital pin 8 through a 100 ohm resistor, and its other terminal to ground.

Power your three FSRs (or any other analog sensor) with 5V in parallel. Connect each sensor to analog pins 0-2, using a 10K resistor as a reference to groud on each input line.

circuit

Schematic

schematic

Code

The sketch below reads three analog sensors. Each corresponds to a note value in an array of notes. If any of the sensors is above a given threshold, the corresponding note is played.

Here's the main sketch:

The sketch uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful for whenever you want to make musical notes.

To make the pitches.h file, either click on the button just below the serial monitor icon and choose "New Tab", or use Ctrl+Shift+N.

Then paste in the following code:

1/*************************************************
2
3 * Public Constants
4
5 *************************************************/
6
7#define NOTE_B0 31
8#define NOTE_C1 33
9#define NOTE_CS1 35
10#define NOTE_D1 37
11#define NOTE_DS1 39
12#define NOTE_E1 41
13#define NOTE_F1 44
14#define NOTE_FS1 46
15#define NOTE_G1 49
16#define NOTE_GS1 52
17#define NOTE_A1 55
18#define NOTE_AS1 58
19#define NOTE_B1 62
20#define NOTE_C2 65
21#define NOTE_CS2 69
22#define NOTE_D2 73
23#define NOTE_DS2 78
24#define NOTE_E2 82
25#define NOTE_F2 87
26#define NOTE_FS2 93
27#define NOTE_G2 98
28#define NOTE_GS2 104
29#define NOTE_A2 110
30#define NOTE_AS2 117
31#define NOTE_B2 123
32#define NOTE_C3 131
33#define NOTE_CS3 139
34#define NOTE_D3 147
35#define NOTE_DS3 156
36#define NOTE_E3 165
37#define NOTE_F3 175
38#define NOTE_FS3 185
39#define NOTE_G3 196
40#define NOTE_GS3 208
41#define NOTE_A3 220
42#define NOTE_AS3 233
43#define NOTE_B3 247
44#define NOTE_C4 262
45#define NOTE_CS4 277
46#define NOTE_D4 294
47#define NOTE_DS4 311
48#define NOTE_E4 330
49#define NOTE_F4 349
50#define NOTE_FS4 370
51#define NOTE_G4 392
52#define NOTE_GS4 415
53#define NOTE_A4 440
54#define NOTE_AS4 466
55#define NOTE_B4 494
56#define NOTE_C5 523
57#define NOTE_CS5 554
58#define NOTE_D5 587
59#define NOTE_DS5 622
60#define NOTE_E5 659
61#define NOTE_F5 698
62#define NOTE_FS5 740
63#define NOTE_G5 784
64#define NOTE_GS5 831
65#define NOTE_A5 880
66#define NOTE_AS5 932
67#define NOTE_B5 988
68#define NOTE_C6 1047
69#define NOTE_CS6 1109
70#define NOTE_D6 1175
71#define NOTE_DS6 1245
72#define NOTE_E6 1319
73#define NOTE_F6 1397
74#define NOTE_FS6 1480
75#define NOTE_G6 1568
76#define NOTE_GS6 1661
77#define NOTE_A6 1760
78#define NOTE_AS6 1865
79#define NOTE_B6 1976
80#define NOTE_C7 2093
81#define NOTE_CS7 2217
82#define NOTE_D7 2349
83#define NOTE_DS7 2489
84#define NOTE_E7 2637
85#define NOTE_F7 2794
86#define NOTE_FS7 2960
87#define NOTE_G7 3136
88#define NOTE_GS7 3322
89#define NOTE_A7 3520
90#define NOTE_AS7 3729
91#define NOTE_B7 3951
92#define NOTE_C8 4186
93#define NOTE_CS8 4435
94#define NOTE_D8 4699
95#define NOTE_DS8 4978

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/08/11 by SM

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.