Thilan Dissanayaka Pet Projects May 06

Tic-Tac-Toe Game with Atmega 256 MicroController

In this blog, I’ll walk you through how I built a Tic-Tac-Toe game using an AVR microcontroller, a 4x3 keypad, and a 3x3 grid of LEDs.

This project is a fun way to combine embedded programming, game logic, and hardware interaction into a complete system that brings a classic game to life using simple electronics.

Project Overview

The objective of this project was to create an interactive Tic-Tac-Toe game where:

Players enter their moves using a 4x3 keypad.

The current game state is visualized using a 3x3 LED grid, with each cell lighting up red or green depending on the player's turn.

The game identifies the winner and plays a simple LED animation to celebrate the victory.

Hardware Components

Here’s what I used for the setup:

  • ATmega microcontroller (running at 8 MHz)

  • 4x3 Keypad for user input

  • 3x3 LED grid for game display

  • Resistors, wires, and breadboard for wiring

  • Power supply (USB/5V battery or programmer)

Software Workflow

Let’s break down the development process step-by-step:

Step 1: Setting Up the Keypad

The keypad serves as the input device where each button represents a cell (1–9) on the Tic-Tac-Toe board. I used PORTK for rows and PINK for columns on the ATmega.

void initKeypad() {
    DDRK = 0x0F;    // Lower nibble (rows) as output, upper nibble (columns) as input
    PORTK |= 0xF0;  // Enable internal pull-ups for the inputs
}

The keypad() function handles:

Polling each row/column to detect key presses

Debouncing to eliminate accidental multiple presses

Mapping key values to grid positions (1–9)

Step 2: Initializing the LED Grid The LED grid is controlled through multiple ports. Each cell corresponds to one LED, and different pins on PORTA, PORTC, PORTD, and PORTG are used.

void initGrid() {
    DDRA = 0xFF;
    DDRC = 0xFF;
    DDRD = 0xFF;
    DDRG = 0xFF;
    // Set all grid control ports as output
}

Each LED is controlled individually based on the game state using a helper function like controlLed(row, col, color, state).

♻️ Step 3: Implementing Game Logic The game logic is responsible for:

Keeping track of the player turn (RED or GREEN)

Preventing overwriting already marked cells

Updating the LED grid

void play(int index) {
    int row = (index - 1) / 3;
    int col = (index - 1) % 3;

    if (grid[row][col] == 0) {          // Check if the cell is empty
        grid[row][col] = turn;          // Mark the cell
        controlLed(row, col, turn, ON); // Light up the LED
        turn = (turn == RED) ? GREEN : RED; // Switch turns
    }
}

🏁 Step 4: Checking for a Winner After every move, we check rows, columns, and diagonals to see if a player has won.

int checkWinner() {
    for (int i = 0; i < 3; i++) {
        // Check rows
        if (grid[i][0] != 0 &&
            grid[i][0] == grid[i][1] &&
            grid[i][1] == grid[i][2])
            return grid[i][0];

        // Check columns
        if (grid[0][i] != 0 &&
            grid[0][i] == grid[1][i] &&
            grid[1][i] == grid[2][i])
            return grid[0][i];
    }

    // Check diagonals
    if (grid[0][0] != 0 &&
        grid[0][0] == grid[1][1] &&
        grid[1][1] == grid[2][2])
        return grid[0][0];

    if (grid[0][2] != 0 &&
        grid[0][2] == grid[1][1] &&
        grid[1][1] == grid[2][0])
        return grid[0][2];

    return 0; // No winner
}

🎉 Step 5: Winning Animation When a player wins, a simple animation flashes the LEDs in the winning row/column/diagonal.

void animation(int winner) {
    for (int i = 0; i < 3; i++) {
        controlLed(0, i, winner, ON);
        _delay_ms(300);
        controlLed(0, i, winner, OFF);
        _delay_ms(300);
    }
}
ALSO READ
How does SLL work?
May 17 Cryptography

Every time you see that small padlock icon in your browser's address bar, you're witnessing one of the internet's most important security technologies at work. This tiny symbol represents....

Proxy Pattern explained simply
Apr 26 Software Architecture

Sometimes you don't want or can't allow direct access to an object. Maybe it's expensive to create, needs special permissions, or you want to control access in some way. This is where the **Proxy....

Exploiting a Stack Buffer Overflow on Windows
May 17 Exploit development

In a previous tutorial we discusses how we can exploit a buffer overflow vulnerability on a Linux machine. I wen through all theories in depth and explained each step. Now today we are going to....

Build A Simple Web shell
Mar 23 Application Security

A web shell is a type of code that hackers use to gain control over a web server. It is particularly useful for post-exploitation attacks, and there are various types of web shells available. Some of....

GraphQL - Interview preparation guide
Oct 01 Interview Guides

## What is GraphQL? GraphQL is a query language for APIs and a runtime for executing those queries. It allows clients to request exactly the data they need, reducing over-fetching and....

Kafka - Interview preparation guide
Jan 28 Interview Guides

## What is Apache Kafka? Apache Kafka is a distributed event streaming platform designed for high-throughput, fault-tolerant, and real-time data streaming. It is used for building real-time data....