This article from circuitstoday.com website aims at developing a Monophonic (Single note) musical keyboard by generating the musical notes frequencies using a microcontroller (ATMEGA8). The project presented in this article is a 12 Key, Monophonic keyboard with the options.
To shift between the Higher and lower octaves
Play the programmed tunes
Record and save new tune in real-time
Hardware Presentation – Musical Keyboard Using Atmega8
Taking the Piano as a view point, every key of a Piano has a unique frequency. The musical notes are named as:
Names of the Musical Notes or Keys
The frequencies of these notes on a Piano are provided in the following web page – https://en.wikipedia.org/wiki/Piano_key_frequencies .
By knowing the frequency of a particular note from the frequency table, we can play the note using a microcontroller. There are 12 unique key notations as shown in the above image. The frequency of the note depends on the octave of the key pressed. There are 0 to 8 Octaves in a Piano.
By using a microcontroller with digital outputs, it is possible to generate square waves with the frequencies of musical notes. The 16-Bit timer-counter of the microcontroller is utilized to generate a square wave with desired frequency. The timer counter is operated in CTC Mode (Clear Timer on Compare Match). Each time when a match occurs between the TCNT1 Value and the Output Compare Registers, the Output Compare 1A Pin is toggled. Every time when a key is pressed, the Output Compare Registers of the Timer1 are loaded with a value corresponding to the frequency of the pressed key. The register defined from the description of registers given in the datasheet:- http://www.atmel.com/Images/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_datasheet.pdf
Musical Keyboard using ATmega8 – Registers Description
In our application, the register values in hexadecimal format are given below,
The timer is set to operate at a predefined frequency by selecting the prescaler value. Here, the internal 8 MHz RC oscillator is selected to reduce the external components as well as to have more number of I/O Pins. Now, the Fcpu=8 MHz and by selecting a prescaling value of 8 for the Timer the counting frequency becomes 1 MHz i.e.., it counts 10,00,000 in one second. Let a frequency of 100 Hz is required at the OC1A Pin. The frequency of 100 Hz corresponds to a timer count value of 10,000.
As the waveform should be symmetric, the OC1A Pin stays at the High state for the first half of the time period and at Low state for the second half of the time period. As the frequency of 100 Hz corresponds to a timer count value of 10,000, the Compare Match Register is set to 5000. After the timer counter starts counting from zero, the OC1A Pin toggles (for example, OC1A goes Low) when the count value reaches 5000. The Counter value is reset to Zero by the CPU itself with the timer in running state. Again when the count value reaches 5000 the OC1A Pin toggles again (OC1A goes The High this time). Thus a cycle is completed. This continues until Stop timer command is received.
As per the circuit operation, when a key is pressed the OCR1A (Output Compare Register) is loaded with a value corresponding to the pressed key frequency and the timer is started. When the key is released, the processor waits until the OC1A Pin goes low and then the timer is stopped. Otherwise, if the timer is stopped with the OCR1A Pin in the High state, the speaker draws current even in the silent state because the coil in the speaker acts as a load. Thus a note is played.
Musical Keyboard using ATmega8 – Circuit Diagram for Microcontroller and Decade counter
The circuit consists of a microcontroller (ATMEGA8), 20 Keys (Push to ON Switches) arranged in the Matrix keypad (5X4) format, 12 LEDs to show the pressed key, few Pull-Up and Current limiting resistors, a general purpose switching transistor (BC 547) to drive the speaker (Headphones), a 3.5 mm stereo socket to connect the headphones with the microcontroller.
Musical Keyboard using ATmega8 – Circuit Diagram for Keypad and Key LEDs
The key is identified in a way that is similar to Interfacing a matrix keypad. For a detailed explanation on interfacing a Matrix Keypad, please go through the following article on ‘Interfacing a Matrix Keypad’,
The key 1 to 12 are defined for musical notes and remaining are functional keys. So, when a key press is detected, if the pressed key is between 1 through 12 then, the Timer1 is started with respective frequency by considering the Octave value. To have an addressable access to all the notes, the frequencies corresponding to each octave are framed into a two-dimensional array. The first dimension holds the octave, while the second dimension holds the frequency value of the Nth note. As there are 9 octaves and each octave has 12 unique notes, the size of the array is frequencies . To call the note with its key number, let’s frame the notes from 1 to 12 locations of the array thereby leading to the array size of frequencies . The frequency table is provided below in a two-dimensional array. The frequency array contains the notes in terms of C,C_ and so on.., which are equal to keys 1 to 12. These are defined in the global declaration part of the program. These alphabet notations can be replaced with actual numbers.
Musical Keyboard Using ATmega8 – Download Frequency Table Code
The program file is provided below which is compiled by using the ‘winavr’ open source software.
Musical Keyboard Using ATmega8 – Download C Program for the Project
Now, by using this array we can play any note. For example, if we want to play the note C5 (‘C’ note of 5th Octave) then, we have to call ‘frequencies ’. Thus, after detecting the pressed key, the respective note is played. Remember that the default Octave is programmed to Octave 4, which can be varied in real-time. The piano contains notes up to ‘A8’. Notes after this location are repeated with A8 in the given array.
The circuit contains an indicator for each of the 12 notes. This will not only show the pressed key but also indicates the note that is being played while playing a tune. As there are 12 notes we may require 12 I/O Pins of the microcontroller in general. But here we have used the matrix technique. This reduces the number of I/O Pins from 12 to 7 by using a 4X3 Matrix configuration. The LEDs are arranged in such a way that, there are four columns and three rows. The columns are anodes of the LEDs and the Rows are Cathodes of the LEDs. In order to enable an LED, its column Pin should be given Logic 1 and the row Pin should be given Logic 0.
Program snippet to turn ON the LED of pressed musical key
Pages: 1 2