Metronome

Stephen Gant

Overview

A metronome is a device that emits rhythmic beeps or clicks and is used to help a musician learn and maintain a steady tempo when practicing. The idea for this project came from my actual need of a metronome to help me learn a difficult piano song for my brother's wedding. There are many metronome smartphone apps, but none that I found had all the features that I wanted. I decided that instead of buying an actual metronome, programing one would be more fun!

Front view

The following is a high-level description of the device from a user's perspective. The metronome has an LCD screen for displaying the beats per minute (bpm) and other information. There are red and blue LEDs to blink with the beat, a buzzer to give audial beeps, and three buttons for control. Button 3 will turn on and off the device and cycle through five modes, and buttons 1 and 2 will change the state of the current mode. Supplying power will turn the device on. Holding button 3 for one second will turn it off, and then one click will turn it on again. If a power source was just applied, by default it will be in BPM mode and set at 60 bpm. The red LED will blink and a low beep will sound, here with 60 bpm, this will be once per second. Pressing button 3 will cycle through the five modes: BPM mode, Beat, Mute, Blue LED, and LEDs.

In BPM mode, button 1 will increment the bpm, and button 2 will decrement the bpm. Holding the buttons will add or subtract 10 from the bpm every second they are held. The current bpm is displayed on the LCD and there is a minimum of 20 bpm and a maximum of 250 bpm. In Beat mode, buttons 1 or 2 will switch between a 4/4 beat and a 3/4 beat. The beat only matters if the blue LED or the high beeps are enabled. The lower line on the LCD will display the beat. In Mute mode, buttons 1 or 2 will switch between mute on and off, and this will be displayed on the lower LCD line. In Blue LED mode, buttons 1 or 2 will switch between both the blue LED and the high beep being on and off. In LEDs mode, buttons 1 or 2 will switch between both LEDs being on and off.

Hardware

The microcontroller for this project is a Freescale MKL05Z32VFK4 from the Kinetis KL05 family. It has a 32-bit ARM Cortex-M0+ processor. The LCD is from Newhaven Display and it has 2 rows of 8 characters and a backlight. The device can be powered by two AAA batteries or a micro-USB cable. The LCD backlight will only illuminate when using USB power. A 32.768kHz crystal is used to help the processer keep more accurate time. The schematic layout and part of the assembled board are shown below.

Schematic Assembly

Software

The code for this project is written in C and was programed in the Keil μVision IDE. In the hardware.c file, the system clock, low-power mode, I/O ports, low-power timer and other timers are all configured. In the LCD.c file, the command, write, setup, and other functions are defined. In the main.c file, the functions and a number of global variables are first defined. There are functions for initializing the variables, changing the bpm and beat, turning on and off the beep, both LEDs, and the blue LED, and functions for creating the beep sound and handling timer interrupts. In the main function, after initialization, deep sleep is enabled and the main application loop is entered. The low power timer interrupt will periodically check the button states and then go back to sleep. Each button has a denouncing algorithm and can detect a single press or a hold for one second. If button 3 is held, the device is awakened from deep sleep and is "turned on". The code will wait for an interrupt every 20 msec, then run through the main application loop once and wait again. The button states are checked with every loop, and if pressed, the appropriate functions are called. The 20 msec intervals are used to implement the length of and the time between beeps and LED flashes. This does pose a problem with calculating the times between the blue LED flashes and high beep. While in 3/4 beat mode, if the number of runs through the main loop to get the correct time is not divisible by three, then there will be a timing problem. The same goes for the 4/4 beat. The simplest fix for this was to adjust the actual bpm so the number of loops would divide correctly. So the bpm is adjusted but this is not displayed on the LCD. It is raised by at most 2 or 3 bpm, and this only occurs when the blue LED or the high beeps are running. If just the red LED, then the actual bpm is correct. Holding button 3 will put the device back into deep sleep mode.

Final Thoughts

I am happy with the outcome of this project and will definitely be using it for practice. There were several more features I would have liked to add if I had more time. These included different volume levels instead of just on or muted, a few more beat options, and a tap mode where a button could be tapped to the beat of a song, and after a number of taps, the running bpm would be calculated and displayed. I may try to add these enhancements sometime in the future. There were a number of things I had to change from the initial design of the project, and I faced quite a few problems in the coding, but these were just opportunities to learn. I gained a lot from this course and it was really fun getting to come up with a project idea and then actually program it.

Downloads


Last modified:Mon, Jan 23, 2023.

00097