A decoder is a combinational logic circuit that translates an n-bit binary input code into a single active signal across 2n possible output lines. In a real circuit, it changes a compact binary address into a discrete physical control signal, allowing a microcontroller with limited pins to individually select one of many memory chips, sensors, or displays. People most commonly confuse decoders with demultiplexers; while structurally similar, a demultiplexer routes an active data stream to one of many outputs, whereas a standard decoder simply asserts an output line based on the input address code.
Think of it like a hotel switchboard operator: you dial a 3-digit room number (the binary input), and the operator connects a single light in that specific room (the active output), leaving all other rooms dark.
The Core Mechanics: How an n-to-2^n Decoder Works
At the silicon level, a decoder is built from an array of AND gates (or NAND gates, for active-low outputs). The inputs act as address lines. For an n-to-2n decoder, every unique combination of HIGH and LOW voltages on the n inputs corresponds to exactly one output line.
The most ubiquitous workhorse IC in the hobbyist and professional bench is the 74HC138 (a 3-to-8 line decoder). It takes 3 input pins (A0, A1, A2) and breaks them out into 8 output pins (Y0 through Y7). According to the Texas Instruments SN74HC138 datasheet, this IC also features three enable pins (E1, E2, and E3). The decoder will only operate if E1 and E2 are LOW and E3 is HIGH; otherwise, all outputs default to HIGH. This enable functionality is what allows you to daisy-chain multiple decoders to expand your output count without adding more microcontroller pins.
Worked Numeric Example: Addressing SPI Flash Memory
Let's look at a concrete scenario: you are building a data logger with an ESP32 and need to interface with eight separate SPI flash memory chips. The ESP32 has limited GPIO pins, so you use a 74HC138 to generate the Chip Select (CS) lines for all eight chips using only three ESP32 pins.
- ESP32 GPIO 25 → 74HC138 A0
- ESP32 GPIO 26 → 74HC138 A1
- ESP32 GPIO 27 → 74HC138 A2
- Enable pins E1 and E2 tied to GND; E3 tied to 3.3V.
- Outputs Y0-Y7 connected to the CS pins of Flash Chips 0-7.
The 74HC138 features active-LOW outputs. This means the selected output drops to 0V, while the unselected outputs remain at 3.3V. SPI flash chips also use active-LOW Chip Select lines, making this a perfect match.
The Calculation:
Suppose your firmware needs to write data to Flash Chip #6.
1. The decimal number 6 translates to the binary code 110.
2. Mapping this to our inputs: A2 = 1 (HIGH), A1 = 1 (HIGH), A0 = 0 (LOW).
3. The ESP32 sets GPIO 27 HIGH, GPIO 26 HIGH, and GPIO 25 LOW.
4. Inside the 74HC138, the internal NAND gate for Y6 evaluates to true.
5. Result: Pin Y6 drops to 0V (LOW), activating Chip #6. Pins Y0-Y5 and Y7 remain at 3.3V (HIGH), keeping those chips deselected.
Because the 74HC series has a typical propagation delay of just 15ns at 5V, the chip select signal settles long before your 20MHz SPI clock begins toggling, preventing bus contention.
Where You Meet Decoders in Practice
While the 3-to-8 line decoder is common, decoders take several forms depending on the application:
- Memory Address Decoding: In retro computing (like the 6502-based Apple II or Commodore 64), decoders like the 74LS138 or 74LS154 translate upper memory address lines into chip selects for RAM, ROM, and I/O registers.
- BCD to 7-Segment Displays: A specialized decoder like the CD4511B takes a 4-bit Binary Coded Decimal (BCD) input (0000 to 1001) and drives the specific combination of segments (a through g) needed to display the numerals 0-9 on an LED display.
- LED Matrix Multiplexing: When driving an 8x8 LED matrix, you cannot wire 64 individual pins. Instead, you use a decoder to sink current through one column at a time while sourcing current to the active rows, relying on persistence of vision to create a stable image.
Decoder vs. Demultiplexer vs. Encoder
Because these three combinational circuits share similar internal logic gates, they are frequently mixed up. Here is how to tell them apart on a schematic or parts list:
| Component | Primary Function | Input / Output Ratio | Common IC Example |
|---|---|---|---|
| Decoder | Activates one specific output line based on a binary address code. | n inputs to 2n outputs | 74HC138 (3-to-8) |
| Demultiplexer | Routes a single active data signal to one of many outputs based on select lines. | 1 data + n select to 2n outputs | 74HC154 (with data in) |
| Encoder | Compresses multiple active input lines into a compact binary output code. | 2n inputs to n outputs | 74HC148 (8-to-3 priority) |
Frequently Asked Questions
What is the difference between an active-high and active-low decoder?
An active-high decoder (like the 74HC154) outputs a HIGH voltage (e.g., 5V or 3.3V) on the selected line, while all other lines remain LOW. An active-low decoder (like the 74HC138) outputs a LOW voltage (0V/GND) on the selected line, while all other lines remain HIGH. Active-low decoders are heavily favored in modern digital design because many memory and communication protocols (like SPI Chip Select or I2C interrupt lines) use active-low triggers, and NMOS transistors are historically more efficient at sinking current to ground than sourcing current from VCC.
Can I use a 74HC138 decoder to directly drive a 12V relay?
No. Standard 74HC logic ICs operate at 3.3V or 5V and can typically source or sink a maximum of 25mA per pin. A 12V relay coil usually requires 30mA to 70mA at 12V. Attempting to drive it directly will result in the relay failing to pull in, and the back-EMF spike when the coil collapses will instantly destroy the silicon inside the decoder. You must use the decoder's output to trigger a logic-level N-channel MOSFET (like a 2N7000) or a Darlington array IC (like the ULN2803) to switch the relay.
Why are my 74HC138 outputs stuck HIGH regardless of the inputs?
This is the most common beginner mistake when wiring a 74HC138: floating or incorrectly biased enable pins. The 74HC138 has three enable pins (E1, E2, and E3). For the decoder to function, E1 and E2 must be tied to GND (LOW), and E3 must be tied to VCC (HIGH). If you leave E1 or E2 unconnected, the high-impedance CMOS input will float, pick up ambient electrical noise, and randomly disable the chip, forcing all Y-outputs HIGH. Always tie unused logic inputs to a defined voltage rail.






