We’re back from summer camp! I’m learning about Arduino and found this tutorial, which contains circuit, schematic, and code for playing a little melody over a speaker. You’ll need an Arduino Uno board, a speaker, a 9-volt battery and snap, and a switch.
Download Arduino IDE, which is an open source software that makes it easy to write code and upload it to the Arduino board.
Copy-paste the code from this tutorial into the Arduino IDE.
If you’re a bit of a music geek like I am you’ll enjoy modifying the tune into whatever you want it to play. There is a line of code devoted to the notes and another line devoted to note durations. The pitches.h file defines each note; C4 = middle C, S = sharp so CS4 means middle C sharp. I changed the melody to the first line from the summer camp song, “Down Where the Birches Sway.” Listen to it on my Instagram Story.
Arduino programs are called sketches. How to upload a sketch from computer to Arduino:
- Go to Tools > Boards and select Arduino/Genuino Uno from the drop down menu.
- Select the serial port from Tools > Port that works best.
- Check that the code is has no errors by choosing Sketch > Verify/Compile. Correct any errors spotted.
- If you haven’t yet, connect the Arduino Uno to your computer via a USB cable.
- Click Sketch > Upload to send the sketch to the Arduino board.
Once the sketch is loaded on to the Arduino, disconnect from the computer and connect your set up. For playing this simple melody, I only need an output speaker, a power source, and a switch. The tutorial shows the schematic for how to set it up.
This is a fun first Arduino sketch for me to play with. Explore more of them by selecting File > Examples in Arduino IDE.