This post is the part 9 of Arduino tutorial. Will be shown how to play music using a buzzer or a speaker and how to use a matrix keypad 4×4.
Playing tone sequence
To build this project, which plays a few tones, only needs a speaker and some wires. Can be a buzzer instead of speaker.
This is the source code.
In the new tone function, the first number is the digital pin connected to the sound transducer, the second one is the frequency in Hertz and the third one is the duration in milliseconds. This is the table of musical notes and your respective frequencies.
It is possible to play 8 bits videogame music and famous themes of games, movies, etc.
Matrix keypad 4×4
This keypad has 8 terminals, the 4 firsts from right to left, in relation to the figure above, are connected to lines and the other four are linked to rows.
Connecting the keypad to the Arduino. Don’t link in the terminals 0 and 1 of digital pins, or some keys won’t respond.
The program below is to test the keypad, you must download the keypad.h library, can be in this link. In tutorial 8, shows how to install a new library, click in the button below to see.
These commands below declare the number of lines and rows.
“matrixkeypad” is a characters matrix showing the keypad’s map and followed by two vectors indicating the pins of lines and rows.
This command inicializes the keypad, creating an object “Keypad” with the map “matrixkeypad” and the pins which will be used.
The first line of void loop() verify if any key was pressed. If yes, will appear in the serial screen.
The serial screen must appear in this way.
Playing music with the keypad
This project uses a speaker and the matrix keypad. When press a key, the speaker must emit a tone in a determined frequency. Below is the complete circuit assembled.
This is the algorithm. In the end, just add more keys and define sound frequency.