Projects / Pocket metronome

Pocket metronome

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO12.7 mASSD1306

    Test the OLED

    Run the Adafruit SSD1306 example first.

  2. NANO

    Wire the encoder

    CLK on D2, DT on D3, SW on D4.

  3. NANO12.7 mA

    Wire the LED and buzzer

    LED with 220 ohm on D5, buzzer on D6.

  4. NANO

    Upload the sketch

    The screen shows 100.

  5. NANO

    Turn the knob

    The number changes by one.

  6. NANO

    Count beats against a stopwatch

    Count 60 beats at 120 bpm.

  7. NANO

    Press the knob

    It stops and starts.

  8. NANO

    Fit it in a small box

    Leave the screen and knob accessible.

In short

How do you build a pocket metronome?

A rotary encoder sets the tempo from 40 to 200 bpm and a buzzer and LED mark every beat, with a louder click on the first beat of each bar. You need Arduino Nano, Rotary encoder module (KY-040), Active buzzer 5 V, LED 5 mm red, Resistor 220 Ω, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
KY-040 CLKNano D2Encoder channel A
KY-040 DTNano D3Encoder channel B
KY-040 SWNano D4Push button, starts and stops
KY-040 plus and GNDNano 5V and GNDPower
Active buzzer plus and minusNano D6 and GNDBeat click
Red LED plusNano D5 through a 220 ohm resistor, minus to GNDBeat light
OLED SDA and SCLNano A4 and A5Display, I2C address 0x3C
OLED VCC and GNDNano 5V and GNDPower (most modules accept 3.3 to 5 V)

Code

The Nano turns the tempo into a time between beats (60000 milliseconds divided by the bpm) and uses millis() to schedule each beat without blocking, so the encoder and display stay responsive. On each beat the LED and the buzzer switch on for a few milliseconds, with a longer click on the first beat of each four-beat bar. The KY-040 encoder is read on the falling edge of its CLK line: if DT differs from CLK the knob is turning one way, otherwise the other. Turning it changes the tempo by one bpm per click between 40 and 200, and pressing the knob starts or stops the beat. The OLED shows the current tempo in large digits. Use an active buzzer, which only needs a steady voltage to click. Changing nextBeat by a fixed period, instead of setting it from the current time, keeps the average tempo accurate even if one beat is slightly late. Setup: Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails). Install the Adafruit SSD1306 and Adafruit GFX libraries. If your screen has only 32 pixel rows, change the 64 to 32.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const uint8_t CLK = 2, DT = 3, SW = 4, LED = 5, BUZ = 6;
const uint8_t BEATS_PER_BAR = 4;

Adafruit_SSD1306 oled(128, 64, &Wire, -1);   // use 128, 32 for the small 0.91 inch screen

int bpm = 100;
bool running = true;
uint8_t beat = 0;
unsigned long nextBeat = 0, offAt = 0;
bool lastClk, lastSw = HIGH;

void draw() {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(4);
  oled.setCursor(0, 8);
  oled.print(bpm);
  oled.setTextSize(1);
  oled.setCursor(0, 52);
  oled.print(running ? "bpm  running" : "bpm  stopped");
  oled.display();
}

void setup() {
  pinMode(CLK, INPUT_PULLUP);
  pinMode(DT, INPUT_PULLUP);
  pinMode(SW, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  pinMode(BUZ, OUTPUT);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  lastClk = digitalRead(CLK);
  draw();
  nextBeat = millis();
}

void loop() {
  unsigned long now = millis();

  // Encoder: read on each falling edge of CLK
  bool clk = digitalRead(CLK);
  if (clk != lastClk && clk == LOW) {
    if (digitalRead(DT) != clk) bpm++; else bpm--;       // flip the two signs if it feels backwards
    bpm = constrain(bpm, 40, 200);
    draw();
  }
  lastClk = clk;

  // Encoder button: start and stop
  bool sw = digitalRead(SW);
  if (sw == LOW && lastSw == HIGH) {
    running = !running;
    beat = 0;
    nextBeat = now;
    draw();
    delay(30);
  }
  lastSw = sw;

  if (running && (long)(now - nextBeat) >= 0) {
    bool accent = (beat == 0);
    digitalWrite(LED, HIGH);
    digitalWrite(BUZ, HIGH);
    offAt = now + (accent ? 90 : 30);                    // longer click on beat one
    nextBeat += 60000UL / bpm;
    beat = (beat + 1) % BEATS_PER_BAR;
  }
  if (offAt && (long)(now - offAt) >= 0) {
    digitalWrite(LED, LOW);
    digitalWrite(BUZ, LOW);
    offAt = 0;
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power on100 bpm and clicking
Turn clockwiseNumber goes up
Count 60 beats at 120 bpmAbout 30 seconds
Press the knobStops, then restarts on the first beat
Turn past 200 or 40Number stops at the limit

If it does not work

Screen blank

Check the I2C address 0x3C and the SDA and SCL wires.

Number goes down when turning right

Swap the plus and minus on the DT line in the code.

Number jumps by several

A bouncy encoder, add 10 nF capacitors from CLK and DT to GND.

No click

Check the buzzer polarity and that it is an active type.

Tempo drifts

Check you did not add long delay() calls to the loop.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pocket metronome?

Arduino Nano, Rotary encoder module (KY-040), Active buzzer 5 V, LED 5 mm red, Resistor 220 Ω, SSD1306 OLED Display, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the pocket metronome take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the pocket metronome works?

Power on: 100 bpm and clicking. Turn clockwise: Number goes up. Count 60 beats at 120 bpm: About 30 seconds.

Screen blank: what should I check?

Check the I2C address 0x3C and the SDA and SCL wires.

Number goes down when turning right: what should I check?

Swap the plus and minus on the DT line in the code.

Number jumps by several: what should I check?

A bouncy encoder, add 10 nF capacitors from CLK and DT to GND.

No click: what should I check?

Check the buzzer polarity and that it is an active type.