Arduino Piano

by 833856 in Circuits > Arduino

22 Views, 1 Favorites, 0 Comments

Arduino Piano

20250615_140818.jpg
20250615_163810.jpg

This project makes a simple keyboard using an Arduino Uno system. The eight buttons play a scale, which alternates between major and minor based on the position of the slideswitch. See the schematic in the image above.

Supplies

20250614_151918.jpg

Arduino Uno with cable

Solderless breadboard x1

Tactile push button x8

Slide switch x1

33Ω resistor x8

Jumper wires x14 (or more)

Passive piezo buzzer x1

LED x1

Button and Resistor Setup

20250614_152357.jpg

Connect eight buttons across the middle of the breadboard, and wire a resistor to the negative bus for each of them. Although the image shows a combination of two- and four-pronged buttons, I recommend using two-pronged buttons for every note.

Wiring

20250614_153642.jpg
20250614_153633.jpg

Use jumper cables to make a connection between the buttons and certain pins on your Arduino Uno. For the purposes of this project, I used pins 3 to 10 inclusive (while reserving pin 11 for the switch and 12 for the buzzer), but any pins should work because the buttons are digital input devices.

Add the Switch

20250614_153809.jpg

Connect a jumper cable from pin 11 (or any unused pin) to one of the side terminals of your slideswitch. Then ground the common terminal of the switch, wiring an LED in series. This switch will change the scale from minor to major and vice versa.

Add the Buzzer

20250614_153937.jpg
20250614_154007.jpg

Wire your buzzer to pin 12 (or any other unused pin) and then ground it. Then connect the 5V and GND pins from the Arduino to the positive and negative buses.

Coding

Screenshot 2025-06-15 164553.png
Screenshot 2025-06-15 164621.png
Screenshot 2025-06-15 164636.png
Screenshot 2025-06-15 164645.png

The bulk of this project is the coding step, which can be broken down into more concise instructions:

  1. Initialize the pins for the buzzer, buttons, and switch, ensuring that they correspond to the correct ones on your Arduino.
  2. Create an array of eight notes which will serve as your scale; I used A Major as my default scale. - each element in the array is an integer value that represents the frequency of each note in Hz (see this website for conversions)
  3. In the setup function, initialize your buzzer pin as an OUTPUT and all other pins as INPUT_PULLUP (do not simply use INPUT since our buttons are wired without pull-up resistors)
  4. In the loop function, initialize the variables representing the button and switch states by declaring them as integers.
  5. Use if conditions to check the state of the buttons. If they are equal to 0 (the button is held down), then use the tone(), delay(), and noTone() functions to play a certain note for 100 milliseconds. Ensure you are indexing the notes array starting from 0.
  6. Finally, check the state of the switch. If the switch pin reads 0, initialize an array with the frequencies of a different scale, then use a for loop to set the values of the main notes array to those of the new scale. If it reads 1, then carry out the same procedure, except resetting the notes to those in your default notes array on line 11.

Packaging

20250615_140818.jpg

Package the circuit in a cardboard box and feel free to design it anyway you want, making sure you leave the switch, buttons, LED, and buzzer exposed. Then your project is ready to go!