Digital Melody Player using ATmega8 with Proteus Simulation

Play a melody on an ATmega8: Timer1 CTC arithmetic, a note table, speaker wiring, a complete C program, the 8 MHz fuse trap and troubleshooting.

Author avatar
By Ibrar Ayyub
Published April 17, 2026
At a glance
CategoryCircuits
DifficultyIntermediate
IncludesCode
UpdatedDecember 2, 2026
Reading timeabout 3 min
On this page

Summary of Digital Melody Player using ATmega8 with Proteus Simulation


This article describes a compact digital melody player built using an ATmega8 microcontroller and simulated in Proteus. The system generates musical tones via a speaker connected to the PB0 pin, utilizing BASCOM-AVR firmware to define note frequencies and durations. It plays predefined melodies like "Final Countdown" in a continuous loop, serving as an educational tool for embedded systems, timing, and audio generation.

Parts used in the Digital Melody Player:

  • ATmega8 AVR microcontroller
  • Speaker (LS1)
  • Power supply connections
  • Proteus simulation environment
  • BASCOM-AVR compiler

An ATmega8 can play a tune with almost no parts: one timer toggles a pin at the note frequency and a table in flash holds the melody. This guide explains the timer arithmetic, gives a complete C program, and shows how to build and test the circuit.

IN SHORT
  • Timer1 in CTC mode toggles the OC1A pin (PB1) at the note frequency.
  • The tune is two arrays in flash: note frequencies and lengths.
  • Output frequency = F_CPU / (2 x prescaler x (OCR1A + 1)).
  • Connect a small speaker or piezo through a resistor, never a large speaker straight to the pin.
Block diagram: note table, Timer1 compare, OC1A pin PB1, speaker
From table entry to sound

How a note is made

In CTC mode Timer1 counts up to the value in OCR1A and starts again. With the output compare unit set to toggle OC1A on each match, the pin changes state every time the counter wraps, so one full cycle takes two wraps. At an 8 MHz clock with a prescaler of 8, the counting rate is 1 MHz, so for a frequency f:

OCR1A = F_CPU / (2 * 8 * f) - 1

A4 (440 Hz):  8,000,000 / (2 * 8 * 440) - 1 = 1136

The result must fit in 16 bits, which it does for notes above about 8 Hz at this prescaler. Lower frequencies need a larger prescaler.

Note Frequency (Hz) OCR1A at 8 MHz, prescaler 8
C4 262 1907
D4 294 1700
E4 330 1514
F4 349 1432
G4 392 1275
A4 440 1135

The circuit

Part Connection
ATmega8 Powered from 5 V with a 100 nF capacitor; internal 8 MHz oscillator
Speaker or piezo PB1 (pin 15 on the 28-pin DIP) through a 100 to 330 ohm resistor to ground
ISP header MOSI, MISO, SCK, RESET, VCC, GND for programming

Set the fuses for the internal 8 MHz oscillator. The ATmega8’s internal oscillator is selectable between 1, 2, 4 and 8 MHz; the default is 1 MHz, so the melody plays eight times too slowly until you change it.

The program

// projects-raspberry.com
// Play a short melody on ATmega8 at 8 MHz: Timer1 in CTC mode toggles OC1A (PB1) at the note frequency
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdint.h>

// frequencies in Hz, 0 is a rest
static const uint16_t notes[] PROGMEM = {262, 262, 392, 392, 440, 440, 392, 0, 349, 349, 330, 330, 294, 294, 262};
static const uint8_t beats[] PROGMEM  = {  1,   1,   1,   1,   1,   1,   2, 1,   1,   1,   1,   1,   1,   1,   2};

static void tone(uint16_t hz, uint16_t ms)
{
    if (hz == 0) {
        TCCR1A = 0;
        TCCR1B = 0;
    } else {
        // clock / 8 prescaler, toggle on compare match: output frequency = F_CPU / (2 * 8 * (OCR1A + 1))
        OCR1A = (uint16_t)(F_CPU / (2UL * 8UL * hz)) - 1;
        TCNT1 = 0;
        TCCR1A = (1 << COM1A0);
        TCCR1B = (1 << WGM12) | (1 << CS11);
    }
    while (ms--) _delay_ms(1);
    TCCR1A = 0;
    TCCR1B = 0;
    PORTB &= ~(1 << PB1);
}

int main(void)
{
    DDRB |= (1 << PB1);                              // speaker on PB1 (OC1A)
    for (;;) {
        for (uint8_t i = 0; i < sizeof(notes) / sizeof(notes[0]); i++) {
            tone(pgm_read_word(&notes[i]), 300UL * pgm_read_byte(&beats[i]));
            _delay_ms(40);
        }
        _delay_ms(1500);
    }
}

The note and length tables are stored in flash with PROGMEM so they do not use the 1 kB of SRAM, and read back with pgm_read_word. A frequency of 0 is a rest.

avr-gcc -Os -mmcu=atmega8 -o melody.elf melody_atmega8.c
avr-objcopy -O ihex melody.elf melody.hex
avrdude -c usbasp -p m8 -U flash:w:melody.hex:i

Write your own tune

  1. Look up the frequencies of the notes you need.
  2. Add them to notes[] and the lengths, in beats, to beats[].
  3. Adjust the 300 ms in the call to tone() to speed the tune up or down.

If it sounds wrong

What you see What to check
Plays very slowly and low The chip is still on its 1 MHz default clock; set the fuses for 8 MHz.
No sound Speaker wiring on PB1; the program sets DDRB bit 1.
Clicks only Not enough drive; use a piezo or a transistor stage.
Wrong pitch F_CPU does not match the real clock.

Quick answers

Can I use BASCOM instead of C?
Yes, BASCOM has a SOUND command; this C version shows what the hardware does.
Can I play two notes at once?
Not with one timer output. Use a second timer or mix in software.
Can I use an Arduino?
Yes, with the tone() function, which uses a timer in the same way.

Downloads and references

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to Digital Melody Player:

  • How does the project generate musical tones?
    The controller uses timed pulse signals on the PB0 output pin with specific period values to create different note frequencies.
  • What determines the pitch of the notes played?
    The pitch or frequency is determined by the Periods value defined in the Sound instruction within the firmware.
  • Can this circuit be simulated in Proteus?
    Yes, the project is designed to be simulated in Proteus with minimal components including the ATmega8 and a speaker.
  • What programming language is used for the firmware?
    The code is written using BASCOM-AVR to configure the microcontroller and generate sound commands.
  • Does the melody play only once or repeatedly?
    The program runs inside a Do ... Loop structure, causing the melody list to repeat endlessly during simulation.
  • Which specific melodies are included in the code?
    The hardcoded sequences include tunes such as Final Countdown, Rhythm of the Night, Oxygene 10, and the Titanic Theme.
  • How are pauses created between notes?
    The firmware uses Waitms and Wait instructions to insert musical spacing between notes and songs.
  • Is complex hardware required for this project?
    No, the hardware setup is very simple, requiring only a speaker connected to the PB0 output pin.
Scroll to Top