
Arduino can be turned into a small piano that plays real musical notes through a simple buzzer, and it can even record the tune a user plays and replay it automatically afterward.
Components needed
An Arduino Uno board, eight push buttons representing piano keys, a buzzer for playing notes, an LCD screen for status display, a toggle switch to select between recording and playback modes, and a set of resistors to build a voltage-divider network.
Wiring it up
Instead of dedicating a separate digital pin to each of the eight buttons, all the buttons connect through a voltage-divider resistor network into a single analog pin, so each button produces a distinct voltage when pressed that the Arduino can tell apart. The buzzer connects to a digital pin that supports the tone() function.
How it works
Each musical note is tied to its own frequency in hertz — for example, C4 corresponds to 262 Hz and C5 to 523 Hz, and so on for the rest of the seven notes in the scale. When a button is pressed, the Arduino reads the resulting analog voltage and identifies the pressed button by matching it against a known voltage range, then plays the buzzer at the matching frequency via tone(). In recording mode, the code saves each pressed button's number and the time gap since the previous press into two arrays. In playback mode, the code reads these arrays back and plays the notes in the exact same order and timing, faithfully reproducing the recorded melody.
You can find buzzers, push buttons, and the precision resistors needed for this project at Juthour Tech.
اترك تعليقا