Notas musicais baseadas em Arduino e gerador de melodia com LCD

Arduino based musical notes and melody generator with LCD

Arduino has a wide variety of applications. It can be used in almost all fields. Its applications are increasing day by day because it is open source and anyone can create a new set of functions and library to interface any new device with Arduino. The provided application demonstrates the use of Arduino as a tone and melody generator. Includes keyboard and LCD for user interface. Musical notes or melody are generated when the key is pressed and the frequency of the generated sound is displayed on the LCD. So actually, the given application illustrates interfacing keyboard and LCD with Arduino along with generating tones and melodies.
Protótipo de notas musicais e gerador de melodia baseado em Arduino

Figure 1: Arduino-based musical note prototype and melody generator

Circuit Description:

As shown in the circuit, a 16×2 LCD, a 4×3 matrix keyboard, and an 8Ω speaker are interfaced with the Arduino board.
• The Vcc pin (2) and Vss pin (1) of the LCD are connected to the +5 V and Gnd pins of the Arduino board respectively to provide biasing.
• The LED+ pin (15) and LED- pin (16) of the LCD are also connected to the +5 and Gnd pins respectively to turn ON the LED backlight of the LCD.
• Rs pin (4) and En pin (6) are connected to Arduino digital pins 8 and 9, while Rw pin (5) is permanently connected to ground for write enablement.
• Data pins D4 – D7 of LCD are connected to digital pins 10 – 13 of Arduino
• 3 columns C1, C2 and C3 of the matrix keyboard are connected with 0,1 and 2 pins of the Arduino board while 4 rows R1, R2, R3 and R4 are connected with 3, 4, 5 and 6 pins.
• An 8Ω speaker is connected to pin 7 as shown in the circuit diagram.

Circuit Operation:

• Initially, the message is displayed on the LCD as “keyboard tone generator”
• When any key is pressed, the key press is detected and Arduino identifies which key was pressed
• Based on the key number pressed, specific frequency sound* (tone) is generated through the speaker for 1 second
• The key number is displayed on the LCD and the frequency of the generated sound is displayed on the LCD
• As shown in the figure, the keypad is the telephone keypad (numeric) and includes the * key and # key. So when the * key is pressed it generates the tone 1 melody and when the # key is pressed it generates the tone 2 melody which is displayed on the LCD
*Note: The frequency of the generated sound is chosen to produce 'sa', 're', 'ga', 'ma', 'pa', 'dha' and 'ni' all seven musical notes
The full functionality of this project is due to the software program loaded into the internal FLASH memory of the Arduino ATMega328 board microcontroller. So let's look at the program and its logical explanation.

Software program and logic:

The software program is written in the Arduino language. It is compiled using the Arduino IDE software tool and loaded into the internal FLASH memory of the Arduino board microcontroller. We all know that Arduino IDE has a powerful library package that includes a complete set of functions for a specific peripheral device. In this program, we are using three libraries:
1) matrix keyboard – to interface the matrix keyboard with Arduino
2) liquid crystal – to interface the LCD with the Arduino
3) tone generator – to generate sound of different frequency
The matrix keyboard library allows us to configure our matrix keyboard as a number of rows-columns, key pattern, etc. It detects keystrokes, identifies which key is pressed, and performs many other functions. The liquid crystal library is, as we know, especially for interfacing different types of LCDs with Arduino. It configures the LCD, displays text, numeric data, clears the LCD, sets cursor positions, etc. Tone generator is used to generate sound of specific frequency from any digital pin of Arduino.

Project source code

###

 //Program to

 #include

 #include


 const byte ROWS = 4; // Four rows

 const byte COLS = 3; // Three columns

 int sound(10) = {1047,1109,1245,1319,1397,1480,1568,1661,1760,1865};

 int sound_pin = 7;

 // Define the Keymap

 char keys(ROWS)(COLS) = {

 {'1','2','3'},

 {'4','5','6'},

 {'7','8','9'},

 {'#','0','*'}

 };
 
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

 byte rowPins(ROWS) = { 3, 4, 5, 6 };

 // Connect keypad COL0, COL1 and COL2 to these Arduino pins.

 byte colPins(COLS) = { 0, 1, 2 };


 //Create the Keypad

 Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

 LiquidCrystal LCD(8, 9, 10, 11, 12, 13);

 #define ledpin 13

 void setup

 {

 pinMode(ledpin,OUTPUT);

 digitalWrite(ledpin, HIGH);

 lcd.begin(16, 2);

 lcd.clear;

 lcd.print("keypad tone");

 lcd.setCursor(4,1);

 lcd.print("generator");

 }

 void loop

 {

 int i;

 char key = kpd.getKey;

 if(key) // Check for a valid key.

 {

 switch (key)

 {

 case '*':

 lcd.clear;

 lcd.print("playing melody 1");

 for(i=0;i<10;i++)

 tone(sound_pin,sound(i),500);

 break;

 case '#':

 lcd.clear;

 lcd.print("playing melody 2");

 for(i=0;i<10;i++)

 tone(sound_pin,sound(10-i),500);

 break;

 default:

 lcd.clear;

 lcd.print("key:");

 lcd.print(key);
 
lcd.setCursor(0,1);

 lcd.print("sound ");

 lcd.print(sound(key-48));

 lcd.print("Hz");

 tone(sound_pin,sound(key-48),1000);

 break;

 }

 }

 }

###

Circuit diagrams

Circuit-Diagram-Arduino-Based-Music-Notes-Melody-Generator

Project video

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.