Introduction: Arduino As a Piano

What this project represents is: you can generate close to any desired frequency using Fast Pulse Width Modulation on any microcontroller up to a top frequency limit (depending on the system clock).

Here, Arduino Uno is used, which has 16 MHz system clock. So, you can generate a maximum frequency of roughly 8 MHz using PWM.

We will using arduino timers and registers to code it.

Skill: Intermediate

You just need:

  • Arduino Arduino UNO
  • Piezoelectric buzzer
  • 4x4 membrane keypad

Step 1: Understanding the Basics

The frequencies of basic notes of a piano are as follows:

  • c - 261 Hz
  • d - 294 Hz
  • e - 329 Hz
  • f - 349 Hz
  • g - 392 Hz
  • a - 440 Hz
  • b - 493 Hz
  • C - 523 Hz

For beginners, this might go over their heads, and for people with non technical background this might be an 'all-greek-to-me' thing. I'm gonna make it simple.

There is an amazing tutorial about the same on Arduino's website. That's where I read and understood it. Basically, what you have to worry about is just 2 registers - TCCR2A and TCCR2B (2 because we are using timer 2 here).

  • TCCR2A - Timer/Counter Control Register A for Timer2)
  • TCCR2B - Timer/Counter Control Register B for Timer 2)

These are 8 bit registers and there 8 bits are as follows :

  • TCCR2A - [COM2A1, COM2A0, COM2B1, COM2B0, reserved, reserved, WGM21, WGM20]
  • TCCR2B - [FOC2A, FOC2B, reserved, reserved, WGM22, CA22, CA21, CS20]

A) Setting WGM22 WGM21 WGM20 as 111 will select the PWM mode - Fast PWM with toggle

B) Setting CA22 CA21 CS20 as 001 will select the pre-scalar as 1 (i.e. the frequency will be divided by 1. For different prescalar values , the table is as in image above.

Step 2: Understanding the Timers in Arduino

There is an amazing tutorial about the same on Arduino's website.

That's where I read and understood it. Basically, what you have to worry about is just 2 registers - TCCR2A and TCCR2B (2 because we are using timer 2 here).

  • TCCR2A - Timer/Counter Control Register A for Timer2)
  • TCCR2B - Timer/Counter Control Register B for Timer 2)

These are 8 bit registers and there 8 bits are as follows :

  • TCCR2A - [COM2A1, COM2A0, COM2B1, COM2B0, reserved, reserved, WGM21, WGM20]
  • TCCR2B - [FOC2A, FOC2B, reserved, reserved, WGM22, CA22, CA21, CS20]

A) Setting WGM22 WGM21 WGM20 as 111 will select the PWM mode - Fast PWM with toggle

B) Setting CA22 CA21 CS20 as 001 will select the pre-scalar as 1 (i.e. the frequency will be divided by 1. For different prescalar values , the table is as follows:

C) Setting COM2A1 COM2A0 as 01 for toggling counter when value reaches top of the OCR2A register. This is the output compare register A. Whatever the value you want your counter to count up to is assigned to this register.

Rest of the bits are 'don't care' for this project.

Step 3: ​Generating a Frequency:

Generating a frequency:

The formula is: frequency in Hz = 16MHz/prescalar/(OCRA+1)/2 (for register A i.e. pin 11).

For e.g. for generating 261 Hz:

  • TCCR2A = B01000011; (com2a1 com2a0 bits are 01 for toggling counter when value reaches 119)
  • TCCR2B = B00001110; (wgm22 wgm21 wgm20 bits are 111 for fast pwm, ca22 ca21 cs20 bits are 110 for selecting the prescalar 256)
  • OCR2A=119;

CALCULATING: 16MHZ/256/120/2 = 260.42 Hz

Similarly, by changing the OCR2A values you can generate rest of the frequencies using this formula. These frequencies are played by a piezoelectric buzzer, which has its +Ve terminal connected to the pin 11 of Arduino Uno and its -Ve terminal to the ground.

Now, for piano keys, I used a 4x4 membrane keypad. For its interfacing refer to my previous project. Use a better keypad and better buzzer for better results. Its code is shown in code1. You can use keypad library instead of this code if you want, to shorten the code.

The basic 8 notes are shown in video 1.

I also played the melody of 'Happy birthday to you', but its half (and twice as bad). It is shown in video2.

Step 4: Schematic & Code

Refer to the hand drawn schematic to create connections either using wires or breadboard.

Upload the code attached here using Arduino IDE.

Your buzzer will start playing the tone !!

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

First Time Author Contest

Participated in the
First Time Author Contest