Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-based Pushbutton Music Keyboard

View and Edit Project In Cirkit DesignerImage of Arduino UNO-based Pushbutton Music Keyboard

Circuit Documentation

Summary

The circuit in question is designed to function as a simple electronic music keyboard using an Arduino UNO microcontroller. The circuit includes multiple pushbuttons that, when pressed, trigger the Arduino to output a tone to a speaker. Each pushbutton corresponds to a different musical note. The circuit also incorporates resistors, likely for pull-down purposes to ensure a stable low state when the buttons are not pressed.

Component List

Microcontroller

Input Components

Output Components

Passive Components

Wiring Details

Arduino UNO

Pushbuttons

Speaker

Resistors

Documented Code

/*
// Simple DIY Electronic Music Projects
//    diyelectromusic.wordpress.com
//
//  Arduino Music Keyboard
//  https://diyelectromusic.wordpress.com/2020/06/01/simple-arduino-music-keyboard/
//
    MIT License
    
    Copyright (c) 2020 diyelectromusic (Kevin)
    
    Permission is hereby granted, free of charge, to any person obtaining a copy of
    this software and associated documentation files (the "Software"), to deal in
    the Software without restriction, including without limitation the rights to
    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN
    AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
  Using principles from the following Arduino tutorials:
    toneMelody - http://www.arduino.cc/en/Tutorial/Tone
    Button     - http://www.arduino.cc/en/Tutorial/Button

*/

// Pitches taken from definitions in toneMelody
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440

// Set up the input and output pins to be used for the speaker and keys
#define SPEAKER  10
int keys[] = {
  2, 3, 4, 5, 6, 7,
};

// Set the notes to be played by each key
int notes[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4,
};


void setup() {
  int numkeys = sizeof(keys)/sizeof(keys[0]);     // Programming trick to get the number of keys
  // Initialise the input buttons as piano "keys"
  for (int k = 0; k < numkeys; k++) {
    pinMode (keys[k], INPUT);
  } 
}

void loop() {
  // Check each button and if pressed play the note.
  // If no buttons are pressed, turn off all notes.
  // Note - the last button to be checked will be
  // the note that ends up being played.
  int numkeys = sizeof(keys)/sizeof(keys[0]);
  int playing = -1;
  for (int k = 0; k < numkeys; k++) {
    int buttonState = digitalRead (keys[k]);
    if (buttonState == HIGH) {
      playing = k;
    }
  }
  
  if (playing != -1) {
    tone (SPEAKER, notes[playing]);
  } else {
    // If we didn't find a button pressed, make sure everything is off
    noTone(SPEAKER);
  }
}

This code is designed to run on the Arduino UNO microcontroller. It initializes the digital pins connected to the pushbuttons as inputs and the pin connected to the speaker as an output. In the main loop, it checks the state of each pushbutton and plays the corresponding note on the speaker when a button is pressed. If no buttons are pressed, it ensures that no tone is output to the speaker.