A simple mockup project to familiarize yourself with the buzzer and the LCD screen.
Devices and components
Arduino Uno Rev3
10 connecting wires 150 mm male
16x2 LCD screen with I²C interface
Resistance 220 ohms
Piezoelectric buzzer
3mm LEDS (choose color)
10kOhm potentiometer
400 point solderless breadboard
Materials and tools
Computer
Software and tools
Arduino IDE
Project description
metronome.ino
1#include <LiquidCrystal.h>
2
3// Pin setup
4const int potPin = A0;
5const int buzzerPin = 8;
6const int ledPin = 13;
7
8// LCD setup: (rs, enable, d4, d5, d6, d7)
9LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
10
11int mappedPotValue = 0;
12int beatCount = 0; // To keep track of beats (for tick-tock effect)
13
14void setup() {
15 Serial.begin(9600);
16 pinMode(ledPin, OUTPUT); // Set LED pin as output
17
18 lcd.begin(16, 2); // Initialize the 2x16 LCD
19 lcd.print("Metronome BPM:");
20}
21
22void loop() {
23 // Read the potentiometer
24 int rawPotValue = analogRead(potPin);
25
26 // Map the potentiometer to BPM (Beats Per Minute)
27 mappedPotValue = map(rawPotValue, 0, 1023, 42, 208);
28
29 // Calculate the beat interval (milliseconds per beat)
30 int beatInterval = 60000 / mappedPotValue;
31
32 // Debugging output
33 Serial.print("Raw Value: ");
34 Serial.println(rawPotValue);
35 Serial.print("Mapped BPM: ");
36 Serial.println(mappedPotValue);
37 Serial.print("Beat Interval (ms): ");
38 Serial.println(beatInterval);
39
40 // Update BPM on LCD
41 lcd.setCursor(0, 1); // Second row
42 lcd.print("BPM: ");
43 lcd.print(mappedPotValue);
44 lcd.print(" "); // Clear leftover digits
45
46 // Flash the LED ON
47 digitalWrite(ledPin, HIGH);
48
49 // Decide between "Tick" and "Tock"
50 if (beatCount % 4 == 0) {
51 // Every 4th beat is a "Tock" (higher tone)
52 tone(buzzerPin, 1025, 20);
53 } else {
54 // Other beats are "Tick" (lower tone)
55 tone(buzzerPin, 775, 20);
56 }
57
58 delay(50); // Keep LED ON during the beep (50ms)
59
60 // Flash the LED OFF
61 digitalWrite(ledPin, LOW);
62
63 // Wait the rest of the beat time
64 delay(beatInterval - 50);
65
66 beatCount++; // Increase beat counter
67
68 // Optional: reset beatCount if it gets too big
69 if (beatCount > 1000) {
70 beatCount = 0;
71 }
72}
Downloadable files
Metronome Breadboard Diagram
circuit_image.png
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.