Skip to main content

ARDUINO AS A PIANO

ARDUINO AS A PIANO

2,672
21
1

Intro: 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

Picture of Understanding the Basics
Picture of 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

Picture of 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

Picture of Schematic & Code
Picture of 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 !!

Comments

Popular posts from this blog

S4A (Scratch for Arduino) Project - A Keyboard Controlled Robot Car A fun project to create a keyboard controlled robot car... based on the   "Robotics: keyboard driven car" project on the   S4A site . This to me, is a perfect example of an interdisciplinary project: there is quite a bit of mechanical, electrical and computer engineering involved. This project is being used by the Grade 5 kids in my child's school this year; the kids have been working with the Pro-Bot for quite a bit of time and I felt that letting them create their own robot car that behaves similar to the Pro-Bot is a great finale. So far, it's my favorite Arduino project. Below is a formal lesson plan for the robot car. Note:   This is a simplified version of the car from the S4A site - it just involves creating the keyboard driven car, minus the remote controller. This means the car stays connected to the laptop via the USB cable.   Aim Design your own robot car, whose movements can be c...

TOUCH SWITCH CIRCUIT WITH MOSFET

TOUCH SWITCH CIRCUIT WITH MOSFET By  leeselectronic  in  Technology Electronics 1,326 13 Featured Download Favorite By  leeselectronic Lee's Electronic Follow More by the author: About: Electronic Hobby Store  More About leeselectronic » Intro: Touch Switch Circuit With MOSFET Created by: Jonsen Li Overview: The simple touch switch LED circuit utilizes biasing characteristics of the MOSFET. MOSFET stands for Metal-oxide-semiconductor field effect transistors. It is a voltage controlled device meaning that the current passing through the device is controlled by the voltage between two terminals. Parts you will need: A power MOSFET (IRFZ-44 NPN)  (Lee's ID: 71211) 9V battery  (Lee's ID: 83741) 12V LED bulb  (Lee's ID: 5504) Jumper wires  (Lee's ID: 21802) Breadboard  (Lee's ID: 10686) 9V battery clip  (Lee's ID: 653) Add Tip Ask Question Comm...