DIY: Create Your Own Beautiful Arduino Christmas LEDs tree, that plays “Jingle Bells”
Winter is here, it’s cold and windy, and every day seems like a great day to stay in bed. Whether you celebrate Hanukkah or Christmas, the holiday season is here to remind us to gather around and light up the darkness.
Today we are going to do just that — but with Arduino.
By the end of this project, you will know how to convert musical notes to Arduino code and play the buzzer, how to set up LEDs in your favorite shape, and how to make them blink according to the song you are playing.
The fine print- In order to keep things simple, I assume some background in Arduino and familiarity with working with LEDs and the buzzer. If you are a beginner, you can find detailed explanations, from the ground up, about these topics, tutorials about Arduino, and other materials on my website. It also contains a step by step installation tutorial.
For this project, I used the “Adafruit-feather-32u4-bluefruit-LE”, an Arduino-compatible board designed and manufactured by Adafruit, but you can use your favorite Arduino board.
The video above shows the final result for this project. The source code is available on GitHub, we will go over it part by part.
Now, let’s get to work!
Step #1: Setting Up the LEDs and the Buzzer
Even though the Christmas tree seems complicated, it is actually based on this simple diagram, that I described in detail here. When you get the basics of it — you can add LEDs and make sure they are connected correctly to the relevant GPIO on the one side and to GND on the other.
The important thing is to define the group of LEDs you want to blink and connect them to the relevant GPIO.
In my tree, I connected all 8 green LEDs and the yellow LED to GPIO #11. These LEDs constructed the tree itself, and the yellow represents a star on the top of it.
I wanted to create two different groups of red LEDs, and an additional orange LED. My code shall be able to blink the groups in an alternating way. For this matter, I separated them into GPIOs 10, 12, and 13 accordingly.
The Buzzer was connected to pin #9 that is a PWM pin.
Step #2: Converting Musical Notes to Arduino Code
The first part “jingle_bells.ino” source code is a list of constant numbers, that represents the different musical notes, and more specifically, the frequencies for the buzzer.
The second part of the code defines two correlative arrays, the first holds the notes, and the second holds the duration of each note. Instead of having two long arrays, I divided them into a few small arrays, for each section of the song. It helped me in the debug process, and I also could reuse them when I needed (see the usage of “first_notes’ for example).
How can you take every song and play it on the buzzer? easily!
The first step is to fill the notes array. Make sure all the notes you need are in the range of the buzzer, and that the frequencies are defined at the beginning of the code. The second step is to fill the duration. Note that the sheet music contains this information and that the notes are partitioned to groups according to the summed duration. You don’t need to be a musician in order to write the code, just keep the ratios reasonable :)
The following picture helps to illustrate the process of converting musical notes to code.
Step #3: Playing the Song and Blinking LEDs
Okay, so we have the arrays of nodes and durations, now all that is left is to play them and blink the LEDs.
In the picture, you can see how the notes are just played one by the other. There is an assumption that the arrays are of the same length.
We already defined what groups of LEDs are connected to the same GPIOs. but we still need to decide in what order we would blink them, and whether or not they would blink together.
I wanted the green LEDs and the yellow LED to be always ON. I also wanted to light the LEDs in a predefined pattern, such that at the end of it, all the LEDs would light up together.
I used a modulo operation inside the beep(…) function, in order to loop over the different groups of LEDs.
We set the tree up, wrote the code for the buzzer, programmed the LEDs, Now all is done! You can sit by the tree, enjoy the song, and let light conquers darkness!
Create your own project and share it with us!