Create and Code Your Own Mini Piano with Arduino

13 May 2023 Balmiki Mandal 0 µC - µP

Arduino Tutorial: How to Make a Mini Piano

Making a mini piano with Arduino is the perfect project for anyone interested in music tech, especially those who want to learn more about using Arduino. With just a few components and some basic coding knowledge, you can make your own working mini piano with Arduino. Here’s how!

Step One: Gather Your Components

For this project, you’ll need the following components:

  • An Arduino (Uno, Nano, Pro Mini or other)
  • A buzzer/piezo
  • Pushbuttons (x 9)
  • A 10K ohm resistor
  • A breadboard and some hookup wires

Step Two: Connect the Components

To get started, connect 8 of the pushbuttons and the 10K ohm resistor to the breadboard like so:

"diagram

Make sure to connect each of the buttons to the same column on the breadboard. The resistor is used to protect the Arduino from damage when multiple buttons are pressed at once. Connect the buzzer and the 9th button to the remaining columns on the breadboard. Finally, connect the breadboard to the Arduino.

Step Three: Write the Code

Now it’s time to write the code that will control the piano. Start out by setting up the pins your buttons and buzzer are connected to. Then, create functions for each tone of the piano. Next, you’ll need to read in the inputs from the pushbuttons and play the corresponding tone. Finally, write the main loop to handle user input and activate the appropriate functions.

Here is an example Arduino source code for a mini piano:

// Mini Piano

// Pin definitions
#define SPEAKER_PIN 3
#define BUTTON_C_PIN 4
#define BUTTON_D_PIN 5
#define BUTTON_E_PIN 6
#define BUTTON_F_PIN 7
#define BUTTON_G_PIN 8
#define BUTTON_A_PIN 9
#define BUTTON_B_PIN 10

// Note frequencies
#define C 261
#define D 294
#define E 329
#define F 349
#define G 391
#define A 440
#define B 493

void setup() {
  pinMode(BUTTON_C_PIN, INPUT_PULLUP);
  pinMode(BUTTON_D_PIN, INPUT_PULLUP);
  pinMode(BUTTON_E_PIN, INPUT_PULLUP);
  pinMode(BUTTON_F_PIN, INPUT_PULLUP);
  pinMode(BUTTON_G_PIN, INPUT_PULLUP);
  pinMode(BUTTON_A_PIN, INPUT_PULLUP);
  pinMode(BUTTON_B_PIN, INPUT_PULLUP);
  pinMode(SPEAKER_PIN, OUTPUT);
}

void loop() {
  // Read button states
  int buttonCState = digitalRead(BUTTON_C_PIN);
  int buttonDState = digitalRead(BUTTON_D_PIN);
  int buttonEState = digitalRead(BUTTON_E_PIN);
  int buttonFState = digitalRead(BUTTON_F_PIN);
  int buttonGState = digitalRead(BUTTON_G_PIN);
  int buttonAState = digitalRead(BUTTON_A_PIN);
  int buttonBState = digitalRead(BUTTON_B_PIN);
  
  // Check which button is pressed and play corresponding note
  if (buttonCState == LOW) {
    playNote(C);
  } else if (buttonDState == LOW) {
    playNote(D);
  } else if (buttonEState == LOW) {
    playNote(E);
  } else if (buttonFState == LOW) {
    playNote(F);
  } else if (buttonGState == LOW) {
    playNote(G);
  } else if (buttonAState == LOW) {
    playNote(A);
  } else if (buttonBState == LOW) {
    playNote(B);
  } else {
    noTone(SPEAKER_PIN);
  }
}

void playNote(int frequency) {
  tone(SPEAKER_PIN, frequency);
}

This code assumes that the mini piano has seven buttons for playing notes and a speaker for producing sound. The code defines the pin numbers for the buttons and speaker, as well as the frequencies for each note. The setup function sets the pin modes for the buttons and speaker. The loop function reads the state of each button and plays the corresponding note using the playNote function. The playNote function takes a frequency parameter and plays that frequency on the speaker using the tone function. If no button is pressed, the noTone function is called to stop playing any note.

Step Four: Play!

Once you’ve written the code, upload it to the Arduino and you’re ready to start playing! With just a few components and some basic coding knowledge, you can make your very own mini piano with Arduino. Have fun!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.