Top Logo Sp Logo
Arduino Metronome Project

Arduino Metronome Project

Arduino Metronome Project

Metronome Project

In this project, we will use the Arduino Uno to read the time between button presses and flash an LED and generate a tone on a buzzer that can be used as a metronome. We will have a button, LED, and buzzer.

Materials

 

Hookup Instructions

  1. Connect a jumper wire from the 5V pin on the Arduino to one of the + rails on the breadboard.

  1. Connect a jumper wire from the GND pin on the Arduino to the - rail next to the + rail you chose on the breadboard.

  1. Place your button on the breadboard straddling the gap between the two sides.

  1. Place small jumper wires going from the + rail with the 5V connected to one pin of the button.

  1. Place 10 kohm resistor from the - rail with the GND pin connected to the other pin on the same side of the gap as the 5V is connected to.

  1. Place jumper wires from digital pin 3 on the Arduino to the same pin that the 10 kohm resistor is connected to, preferable in a sensible order. (The 5 points on the breadboard going across in the same row are all connected. So on row 15, points 15a, 15b, 15c, 15d, and 15e are all connected.)

  1. Place your LED on the breadboard with the legs in different rows (the numbered rows).

  1. Place a 470 ohm resistor with one leg connected to the long leg of the resistor an the other connected to a different row on the breadboard.

  1. Place a jumper between the - rail that is ground and the short leg of the LED.

  1. Place a jumper between pin 4 and the leg of the resistor that isn’t connected to anything yet.

  1. Place your buzzer (piezo speaker) on the breadboard with the legs in different rows.

  1. Place a jumper between the black leg and the - rail that is ground on the breadboard.

 

  1. Place a jumper between pin 5 and the red leg of the buzzer.

Programming Instructions

Overview: There are many ways to go about programming a metronome such as this one. In this one, we will look for the button press, determine if it’s the first button press in a sequence or the second, save the times of each press, and calculate the elapsed time. We will then flash the LED and play a tone on the buzzer each time that elapsed time occurs.

Variables

 We need several variables in the program to keep track of various things. Create these as global variables at the beginning of the sketch.

 

  1. Create a TRUE/FALSE variable to keep track of if the button has been pressed already.
  2. Create an integer value to save the first time to.
  3. Create an integer value to save the second time to.
  4. Create an integer value to save the elapsed time to.
  5. Create a variable for each digital pin we will use (so 3) with names that make sense to you for the buzzer, LED, and button. Make the buzzer variable equal to 5, the LED variable equal to 4, and the button variable equal to 3 to use the wiring in this walk through.
  6. Initialize all integers as 0.

Setup

  • Make the digital pins used for the LED and buzzer outputs.
  • Make the digital pin used for the button an input.
  • Initialize the serial port at 9600 baud.

Loop

Start by checking if the button has been pressed, it’s the first time in a sequence, and the first time is equal to 0.

  • If all this is true, save the current execution time to your first time variable (see millis() at arduino.cc/references.
  • Turn your TRUE/FALSE variable on to show the button has been pressed.

Else if (hint hint), check if the button has been pressed and your TRUE/FALSE variable is on, meaning this is the second press in a sequence.

  • If all of this is true, save the current execution time to your second time variable and turn your TRUE/FALSE variable off.

Else if (hint hint), check if the button has been pressed and both your time variables have a value.

  • If these conditions are true, set both your time variables to 0.
  • This means every third press will reset the times and allow the next two presses to set a new tempo of the light and buzzer.

Else (hint hint) do nothing.

Next, make sure you don’t read the same button press more than once! The Arduino scans thousands of times per second, so even if you just hit the button for as quick as you can, it could pick this up several times. To eliminate this, find a way to freeze the program while the button is still high, then continue on through it after it’s released. (See while() at arduino.cc/reference)

 Next, if both times are not equal to 0, and your first time is less than your second time (trying to use a negative time in things like delay() will break the program), do the following:

  1. Calculate your elapsed time.
  2. Turn on the LED
  3.  Play a tone to the buzzer of your frequency choice (I used 1000 Hz).
  4. Delay 100 milliseconds.
  5. Turn the LED off.
  6. Turn the buzzer off.
  7. Delay for the elapsed time - 100 milliseconds (this way your temp doesn’t increase by 100 milliseconds for each cycle).
Then, at the end of each run through, print the elapsed time with the explanation of your choice.
Shop the story