Welcome to a new Arduino project. In this tutorial, you will learn how to play the Spanish anthem, or any other song, with Arduino. You will also learn how to create all kinds of songs with Arduino so you can reproduce many different melodies.
Table of contents
Materials used to play songs with Arduino
Below you can see the different materials used in this project. To play the Spanish anthem with Arduino, it is essential to use a passive buzzer.
If you want to build the project yourself, you can click the images to go to a website where you can buy the materials.
Arduino Uno board: it is the brain of the project and controls every part of it through the code you will find below.
A board with interconnected holes (pins) that we use to make all the project connections.
Jumper wires: these wires have male or female pin connectors that let us connect the different elements mentioned above.
Passive buzzer: a component with a membrane that vibrates when it is driven by a PWM signal at different frequencies. Unlike an active buzzer that only produces one tone, a passive buzzer can reproduce all kinds of notes.
Components used in the project
Below you can find a list with all the components used in the project so you can go directly to the site and buy them.
IMPORTANT! The buzzer must be passive in order to reproduce different melodies.
WE DEVELOP YOUR IDEA
Need help with a project?
- Prototyping and MVPs
- Arduino and ESP32
- PCB design
- 3D part design
- Bluetooth connectivity
- Feasibility study
- Cost optimization
- Technical consulting
- Internet of Things
- Patent supbyt
How to play songs with Arduino
If you want a much more detailed walkthrough of how to build this project from start to finish, including a code explanation, the video below covers everything in a more visual and easier-to-follow way. And remember, if you enjoy this kind of content, do not forget to subscribe.
Wiring diagram to connect the buzzer to your Arduino
One of the most important parts of playing a melody with Arduino is wiring the passive buzzer correctly.
In this case, the assembly is very simple, but for the code to work correctly, the pins must be connected exactly as shown in the diagram below.
With this wiring diagram, you can use the code at the end of the post without making any modifications.

Equivalent musical notes and frequencies for Arduino
With the simple table below, you can create any song.
| Musical note | Frequency in hertz (Hz) |
| C | 523.25 |
| D | 587.33 |
| E | 659.26 |
| F | 698.46 |
| G | 783.99 |
| A | 880 |
| B | 987.77 |
| D2 | 1174.66 |
| C2 | 1062 |
| DS | 622.25 |
| FS | 739.99 |
| GS | 830.61 |
| FS2 | 1479.98 |
Arduino code for the Spanish anthem with Arduino
Below you can find the code developed specifically to play the Spanish anthem. The pins used in the code are the same ones shown in the wiring diagram above.
If you want a more detailed explanation of the different parts of the code used to program the Spanish anthem and how it works, I recommend watching the video linked above.
If you have questions about how to use or install the Arduino development environment, here is a link to how to download the Arduino IDE





//Canal de YouTube -> Robot UNO<br>//Proyecto -> Himno de España con Arduino
#define DO 523.25
#define RE 587.33
#define MI 659.26
#define FA 698.46
#define SOL 783.99
#define LA 880
#define SI 987.77
#define RE2 1174.66
#define DO2 1062
#define DoS 554.37
#define RES 622.25
#define FAS 739.99
#define SOLS 830.61
#define FAS2 1479.98
#define a 1000
#define b 500
void setup() {
pinMode(8,OUTPUT);
}
void loop() {
//---------------- PRIMERA PARTE
tone(8, SOL , a);
delay(500);
noTone(8);
tone(8, RE , a);
delay(500);
noTone(8);
tone(8, SI , a);
delay(500);
noTone(8);
tone(8, SOL , b);
delay(500);
noTone(8);
tone(8, RE2 , b);
delay(400);
noTone(8);
tone(8, DO2 , b);
delay(400);
noTone(8);
tone(8, SI , b);
delay(400);
noTone(8);
tone(8, LA , b);
delay(400);
noTone(8);
tone(8, SOL , b);
delay(400);
noTone(8);
tone(8, SOL , b);
delay(400);
noTone(8);
tone(8, FAS , b);
delay(400);
noTone(8);
tone(8, MI , b);
delay(400);
noTone(8);
tone(8, RE , b);
delay(400);
noTone(8);
tone(8, SOL , a);
delay(500);
noTone(8);
tone(8, LA , a);
delay(500);
noTone(8);
tone(8, SI , 1500);
delay(1000);
noTone(8);
tone(8, RE2 , b);
delay(400);
tone(8, DO2 , b);
delay(400);
tone(8, SI , b);
delay(400);
tone(8, LA , b);
delay(400);
tone(8, SOL , b);
delay(400);
tone(8, RE2 , 2000);
delay(1000);
//--------------------- SEGUNDA PARTE
tone(8, RE2 , a);
delay(500);
tone(8, SI , 250);
delay(500);
tone(8, RE2 , 250);
delay(400);
tone(8, DO2 , a);
delay(400);
tone(8, LA , 250);
delay(500);
tone(8, DO2 , 250);
delay(400);
tone(8, SI , a);
delay(500);
tone(8, SOL , 250);
delay(400);
tone(8, SI , 250);
delay(400);
tone(8, LA , b);
delay(400);
tone(8, RE , b);
delay(400);
tone(8, MI , b);
delay(400);
tone(8, FAS , b);
delay(400);
tone(8, SOL , a);
delay(500);
tone(8, LA , a);
delay(500);
tone(8, SI , 250);
delay(500);
tone(8, DO2 , 250);
delay(400);
tone(8, RE2 , b);
delay(400);
tone(8, DO2 , b);
delay(400);
tone(8, SI , a);
delay(500);
tone(8, LA , a);
delay(500);
tone(8, SOL , 2000);
delay(1000);
}



