ATmega16 Digital Metronome with 4-Digit 7-Segment Display, Tap Tempo & Accent Beat

Build an ATmega16 digital metronome that shows the tempo (30–250 BPM) and the beat number on a 4-digit 7-segment display, clicks a piezo with an accented first beat, flashes two LEDs and supports tap tempo and 1–9 beats per bar.

  • DifficultyEasy
  • Build time2–3 hours
  • Board / MCUATmega16 (8 MHz internal)
  • Parts12 items
  • CodeEmbedded C (AVR-GCC)

Need it ready-made? Buy Ready Project

A metronome gives musicians a steady beat to practise with. This ATmega16 digital metronome shows the tempo in beats per minute (BPM) on a bright 4-digit 7-segment display, makes a sharp click on a piezo buzzer for every beat and gives the first beat of each bar a higher, accented click. Two LEDs flash with the beat, so you can also follow it by eye in a noisy room.

It is a great small project for learning ATmega16 timers: Timer0 creates an exact 1 ms tick that multiplexes the display and times the beats, and Timer1 makes the click tone in hardware on the OC1A pin. You can set the tempo from 30 to 250 BPM with UP and DOWN buttons, choose 1 to 9 beats per bar, or simply tap the TAP button a few times at the speed of a song. The settings are saved in EEPROM.

Connection diagram of the ATmega16 digital metronome with 4-digit 7-segment display, BC547 digit drivers, five push buttons, piezo buzzer and beat LEDs

How It Works

  • 1 ms system tick: Timer0 runs in CTC mode with a prescaler of 64 and OCR0 = 124, so with the 8 MHz internal clock it interrupts exactly 1000 times per second (8 000 000 / 64 / 125).
  • Display multiplexing: on each tick the interrupt switches off all four digits, puts the segment pattern of the next digit on PORTC and turns on that digit’s BC547 transistor. Each digit is lit for 1 ms out of every 4 ms (250 Hz), which looks completely steady to the eye.
  • Beat timing: the beat interval is 60000 / BPM milliseconds (500 ms at 120 BPM). The same interrupt counts milliseconds and fires a beat when the interval is reached. Because the counting happens inside the ISR, button handling in the main loop can never make the beat drift.
  • Click sound: Timer1 in CTC mode toggles the OC1A pin (PD5) in hardware. OCR1A = 249 gives 2000 Hz for the accented first beat and OCR1A = 499 gives 1000 Hz for the other beats. After 30 ms the interrupt disconnects OC1A, so you hear a short, clean click.
  • Beats per bar: the BEATS button cycles from 1 to 9. The first digit shows the current beat (1, 2, 3, 4, 1 …) while running. When stopped, it shows the beats-per-bar setting with its decimal point lit.
  • Tap tempo: press TAP at least twice in time with the music. The ATmega16 measures the gap between taps (240–2000 ms), averages the last few gaps and converts it to BPM.
  • Memory: 3 seconds after your last change, BPM and beats per bar are written to the internal EEPROM with eeprom_update_byte(), which only writes when the value really changed.

Components Required

  • ATmega16 microcontroller (40-pin DIP) – 1
  • USBasp programmer (10-pin to 6-pin adapter if needed) – 1
  • 4-digit 7-segment display, common cathode, 0.56 inch (e.g. 5641AS) – 1
  • BC547 NPN transistors (digit drivers) – 4
  • 1 kΩ resistors (transistor bases) – 4
  • 220 Ω resistors (8 segments + 2 LEDs) – 10
  • Passive piezo buzzer + 100 Ω series resistor – 1
  • 5 mm LEDs, red and green – 1 each
  • 12 mm tactile push buttons (UP, DOWN, START/STOP, BEATS, TAP) – 5
  • 10 kΩ resistor (RESET pull-up) and 100 nF capacitors – 1 and 2
  • 5 V supply: USB 5 V or 9 V battery with LM7805 – 1
  • Breadboard or perfboard and jumper wires – 1 set

Wiring Table

Part / pinATmega16 pinNotes
+5 VVCC (10), AVCC (30)100 nF from each to GND
GNDGND (11, 31)Common ground for everything
RESETPin 910 kΩ to +5 V (also to USBasp RESET)
Segment a, b, c, dPC0, PC1, PC2, PC3 (22–25)Each through a 220 Ω resistor
Segment e, f, g, dpPC4, PC5, PC6, PC7 (26–29)Each through a 220 Ω resistor
Digit 1 (beat) cathodeBC547 collector, base 1 kΩ to PB0 (1)Emitter to GND
Digit 2 (hundreds) cathodeBC547 collector, base 1 kΩ to PB1 (2)Emitter to GND
Digit 3 (tens) cathodeBC547 collector, base 1 kΩ to PB2 (3)Emitter to GND
Digit 4 (units) cathodeBC547 collector, base 1 kΩ to PB3 (4)Emitter to GND
UP / DOWN buttonsPA0 (40) / PA1 (39)Other leg to GND, internal pull-ups
START/STOP / BEATS / TAPPA2 (38) / PA3 (37) / PA4 (36)Other leg to GND, internal pull-ups
Passive piezo (+)PD5 / OC1A (19)Through 100 Ω; piezo (–) to GND
Red LED (accent)PD6 (20)220 Ω, cathode to GND
Green LED (beat)PD7 (21)220 Ω, cathode to GND
USBasp MOSI / MISO / SCKPB5 (6) / PB6 (7) / PB7 (8)Left free for programming

Pin check: PC2–PC5 are also the JTAG pins (TCK, TMS, TDO, TDI). A new ATmega16 has JTAG enabled, so you must program the high fuse to 0xD9 to free them; the code also writes the JTD bit twice at start-up. PD5 is the OC1A pin, so Timer1 can make the tone without any CPU work. Use a passive piezo; an active buzzer only gives one fixed tone.

Connection Diagram

The diagram above shows how the display, transistors, buttons and sound parts connect to the ATmega16. The drawing below lists every pin exactly as used in the code.

ATmega16 Digital Metronome – Pin-Accurate Wiring ATmega16 PA0 (40) UP button → GND PA1 (39) DOWN button → GND PA2 (38) START / STOP button → GND PA3 (37) BEATS button → GND (1–9 beats per bar) PA4 (36) TAP tempo button → GND PB0–PB3 (1–4) 1 kΩ → BC547 base; collector → digit 1–4 cathode; emitter → GND PC0–PC6 (22–28) 220 Ω → segments a, b, c, d, e, f, g PC7 (29) 220 Ω → segment dp (decimal point) PD5 (19) OC1A → 100 Ω → piezo (+), piezo (–) → GND PD6 (20) 220 Ω → red LED (accent, beat 1) → GND PD7 (21) 220 Ω → green LED (every beat) → GND RESET (9) 10 kΩ → 5 V and USBasp RESET VCC 10 / AVCC 30 +5 V, 100 nF to GND at each pin GND 11 / 31 Common GND Program hfuse 0xD9 (JTAG off) first, otherwise segments c, d, e and f on PC2–PC5 stay dark or dim. Display: digit 1 = beat number (dot lit when stopped = beats-per-bar setting), digits 2–4 = BPM.
Pin-accurate connection diagram for the ATmega16 digital metronome.

Code

The program is written in plain AVR C for AVR-GCC (Microchip Studio, or avr-gcc on Linux / WinAVR on Windows). It was compile-checked for the ATmega16: about 1.7 KB of flash and 30 bytes of RAM.

/*
 * ATmega16 Digital Metronome with 4-Digit 7-Segment Display
 * Arduino Project Hub - https://ardiunoprojecthub.online
 *
 * MCU    : ATmega16, internal 8 MHz RC oscillator
 * Fuses  : lfuse 0xE4 (int. 8 MHz), hfuse 0xD9 (JTAG off - PORTC is free)
 * Build  : avr-gcc -mmcu=atmega16 -DF_CPU=8000000UL -Os -o metronome.elf atmega16_metronome.c
 *
 * Display: 4-digit common-cathode 7-segment
 *          segments a..g, dp -> PC0..PC7 (through 220 ohm resistors)
 *          digit 1..4 cathodes -> BC547 NPN transistors driven from PB0..PB3 (1k base)
 *          digit 1 = beat number (running) or beats per bar (stopped)
 *          digits 2-4 = tempo in BPM (30..250)
 * Buttons: PA0 UP, PA1 DOWN, PA2 START/STOP, PA3 BEATS (1..9), PA4 TAP (to GND, pull-ups on)
 * Sound  : passive piezo buzzer on PD5 (OC1A) - high click on beat 1, low click on others
 * LEDs   : PD6 red = accent (beat 1), PD7 green = every beat (220 ohm resistors)
 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdint.h>

#define BPM_MIN     30
#define BPM_MAX     250
#define CLICK_MS    30          /* length of each click                 */
#define TONE_HIGH   249         /* OCR1A for 2000 Hz (accent)           */
#define TONE_LOW    499         /* OCR1A for 1000 Hz (normal beat)      */
#define LED_MS      80          /* how long the beat LEDs stay on       */

/* Segment patterns for 0-9, bit0 = a ... bit6 = g (common cathode) */
static const uint8_t SEG[10] = {
    0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
};
#define SEG_BLANK 0x00
#define SEG_DP    0x80

/* Settings saved in EEPROM */
uint8_t EEMEM ee_bpm   = 120;
uint8_t EEMEM ee_beats = 4;

/* Shared with the 1 ms timer interrupt */
static volatile uint8_t  disp[4];          /* raw segment bytes per digit */
static volatile uint32_t ms_count = 0;     /* milliseconds since start    */
static volatile uint16_t beat_interval;    /* ms between beats            */
static volatile uint16_t beat_timer = 0;   /* ms since last beat          */
static volatile uint8_t  running = 0;
static volatile uint8_t  beat_no = 0;      /* 1..beats while running      */
static volatile uint8_t  beats_per_bar;
static volatile uint8_t  click_left = 0;   /* ms of click remaining       */
static volatile uint8_t  led_left = 0;     /* ms of LED flash remaining   */
static volatile uint8_t  refresh = 0;      /* ask main loop to redraw     */

static uint8_t bpm;

/* ---------- sound and beat (called from the ISR) ---------- */
static void start_click(uint8_t accent)
{
    OCR1A  = accent ? TONE_HIGH : TONE_LOW;
    TCNT1  = 0;
    TCCR1A = (1 << COM1A0);                /* toggle OC1A on compare match */
    click_left = CLICK_MS;

    PORTD |= (1 << PD7);                   /* green LED on every beat      */
    if (accent) PORTD |= (1 << PD6);       /* red LED on beat 1            */
    led_left = LED_MS;
}

static void do_beat(void)
{
    beat_no++;
    if (beat_no > beats_per_bar) beat_no = 1;
    /* with 1 beat per bar every beat is a normal click (no accent) */
    start_click(beat_no == 1 && beats_per_bar > 1);
    refresh = 1;
}

/* ---------- Timer0 compare: runs every 1 ms ---------- */
ISR(TIMER0_COMP_vect)
{
    static uint8_t digit = 0;

    /* 1. multiplex one digit per interrupt (250 Hz per digit, no flicker) */
    PORTB &= 0xF0;                         /* all digits off               */
    PORTC  = disp[digit];                  /* load segments                */
    PORTB |= (1 << digit);                 /* switch on this digit         */
    if (++digit > 3) digit = 0;

    /* 2. time keeping */
    ms_count++;

    /* 3. beat generator - timed inside the ISR so it never drifts */
    if (running) {
        if (++beat_timer >= beat_interval) {
            beat_timer = 0;
            do_beat();
        }
    }

    /* 4. end the click and the LED flash */
    if (click_left && --click_left == 0) {
        TCCR1A = 0;                        /* disconnect OC1A              */
        PORTD &= ~(1 << PD5);              /* leave piezo pin low          */
    }
    if (led_left && --led_left == 0) {
        PORTD &= ~((1 << PD6) | (1 << PD7));
    }
}

static uint32_t millis(void)
{
    uint32_t m;
    cli(); m = ms_count; sei();
    return m;
}

/* ---------- display ---------- */
static void show(void)
{
    uint8_t d[4];
    uint8_t lead = running ? beat_no : beats_per_bar;

    d[0] = SEG[lead % 10];
    if (!running) d[0] |= SEG_DP;          /* dot = stopped, setting mode  */
    d[1] = (bpm >= 100) ? SEG[bpm / 100] : SEG_BLANK;
    d[2] = SEG[(bpm / 10) % 10];
    d[3] = SEG[bpm % 10];

    for (uint8_t i = 0; i < 4; i++) disp[i] = d[i];   /* byte writes are atomic */
}

static void set_bpm(int16_t v)
{
    if (v < BPM_MIN) v = BPM_MIN;
    if (v > BPM_MAX) v = BPM_MAX;
    bpm = (uint8_t)v;
    uint16_t iv = 60000U / bpm;            /* 120 BPM -> 500 ms            */
    cli(); beat_interval = iv; sei();
}

static void save_settings(void)
{
    eeprom_update_byte(&ee_bpm, bpm);      /* only writes if value changed */
    eeprom_update_byte(&ee_beats, beats_per_bar);
}

/* ---------- buttons ---------- */
#define BTN_UP    0
#define BTN_DOWN  1
#define BTN_START 2
#define BTN_BEATS 3
#define BTN_TAP   4

static uint8_t pressed(uint8_t b) { return !(PINA & (1 << b)); }

int main(void)
{
    /* turn JTAG off in software too (needs two writes within 4 cycles) */
    MCUCSR |= (1 << JTD);
    MCUCSR |= (1 << JTD);

    DDRC  = 0xFF; PORTC = 0x00;            /* segments                     */
    DDRB |= 0x0F; PORTB &= 0xF0;           /* digit transistors            */
    DDRD |= (1 << PD5) | (1 << PD6) | (1 << PD7);
    DDRA &= ~0x1F; PORTA |= 0x1F;          /* buttons with pull-ups        */

    /* load saved settings (first power-up: EEPROM is 0xFF) */
    uint8_t b = eeprom_read_byte(&ee_bpm);
    uint8_t n = eeprom_read_byte(&ee_beats);
    if (b < BPM_MIN || b > BPM_MAX) b = 120;
    if (n < 1 || n > 9) n = 4;
    beats_per_bar = n;
    set_bpm(b);

    /* Timer0: CTC, 8 MHz / 64 / 125 = 1 kHz interrupt */
    TCCR0 = (1 << WGM01) | (1 << CS01) | (1 << CS00);
    OCR0  = 124;
    TIMSK |= (1 << OCIE0);

    /* Timer1: CTC (top OCR1A), prescaler 8 -> 1 MHz; f = 1 MHz / (2*(OCR1A+1)) */
    TCCR1A = 0;
    TCCR1B = (1 << WGM12) | (1 << CS11);

    sei();
    show();

    uint8_t  last = 0x1F;                  /* previous raw button state    */
    uint32_t t_press = 0, t_repeat = 0, t_change = 0;
    uint32_t last_tap = 0, tap_sum = 0;
    uint8_t  tap_count = 0, dirty = 0;

    while (1) {
        uint32_t now = millis();
        uint8_t  cur = PINA & 0x1F;

        if (cur != last) {                 /* a button changed state       */
            _delay_ms(20);                 /* simple debounce              */
            cur = PINA & 0x1F;
            uint8_t down = last & ~cur;    /* bits that just went LOW      */
            last = cur;
            now = millis();

            if (down & (1 << BTN_UP))   { set_bpm(bpm + 1); t_press = now; t_repeat = now; dirty = 1; }
            if (down & (1 << BTN_DOWN)) { set_bpm(bpm - 1); t_press = now; t_repeat = now; dirty = 1; }

            if (down & (1 << BTN_START)) {
                cli();
                running = !running;
                beat_no = 0;
                beat_timer = 0;
                sei();
                if (running) { cli(); do_beat(); sei(); }   /* first click at once */
            }

            if (down & (1 << BTN_BEATS)) {
                cli();
                if (++beats_per_bar > 9) beats_per_bar = 1;
                beat_no = 0;               /* restart the bar on beat 1    */
                sei();
                dirty = 1;
            }

            if (down & (1 << BTN_TAP)) {
                uint32_t gap = now - last_tap;
                last_tap = now;
                if (gap >= 240 && gap <= 2000) {   /* 250..30 BPM range   */
                    tap_sum += gap;
                    tap_count++;
                    set_bpm((int16_t)(60000UL * tap_count / tap_sum));
                    if (tap_count >= 4) { tap_sum = tap_sum / tap_count * 3; tap_count = 3; }
                    dirty = 1;
                } else {                   /* first tap or too slow: start over */
                    tap_sum = 0; tap_count = 0;
                }
            }
            t_change = now;
            show();
        }

        /* auto-repeat: hold UP/DOWN 500 ms, then 10 steps per second */
        if ((pressed(BTN_UP) || pressed(BTN_DOWN)) &&
            now - t_press > 500 && now - t_repeat > 100) {
            set_bpm(pressed(BTN_UP) ? bpm + 1 : bpm - 1);
            t_repeat = now;
            dirty = 1;
            t_change = now;
            show();
        }

        if (refresh) { refresh = 0; show(); }

        /* save to EEPROM 3 s after the last change (saves EEPROM wear) */
        if (dirty && now - t_change > 3000) {
            save_settings();
            dirty = 0;
        }
    }
}

Compile, Set Fuses and Upload

Connect the USBasp to MOSI, MISO, SCK, RESET, VCC and GND, then run these commands. The fuse values select the internal 8 MHz oscillator (lfuse 0xE4) and turn JTAG off (hfuse 0xD9).

avr-gcc -mmcu=atmega16 -DF_CPU=8000000UL -Os -o metronome.elf atmega16_metronome.c
avr-objcopy -O ihex -R .eeprom metronome.elf metronome.hex
avrdude -c usbasp -p m16 -U lfuse:w:0xE4:m -U hfuse:w:0xD9:m
avrdude -c usbasp -p m16 -U flash:w:metronome.hex:i

Using the Metronome

  • Set the tempo: press UP or DOWN to change the BPM by 1. Hold the button for half a second to change it quickly (10 steps per second).
  • Start and stop: press START/STOP. The first click comes at once, and the first digit counts the beats of the bar.
  • Change the bar: press BEATS to choose 2 for marches, 3 for waltzes, 4 for most pop songs, or 1 for no accent at all.
  • Tap tempo: tap TAP four or five times along with a song and the display jumps to its tempo.
  • Power off: your last tempo and bar setting come back the next time you switch on.

Troubleshooting

ProblemFix
Segments c, d, e or f never lightJTAG is still enabled. Program hfuse to 0xD9 (JTAGEN unprogrammed) and power-cycle the chip.
Tempo is a little too fast or slowThe internal RC oscillator is about ±3 % out of the box. Write a calibration byte to OSCCAL, or use an 8 MHz crystal with lfuse 0xFF.
Display looks dim or ghost segments appearCheck that each digit uses its own BC547 and that the segment resistors are 220 Ω (not 1 kΩ). A display with common anode needs PNP drivers and inverted code.
No sound from the piezoIt must be a passive piezo on PD5 (OC1A). An active buzzer, or one wired to another pin, will not work with the hardware tone.
Buttons change the value twiceUse proper tactile switches; the code debounces for 20 ms. Increase the _delay_ms(20) value to 40 for worn switches.
Avrdude says the device signature is 0x000000Check the USBasp wiring and that the chip has 5 V. On a factory-fresh chip set the USBasp to slow SCK (jumper JP3) for the first fuse write.
Shows 120 and 4 every time after power-upThe settings save 3 seconds after the last button press. Wait a moment before switching off.

Ideas to Extend

  • Add a rotary encoder for faster tempo setting.
  • Drive a small 8 Ω speaker through a transistor for a louder click.
  • Add a 3.5 mm headphone jack with a volume pot for quiet practice.

Leave a Comment