This metronome project uses a piezo buzzer and LED lights to play a 4/4 rhythm with an adjustable bpm via a potentiometer.
Devices and components
Modulino™ button
16x2 LCD screen with I²C interface
Modulino™ Ringtone
Breadboard - 830 contacts
40 colorful male-male connection wires
Set of 70 5mm LEDs in assorted colors
Arduino Uno Rev3
Software and tools
Arduino IDE
Project description
Connect 5V to the positive rail and GND to the negative rail of the Arduino Uno
LED pins wired in parallel to pins 9 to 12 and connected to the ground rail. The orange LED has a resistance of 339 Ohm and each blue LED has resistors of 257 Ohm
Piezo buzzer wired to pin 13 and ground rail with 100 Ohm resistance
Potentiometer 1 (to adjust the bpm) is wired from the wiper to analog pin A0, terminal 2 to the positive rail, and terminal 1 to the ground rail. The resistance of the potentiometer in this simulator is 250 KOhms
LCD Wiring: GND to ground rail, VCC to positive rail, VO to potentiometer 2, RS to pin 2, RW to ground rail, E to pin 4, DB4 to pin 5, DB5, to pin 6, DB6 to pin 7, DB7 to pin 8, LED (left) to positive rail via 160 Ohm resistor, LED (right) to rail earth.
Potentiometer 2 (to adjust the contrast of the LCD screen): Wiper wired to the LCD screen in VO, terminal 2 to the positive rail, terminal 1 to the ground rail. Potentiometer resistance is set at 10 KOhms
Metronome Arduino Uno R3 Code
1//IMPORTANT NOTE: This code only reflects the Tinkercad simulator setup. If using the Arduino Modulino components (buzzer and knob) listed in the components section of this project page, code for those pieces would look slightly different. Adjust as needed
2
3#include <LiquidCrystal.h>
4
5//pin variables
6const int SPEAKER = 13;
7const int YELLOW_LED = 12;
8const int BLUE_LED1 = 11;
9const int BLUE_LED2 = 10;
10const int BLUE_LED3 = 9;
11const int DIAL = A0;
12//lcd pin variables
13const int RS = 2;
14const int E = 4;
15const int DB4 = 5;
16const int DB5 = 6;
17const int DB6 = 7;
18const int DB7 = 8;
19
20//instantiate lcd object
21LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7);
22
23//for counting beats in the time signature
24int beatCount = 1;
25
26void setup()
27{
28 //set input/output pins
29 pinMode(SPEAKER, OUTPUT);
30 pinMode(YELLOW_LED, OUTPUT);
31 pinMode(BLUE_LED1, OUTPUT);
32 pinMode(BLUE_LED2, OUTPUT);
33 pinMode(BLUE_LED3, OUTPUT);
34 pinMode(DIAL, INPUT);
35
36 //declare rows and columns of lcd
37 lcd.begin(16, 2);
38}
39
40void loop()
41{
42 //map the dial's value to the beats per minute range of 50 to 300
43 int bpm = map(analogRead(DIAL),0, 1023, 50, 300);
44
45 //lcd print
46 lcd.setCursor(0,0);
47 lcd.print("Time Sig: 4/4");
48 lcd.setCursor(0, 1);
49 lcd.print("BPM: ");
50 lcd.setCursor(5, 1);
51 lcd.print(bpm);
52 //clear third digit on display if bpm is two digits
53 if(bpm < 100) {
54 lcd.setCursor(7, 1);
55 lcd.print(" ");
56 }
57
58 //set time of each individual beat based on bpm
59 //60000 = 1 minute
60 int beatInterval = 60000 / bpm;
61
62 //in 4/4 time signature there are four beats
63 //first beat of each 4 beats will be a different tone
64 //and different color light to indicate start of new measure
65 if(beatCount == 1) {
66 //higher note at start of measure
67 //tone plays for half the length of the beat to leave a pause
68 tone(SPEAKER, 1025, beatInterval / 2);
69 //turn on and off appropraite lights
70 digitalWrite(YELLOW_LED, HIGH);
71 digitalWrite(BLUE_LED1, LOW);
72 digitalWrite(BLUE_LED2, LOW);
73 digitalWrite(BLUE_LED3, LOW);
74 //keep respective light on for whole beat
75 delay(beatInterval);
76 } else {
77 //lower tone for the rest of the measure
78 tone(SPEAKER, 775, beatInterval / 2);
79 //tone is the same for beats 2-4
80 //but will have different lights on
81 if(beatCount == 2) {
82 digitalWrite(BLUE_LED1, HIGH);
83 digitalWrite(YELLOW_LED, LOW);
84 digitalWrite(BLUE_LED2, LOW);
85 digitalWrite(BLUE_LED3, LOW);
86 delay(beatInterval);
87 } else if(beatCount == 3) {
88 digitalWrite(BLUE_LED2, HIGH);
89 digitalWrite(YELLOW_LED, LOW);
90 digitalWrite(BLUE_LED1, LOW);
91 digitalWrite(BLUE_LED3, LOW);
92 delay(beatInterval);
93 } else {
94 digitalWrite(BLUE_LED3, HIGH);
95 digitalWrite(YELLOW_LED, LOW);
96 digitalWrite(BLUE_LED1, LOW);
97 digitalWrite(BLUE_LED2, LOW);
98 delay(beatInterval);
99 }
100 }
101
102 //incriment beat count or reset beat count after 4
103 if(beatCount < 4)
104 beatCount++;
105 else
106 beatCount = 1;
107
108}
Note: Content and images are from: https://projecthub.arduino.cc/, with some modifications.
If you want it removed due to copyright reasons, please leave a comment. Thank you.
I want to share this article more widely so that everyone knows about Arduino and your project.