0
0
VhdlHow-ToBeginner · 4 min read

VHDL Code for LED Matrix Controller: Syntax and Example

A basic LED matrix controller in VHDL uses row and column scanning with multiplexing to light LEDs one row at a time quickly. The controller cycles through rows by enabling one row and setting column outputs to display the pattern. This is done using a clock-driven process and counters in VHDL.
📐

Syntax

The LED matrix controller uses a process triggered by a clock to scan rows and set columns. Key parts include:

  • Entity: Defines inputs (clock, reset) and outputs (row and column signals).
  • Architecture: Contains signals for row scanning and column data.
  • Process: Runs on clock edge to update the current row and output columns.
vhdl
entity LedMatrixController is
    Port (
        clk    : in  std_logic;
        reset  : in  std_logic;
        rows   : out std_logic_vector(3 downto 0);
        cols   : out std_logic_vector(3 downto 0)
    );
end LedMatrixController;

architecture Behavioral of LedMatrixController is
    signal current_row : integer range 0 to 3 := 0;
    type matrix_type is array (0 to 3) of std_logic_vector(3 downto 0);
    constant matrix_pattern : matrix_type := (
        "1001", -- row 0 pattern
        "0110", -- row 1 pattern
        "0110", -- row 2 pattern
        "1001"  -- row 3 pattern
    );
begin
    process(clk, reset)
    begin
        if reset = '1' then
            current_row <= 0;
        elsif rising_edge(clk) then
            if current_row = 3 then
                current_row <= 0;
            else
                current_row <= current_row + 1;
            end if;
        end if;
    end process;

    rows <= (others => '1');
    rows(current_row) <= '0';  -- Active low to select row
    cols <= matrix_pattern(current_row);
end Behavioral;
💻

Example

This example shows a 4x4 LED matrix controller that cycles through each row and lights LEDs according to a fixed pattern. The rows output activates one row at a time (active low), and cols sets which LEDs in that row are on.

The pattern forms a simple shape with LEDs lit at the edges.

vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity LedMatrixController is
    Port (
        clk    : in  std_logic;
        reset  : in  std_logic;
        rows   : out std_logic_vector(3 downto 0);
        cols   : out std_logic_vector(3 downto 0)
    );
end LedMatrixController;

architecture Behavioral of LedMatrixController is
    signal current_row : integer range 0 to 3 := 0;
    type matrix_type is array (0 to 3) of std_logic_vector(3 downto 0);
    constant matrix_pattern : matrix_type := (
        "1001", -- row 0 pattern
        "0110", -- row 1 pattern
        "0110", -- row 2 pattern
        "1001"  -- row 3 pattern
    );
begin
    process(clk, reset)
    begin
        if reset = '1' then
            current_row <= 0;
        elsif rising_edge(clk) then
            if current_row = 3 then
                current_row <= 0;
            else
                current_row <= current_row + 1;
            end if;
        end if;
    end process;

    rows <= (others => '1');
    rows(current_row) <= '0';  -- Active low to select row
    cols <= matrix_pattern(current_row);
end Behavioral;
Output
The controller cycles rows 0 to 3 repeatedly, activating one row at a time (active low) and lighting LEDs on columns as per the pattern: row 0 and 3 light LEDs at edges, rows 1 and 2 light LEDs in the middle.
⚠️

Common Pitfalls

Common mistakes when writing an LED matrix controller in VHDL include:

  • Not using active low or active high consistently for row selection, causing no LEDs to light.
  • Forgetting to reset the row counter, which can cause undefined behavior on startup.
  • Not cycling through rows fast enough, causing flickering or dim LEDs.
  • Incorrectly assigning column patterns, leading to wrong LEDs lighting.

Always verify the hardware wiring matches your active low/high logic.

vhdl
wrong: rows <= (others => '0'); -- all rows active low, no scanning
right: rows <= (others => '1'); rows(current_row) <= '0'; -- scan one row at a time
📊

Quick Reference

  • Rows: Usually active low signals to select one row at a time.
  • Columns: Set bits high or low to turn LEDs on/off in the selected row.
  • Clock: Drives the scanning process cycling through rows.
  • Reset: Initializes the row counter to zero.
  • Pattern Storage: Use arrays or constants to hold LED patterns per row.

Key Takeaways

Use a clock-driven process to scan rows one by one for multiplexing the LED matrix.
Rows are usually active low signals to select which row is lit at a time.
Store LED patterns in arrays and output columns accordingly for each row.
Always reset the row counter to avoid startup glitches.
Cycle through rows fast enough to avoid visible flicker.