ATmega16 Digital Dice with 7-Segment

Build a small ATmega16 digital dice with a 7-segment display and one roll button. Includes the wiring table, connection diagram, AVR-GCC code and fuse settings.

  • DifficultyEasy
  • Build time1–2 hours
  • Board / MCUATmega16
  • Parts10 items
  • CodeAVR C

Need it ready-made? Buy Ready Project

ATmega16 digital dice on a breadboard with a single 7-segment display, roll button and resistors

An ATmega16 digital dice is a small, satisfyingfirst project on this 40-pin AVR chip. One push button rolls the die, a single common-cathode 7-segment display flashes through the faces, and when you let go a number from 1 to 6 stays lit. There is no Arduino board in this build: you program the bare ATmega16 (or an ATmega16 development board) in C with avr-gcc or Microchip Studio.

This tutorial gives you the parts list, a pin-to-pin wiring table, a connection diagram, fuse settings for the internal 8 MHz oscillator, and fully commented AVR-GCC code. The circuit uses only PORTA for the display and PD2 for the button, so it is easy to build on a breadboard in one sitting.

How the ATmega16 Digital Dice Works

The seven segments of the display (a through g) are driven by PORTA pins PA0 to PA6. Because the display is common cathode, a logic HIGH on a port pin lights that segment through a 220 Ω resistor. The code stores a bit pattern for each dice face 1 to 6 and writes it to PORTA whenever the face changes.

The ROLL button is wired from PD2 to ground. The pin’s internal pull-up holds it HIGH, so a press reads as LOW. While you hold the button the firmware cycles the display through 1, 2, 3, 4, 5 and 6 about twenty-five times a second, which looks like a rolling die. Timer0 runs freely in the background. When you release the button the firmware mixes the timer count with how long you held the button and maps the result to a face from 1 to 6, so two rolls in a row almost never freeze on the same number by accident.

The ATmega16 runs from its internal 8 MHz RC oscillator, so you do not need an external crystal for this project. You do need to program the low fuse to 0xE4 (internal 8 MHz, CKDIV8 disabled). The factory default is internal 1 MHz, which would make every delay four times too long.

Components Required

  • ATmega16 microcontroller (DIP-40) – 1, or an ATmega16 development board
  • Common-cathode 7-segment display (single digit) – 1
  • Tactile push button – 1 (ROLL)
  • 220 Ω resistors – 7 (one for each segment a–g)
  • 10 kΩ resistor – 1 (RESET pull-up)
  • 100 nF ceramic capacitor – 1 (VCC decoupling)
  • 5 V regulated supply (USB-to-5 V module or a bench supply)
  • Breadboard and jumper wires
  • USBASP, USBtinyISP, or an Arduino Uno used as ISP, plus a 6-pin or 10-pin ISP cable
  • Optional: 16 MHz crystal and 2 × 22 pF capacitors if you prefer an external clock instead of the internal oscillator

Wiring Table (Pin-to-Pin)

ModuleModule PinATmega16 PinNotes
Power+5 VPin 10 (VCC) and pin 30 (AVCC)Always tie AVCC to VCC, even if you are not using the ADC
PowerGNDPin 11 and pin 31 (GND)Connect both ground pins
Decoupling cap100 nFVCC to GNDPlace it close to pins 10 and 11
RESET10 kΩ to +5 VPin 9 (RESET)Keeps the chip out of reset. Do not leave RESET floating
7-segment (CC)Segment aPin 40 (PA0)Through a 220 Ω resistor
7-segment (CC)Segment bPin 39 (PA1)Through a 220 Ω resistor
7-segment (CC)Segment cPin 38 (PA2)Through a 220 Ω resistor
7-segment (CC)Segment dPin 37 (PA3)Through a 220 Ω resistor
7-segment (CC)Segment ePin 36 (PA4)Through a 220 Ω resistor
7-segment (CC)Segment fPin 35 (PA5)Through a 220 Ω resistor
7-segment (CC)Segment gPin 34 (PA6)Through a 220 Ω resistor
7-segment (CC)Common cathodeGNDAll commons on a single-digit display go to ground
ROLL buttonLeg 1Pin 16 (PD2 / INT0)Internal pull-up is enabled in code
ROLL buttonLeg 2GNDPressed = LOW
ISP programmerMISO / MOSI / SCK / RESET / VCC / GNDPin 7 / pin 6 / pin 8 / pin 9 / pin 10 / pin 11PB6, PB5, PB7, RESET. Unplug the programmer after flashing if the wires crowd the breadboard

Use a common-cathode display. A common-anode part will look dark or show inverted garbage because this code drives segments HIGH to turn them on. If your development board already has a reset pull-up and a decoupling capacitor, you do not need to add those two parts again.

Connection Diagram

The wiring diagram below matches the table. Follow the table if any artistic label on the drawing looks crowded: the pin numbers in the table are the ones to trust.

ATmega16 Digital Dice Wiring Wiring diagram: ATmega16 PA0-PA6 drive 7-segment segments a-g through 220 ohm resistors, PD2 to the ROLL button, RESET 10k pull-up, 100nF on VCC. ATmega16 Digital Dice Wiring ATmega16 DIP-40 VCC pin 10 → +5V AVCC pin 30 → +5V GND pins 11 & 31 → GND RESET pin 9 → 10kΩ → +5V PA0 pin 40 → 220Ω → segment a PA1 pin 39 → 220Ω → segment b PA2 pin 38 → 220Ω → segment c PA3 pin 37 → 220Ω → segment d PA4 pin 36 → 220Ω → segment e PA5 pin 35 → 220Ω → segment f PA6 pin 34 → 220Ω → segment g PD2 pin 16 → ROLL button → GND 100nF capacitor between VCC and GND 7-SEGMENT (common cathode) a b c d e f g Common cathode → GND ROLL button PD2 (pin 16) to GND Supply: +5V on VCC and AVCC. Internal 8 MHz oscillator (low fuse 0xE4). No crystal required.
Connection diagram for the ATmega16 digital dice. Match each labelled pin to the wiring table above.

Fuse Settings

Program these fuses once before or after you flash the hex file. The high fuse value 0xD9 disables JTAG so PORTC is free if you later expand the project; SPI programming stays enabled, so you will not lock yourself out.

  • Low fuse = 0xE4 — internal 8 MHz RC oscillator, CKDIV8 disabled, brown-out detector off
  • High fuse = 0xD9 — JTAG disabled, SPIEN enabled

With a USBASP the commands are:

avrdude -c usbasp -p m16 -U lfuse:w:0xE4:m -U hfuse:w:0xD9:m
avrdude -c usbasp -p m16 -U flash:w:atmega16_digital_dice.hex

Complete AVR-GCC Code

Save the file as atmega16_digital_dice.c. Define F_CPU as 8 MHz so _delay_ms() matches the fuse settings above.

/*
 * ATmega16 Digital Dice with a single 7-segment display
 *
 * Press and hold the ROLL button (PD2, active LOW). The display
 * cycles quickly through 1-6 so it looks like a rolling die.
 * When you release the button, a number from 1 to 6 stays on
 * the display. Randomness comes from Timer0 (free-running) mixed
 * with how long the button was held.
 *
 * Hardware assumptions
 * --------------------
 * MCU:        ATmega16, DIP-40
 * Clock:      internal 8 MHz RC oscillator (CKDIV8 disabled)
 * Display:    common-cathode 7-segment, segments a-g on PORTA
 * Button:     PD2 to GND, internal pull-up enabled
 *
 * Fuse bytes (AVRDUDE)
 * --------------------
 * Low  fuse  = 0xE4   // internal 8 MHz, CKDIV8 off, BOD off
 * High fuse  = 0xD9   // JTAG disabled, SPI programming enabled
 *
 *   avrdude -c usbasp -p m16 -U lfuse:w:0xE4:m -U hfuse:w:0xD9:m
 *   avrdude -c usbasp -p m16 -U flash:w:atmega16_digital_dice.hex
 *
 * Build (avr-gcc)
 * ---------------
 *   avr-gcc -mmcu=atmega16 -DF_CPU=8000000UL -Os -o dice.elf atmega16_digital_dice.c
 *   avr-objcopy -O ihex dice.elf atmega16_digital_dice.hex
 *
 * Written for Arduino Project Hub / this tutorial. Public domain.
 */

#define F_CPU 8000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>

/* Segment pins: PA0=a, PA1=b, PA2=c, PA3=d, PA4=e, PA5=f, PA6=g.
 * Common cathode: a 1-bit turns that segment ON.
 * Patterns are for digits 1 to 6 (index 0 is unused).
 */
static const uint8_t DICE_FACE[7] = {
    0x00,       /* 0: blank, never shown as a dice face */
    0b0000110,  /* 1: b + c                         = 0x06 */
    0b1011011,  /* 2: a + b + d + e + g             = 0x5B */
    0b1001111,  /* 3: a + b + c + d + g             = 0x4F */
    0b1100110,  /* 4: b + c + f + g                 = 0x66 */
    0b1101101,  /* 5: a + c + d + f + g             = 0x6D */
    0b1111101   /* 6: a + c + d + e + f + g         = 0x7D */
};

#define ROLL_PIN   PD2
#define DEBOUNCE_MS 20

static void display_init(void)
{
    /* PA0-PA6 are outputs that drive the seven segments. PA7 unused. */
    DDRA  = 0x7F;
    PORTA = 0x00;

    /* PD2 is an input with the internal pull-up so the pin idles HIGH.
     * The roll button shorts PD2 to GND when pressed.
     */
    DDRD  &= ~(1 << ROLL_PIN);
    PORTD |=  (1 << ROLL_PIN);
}

static void show_face(uint8_t value)
{
    if (value >= 1 && value <= 6) {
        PORTA = DICE_FACE[value];
    } else {
        PORTA = 0x00;
    }
}

static uint8_t button_is_pressed(void)
{
    /* Active LOW because of the pull-up. */
    return (PIND & (1 << ROLL_PIN)) == 0;
}

static void timer0_start(void)
{
    /* Timer0 in normal mode, no prescaler, free-running 0..255.
     * We never enable its interrupt; we only read TCNT0 as a
     * fast-changing number to seed the roll.
     */
    TCCR0 = (1 << CS00);
    TCNT0 = 0;
}

/* Mix the hold time with the free-running timer so two rolls
 * in a row almost never land on the same face by accident.
 */
static uint8_t roll_face(uint16_t hold_loops)
{
    uint8_t raw = (uint8_t)(TCNT0 ^ (hold_loops & 0xFF) ^ (hold_loops >> 4));
    return (uint8_t)((raw % 6) + 1);
}

int main(void)
{
    uint8_t  face = 1;
    uint16_t hold_loops;

    display_init();
    timer0_start();
    show_face(face);

    while (1) {
        if (button_is_pressed()) {
            _delay_ms(DEBOUNCE_MS);
            if (!button_is_pressed()) {
                continue;
            }

            /* Animate 1-6 while the button stays down. */
            hold_loops = 0;
            while (button_is_pressed()) {
                hold_loops++;
                show_face((uint8_t)((hold_loops % 6) + 1));
                _delay_ms(40);
            }

            _delay_ms(DEBOUNCE_MS);
            face = roll_face(hold_loops);
            show_face(face);
        }
    }
}

Build it with:

avr-gcc -mmcu=atmega16 -DF_CPU=8000000UL -Os -o dice.elf atmega16_digital_dice.c
avr-objcopy -O ihex dice.elf atmega16_digital_dice.hex

Code Explanation

  • DICE_FACE[] holds the seven-segment patterns for 1 to 6. Bit 0 is segment a (PA0) and bit 6 is segment g (PA6). A 1 means that segment is ON.
  • display_init() sets PA0–PA6 as outputs and enables the pull-up on PD2 so the roll pin idles HIGH.
  • timer0_start() runs Timer0 with no prescaler. The overflow interrupt is never enabled; the code only reads TCNT0 as a fast-changing number.
  • roll_face() XOR-mixes TCNT0 with the button-hold counter and reduces the result modulo 6, then adds 1 so the face is in the range 1–6.
  • The main loop waits for a press, debounces it, animates the faces while the button stays down, debounces the release, then shows the final face.

Step-by-Step Build Instructions

  1. Place the ATmega16 across the centre of the breadboard so the two rows of pins sit on opposite sides. Pin 1 is marked with a notch and a dot.
  2. Connect pin 10 (VCC) and pin 30 (AVCC) to +5 V. Connect pin 11 and pin 31 to ground. Fit the 100 nF capacitor between VCC and GND near the chip.
  3. Fit the 10 kΩ resistor from pin 9 (RESET) to +5 V.
  4. Wire PA0–PA6 (pins 40 down to 34) through seven 220 Ω resistors to segments a–g of the common-cathode display. Connect the display common to ground.
  5. Connect the roll button between pin 16 (PD2) and ground.
  6. Hook up your USBASP (or Arduino-as-ISP) to MOSI, MISO, SCK, RESET, VCC and GND.
  7. Compile the source to a hex file, write the fuses (0xE4 / 0xD9), then flash the hex file.
  8. Remove the programmer if you want a tidier breadboard, apply 5 V, and press ROLL. The display should rattle through 1–6 and freeze on a face when you release the button.

Troubleshooting

ProblemFix
Display stays darkConfirm the part is common cathode, check the 220 Ω resistors, and measure 5 V on VCC and AVCC. A common-anode display will not light with this code.
Wrong or inverted digitsYour segment-to-pin order may differ from a–g on PA0–PA6. Swap those seven wires to match the table, or edit DICE_FACE[].
Button does nothingCheck PD2 (pin 16) to the button and the other leg to GND. The code uses the internal pull-up, so you should measure about 5 V on PD2 when the button is not pressed.
Animation is very slowThe chip is still running at the factory 1 MHz clock. Write low fuse 0xE4 so it runs at 8 MHz.
avrdude cannot talk to the chipCheck MOSI/MISO/SCK/RESET wiring, share ground with the programmer, and make sure RESET is not stuck low. Try -B 8 to slow the ISP clock.
Chip gets warmRemove power at once and look for a reversed display common or a segment wired straight to VCC without a resistor.

Ideas to Extend the Project

  • Add a second 7-segment digit and show two dice at once, using two PORTA/PORTC nibbles or a 74HC595 shift register.
  • Use the ADC on an unused pin as extra randomness by reading a floating analog pin inside roll_face().
  • Drive a small piezo from PB3 (OC0) and play a short beep when the roll lands.
  • Replace the 7-segment display with seven LEDs arranged as a real die face (pips 1 through 6).

Frequently Asked Questions

Can I use an ATmega32 instead of an ATmega16?

Yes. The DIP-40 pinout is the same for the ports used here. Compile with -mmcu=atmega32 and tell avrdude -p m32. Check the ATmega32 fuse map before you write fuses; do not copy the ATmega16 values blindly.

Can I use a common-anode 7-segment display?

Yes, if you invert the patterns (write PORTA = DICE_FACE[value] ^ 0x7F) and connect the common anode to +5 V instead of ground. The 220 Ω resistors stay in series with each segment.

Do I need an external crystal?

No. The internal 8 MHz oscillator is accurate enough for a dice animation and button debounce. Add a 16 MHz crystal on XTAL1/XTAL2 only if you later add time-critical serial communication and want a more precise clock.

Will this compile in Arduino IDE?

Not as an official Arduino board. ATmega16 is not a standard Arduino core target. Use avr-gcc, Microchip Studio, or add a third-party ATmega16 core. The source itself is plain AVR C and does not use the Arduino API.

Can I power it from a 9 V battery?

Not directly. The ATmega16 is a 5 V part in this circuit (it can run from 4.5–5.5 V at 8 MHz). Use a 7805 or another 5 V regulator if your supply is higher than 5.5 V.

Related Projects

Browse more ATmega16 on Arduino Project Hub.

1 thought on “ATmega16 Digital Dice with 7-Segment”

Leave a Comment