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

Arduino NANO 3X3X3 LED CUBE

DIY | 3X3X3 LED CUBE FOR ARDUINO NANO+ By   RGBFreak   in   Technology Arduino 17,143 146 22 Featured Download Favorite By   RGBFreak RGBFreak's YouTube Channel Follow More by the author: About: Hi there visitor! First of all thank you for checking out my profile! My name is Youri. I study Technical Computer Science in the Netherlands. I especially love the electronical part of my study. Since I l...   More About RGBFreak » Intro: DIY | 3x3x3 LED Cube for Arduino Nano+ Hi everyone! My name is RGBFreak and I created a YouTube channel quite recently. I love DIY and I especially love LED's, so that's why I decided to make my own 3x3x3 LED Cube! You will need the following items to make this LED Cube: • 27 single colored LED's. • Arduino Nano or one of his bigger brothers. • 3 NPN Transistors. I used the BC547. • 3 pin headers with 3 pins. • 1 pin header with 4 pins. • A piece of perfboard. • A few small cables. Ad

Ultrasonic Distance Sensor in Arduino With Tinkercad

  Ultrasonic Distance Sensor in Arduino With Tinkercad Let's measure distances with an ultrasonic rangefinder (distance sensor) and Arduino's digital input. We'll connect up a circuit using a breadboard and use some simple Arduino code to control a single LED. You may have already learned to   read a pushbutton   and   PIR motion sensor   with Arduino's digital input, and we'll build on those skills in this lesson. Ultrasonic rangefinders use sound waves to bounce off objects in front of them, much like bats using echolocation to sense their environment. The proximity sensor sends out a signal and measures how long it takes to return. The Arduino program receives this information and calculates the distance between the sensor and object. Find this circuit on Tinkercad Explore the sample circuit embedded here by starting the simulation and clicking on the proximity sensor. This will activate a highlighted area in front of the sensor with a circle "

DIGITAL UV-METER, WITH OLED DISPLAY. ARDUINO PROJECT FOR BEGINNERS

DIGITAL UV-METER, WITH OLED DISPLAY. ARDUINO PROJECT FOR BEGINNERS By   techn0man1ac   in   Technology Arduino 4,792 87 13 Featured Download Favorite By   techn0man1ac Techn0man1ac blog Follow About: Techn0man1ac - техноманьяк   More About techn0man1ac » Intro: Digital UV-meter, With OLED Display. Arduino Project for Beginners Video demonstration (English subtitles). Hello,   instructable . Today I will tell you how to make a simple digital VU meter (sound level meter) using Arduino and OLED displays and 2 resistors by yourself (DIY). The device is quite simple, for beginners it will be a rewarding experience. Add Tip Ask Question Comment Download Step 1: Components for This Arduino Project: 3 More Images Arduino Nano V3.0; 0.96 inch IIC I2C 128X64 OLED Display; One 10K, resistor(R1 no scheme); One 10K-100K potentiometer(R2no scheme); Wires. Add Tip Ask Question Comment Download Step 2: Dev