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.
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 Constants4
5 *************************************************/6
7#define NOTE_B0 318#define NOTE_C1 339#define NOTE_CS1 3510#define NOTE_D1 3711#define NOTE_DS1 3912#define NOTE_E1 4113#define NOTE_F1 4414#define NOTE_FS1 4615#define NOTE_G1 4916#define NOTE_GS1 5217#define NOTE_A1 5518#define NOTE_AS1 5819#define NOTE_B1 6220#define NOTE_C2 6521#define NOTE_CS2 6922#define NOTE_D2 7323#define NOTE_DS2 7824#define NOTE_E2 8225#define NOTE_F2 8726#define NOTE_FS2 9327#define NOTE_G2 9828#define NOTE_GS2 10429#define NOTE_A2 11030#define NOTE_AS2 11731#define NOTE_B2 12332#define NOTE_C3 13133#define NOTE_CS3 13934#define NOTE_D3 14735#define NOTE_DS3 15636#define NOTE_E3 16537#define NOTE_F3 17538#define NOTE_FS3 18539#define NOTE_G3 19640#define NOTE_GS3 20841#define NOTE_A3 22042#define NOTE_AS3 23343#define NOTE_B3 24744#define NOTE_C4 26245#define NOTE_CS4 27746#define NOTE_D4 29447#define NOTE_DS4 31148#define NOTE_E4 33049#define NOTE_F4 34950#define NOTE_FS4 37051#define NOTE_G4 39252#define NOTE_GS4 41553#define NOTE_A4 44054#define NOTE_AS4 46655#define NOTE_B4 49456#define NOTE_C5 52357#define NOTE_CS5 55458#define NOTE_D5 58759#define NOTE_DS5 62260#define NOTE_E5 65961#define NOTE_F5 69862#define NOTE_FS5 74063#define NOTE_G5 78464#define NOTE_GS5 83165#define NOTE_A5 88066#define NOTE_AS5 93267#define NOTE_B5 98868#define NOTE_C6 104769#define NOTE_CS6 110970#define NOTE_D6 117571#define NOTE_DS6 124572#define NOTE_E6 131973#define NOTE_F6 139774#define NOTE_FS6 148075#define NOTE_G6 156876#define NOTE_GS6 166177#define NOTE_A6 176078#define NOTE_AS6 186579#define NOTE_B6 197680#define NOTE_C7 209381#define NOTE_CS7 221782#define NOTE_D7 234983#define NOTE_DS7 248984#define NOTE_E7 263785#define NOTE_F7 279486#define NOTE_FS7 296087#define NOTE_G7 313688#define NOTE_GS7 332289#define NOTE_A7 352090#define NOTE_AS7 372991#define NOTE_B7 395192#define NOTE_C8 418693#define NOTE_CS8 443594#define NOTE_D8 469995#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
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.