Education STEM

Arduino: Building a music tempo controller with simple components

In this project, I built a simple system where we can play a tune/music through a speaker (sound/tone is of our choice) and while it’s playing, we can control the tempo in real time.

For the speaker, I used an inexpensive passive buzzer hooked up to a 10K resistance potentiometer. I insert a turning knob on the potentiometer so we can easily turn it to adjust the resistance. In my code, continuously read its values through another wire and send signals accordingly to the buzzer.

The code is written in the Arduino IDE (C/C++), then downloaded to the chip onboard an Arduino-compatible board UNO. Needless to say, it was a lot of fun! Sorry, I don’t have the video for this as I’ve moved on to other projects and had to dismantle it…but don’t despair there are more fun projects that’ll be shared here. And at some time point (if I ever find the motivation and time), I will write detailed explanation and steps for each project. For now, it’s a glimpse.

Potentiometer used

Here’s my circuit setup:

A peek at the code that gets run on the controller:

Core part of the forever loop to play and check the knob setting, and adjust tempo accordingly. User can interrupt the loop by entering “stop” in the serial monitor/console.

There are 2 arrays (but they’re indirectly associated) used…one contains the notes to play and another the duration for which the note should play in musical time. For 8 is in eighth time, 4 means quarter-time, 2 means half-time, and so on…this creates a true melody (this one plays “jingle bells” tune…notes for which I got from the internet).

The constants NOTE_xx are obviously defined as specific frequencies. For example, they’re in a separate header and they look like this:

The VOLTPIN is for reading voltage from the potentiomenter, and buzzerPIN is a digital pin used to write to the buzzer…i.e. to play a note. The line pauseBetweenNotes=dt; may seem unnecessary and in this case, it is because I could have just done delay(dt); instead of delay(pauseBetweenNotes); but there’s a reason behind it which I won’t elaborate much on other than to say, I had other formulas in use for pauseBetweenNotes such as pauseBetweenNotes = noteDuration * 1.30; to get a feel for a “normal” tempo.

Now you have seen 2 good uses of the potentiometer already. If you haven’t seen the “dimmer” project, take a look here.

Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top