Posts with «tone()» label

Build an 8-Key Piano on a Breadboard with Arduino

An 8-key piano on a breadboard with an Arduino Uno R3, eight 6×6 mm tactile buttons and a passive piezo buzzer. Each key you press plays a note of the C major scale, from C4 at 262 Hz up to C5 at 523 Hz. That is enough to play tunes like Twinkle Twinkle Little Star. No shield, no external library and no soldering are needed: just a breadboard, jumpers and a USB cable.

Assembly is the quickest part. The tactile buttons go into the breadboard straddling the centre channel, one next to the other. Each button has one side connected to an Arduino digital pin and the other side to GND. The passive buzzer has its positive leg on pin D10 and its negative leg on GND. A jumper connects the breadboard’s GND rail to the board’s ground.

The keys occupy pins D2 to D9, eight consecutive digital pins. The Arduino Uno has 14 digital pins (D0-D13) and 6 analog pins usable as digital inputs, so there is plenty of room to add keys in a later version. For the keys, the catalogue has a 6×6 mm PCB mini pushbutton, the same format used in the project.

INPUT_PULLUP and the tone() function in the firmware

The code uses no library: it relies on the tone() function built into the Arduino IDE. Each button is connected between a pin and GND, so the firmware enables the internal pull-up resistor with INPUT_PULLUP. As a result the pin reads HIGH when the key is at rest and LOW when it is pressed. The code scans the eight pins, stores the first key pressed and calls tone() with the associated frequency.

The comparison if (pressedKey != lastKey) makes tone() fire only once per press, instead of repeating the call on every pass of the loop. When no key is pressed, the code calls noTone() and the sound stops. The serial monitor opens at 9600 baud and is used to check which keys are being read during testing.

  • Key 1: C4 at 262 Hz
  • Key 2: D4 at 294 Hz
  • Key 3: E4 at 330 Hz
  • Key 4: F4 at 349 Hz
  • Key 5: G4 at 392 Hz
  • Key 6: A4 at 440 Hz
  • Key 7: B4 at 494 Hz
  • Key 8: C5 at 523 Hz

The piano is monophonic, and that is a limitation of the tone() function: it generates only one frequency at a time. So if you press two keys together, the one with the lower number wins, that is, the first one the loop encounters. Anyone who wants real chords has to change approach, for example generating the waveforms by hand with a timer.

Passive buzzer, current draw and an alternative speaker

The passive piezo buzzer works from 3.3V to 5V and draws less than 30 mA, so you can drive it straight from an Arduino pin without a transistor. Its best output is around 2-4 kHz, while the notes of this piano run from 262 Hz to 523 Hz. The sound comes out, but don’t expect concert volume: to raise the sound pressure, a speaker is a better bet.

Instead of the buzzer you can use an 8 ohm speaker with a 100 ohm resistor in series. The resistor limits the current and protects the pin. On top of that, the sound is fuller on the low notes of the scale. The rest of the circuit does not change: positive on D10, negative on GND.

Rebuilding the project takes only a few parts: an Arduino Uno R3, eight 6×6 mm tactile buttons, a passive piezo buzzer, a breadboard, jumpers and a USB cable. The cost is low and so is the difficulty. In fact it is an ideal first project for learning about digital inputs, INPUT_PULLUP and the tone() function, and you can finish it in an evening.

If you want to see the build in action, on the maker’s site you will find the video with the piano played key by key. There you can see how the buzzer behaves on the different notes before soldering or buying anything.

Source: https://www.circuitschools.com/arduino-piano-project-with-8-keys-build-a-simple-piano/

Related products

The post Build an 8-Key Piano on a Breadboard with Arduino appeared first on Open Electronics.