How to Charlieplex LEDs

Last modified by Microchip on 2025/01/02 09:42

Overview

Charlieplexing is a technique that multiplexes LEDs with tri-state to save on I/O pins. For N pins used, charlieplexing allows for (N2 - N) LEDs to be controlled. In this example, three pins drive six LEDs in the following arrangement.

Charlieplexed LEDs Schematic

Figure 1

Since the three LEDs have three states (HIGH, LOW, High-Z [tri-state]), each pin has two outputs – an Output Enable (OE) and the digital output. This creates a truth table for each LED as such:

Output EnableDigital OutputI/O State
0XHigh-Z
100
111

For this example, the LEDs turn on according to the number the microcontroller sets in the CLBSWINx Register (CLB Software Input):

CLBWIN2CLBWIN1CLBWIN01Active LED
000None
001LED1
010LED2
011LED3
100LED4
101LED5
110LED6
111None

Here’s how the schematic looks in MPLAB® Code Configurator (MCC) Melody Configurable Logic Block (CLB)  tool:

MPLAB Melody CLB Charlieplexed LED Schematic

Figure 2

Requirements

Procedure

Implement the CLB Logic

The clock source used was HFINTOSC, but other sources like LFINTOSC, shouldn’t cause issues.

Back to Top


Configure the Look-Up Tables (LUTs)

Click on each LUT and look at properties.

At the bottom of the table, there is a text field for entering a hexadecimal number corresponding to the truth table.

  • PPS_OE0 = 001E
  • PPS_OE1 = 0078
  • PPS_OE2 = 0066
  • PPS_OUT0 = 0012
  • PPS_OUT1 = 0048
  • PPS_OUT2 = 0024

Back to Top


Assign the pins to the desired locations.

Back to Top


Generate MCC code.

Back to Top


Set up the main function as follows:

int main(void)
{
 SYSTEM_Initialize();
 while(1)
  {
   NOP()
   NOP()
   NOP();
   CLB1_SWIN_Write16(0);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(1);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(2);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(3);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(4);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(5);
   NOP(); __delay_ms(250u);
   CLB1_SWIN_Write16(6);
   NOP(); __delay_ms(250u);
  }
}

Back to Top


Build the circuit on a breadboard. Refer to Figure 1 for guidance.

Back to Top


Verify the design.

Back to Top

Results

In this screenshot from a logic analyzer, the seven states can be seen clearly. The top three traces represent the digital value the pin is in, while the three traces below are the analog values of those signals. Each marker pair (1, 2, 3, etc…) represents which LED is enabled at that time. The LEDs are off before and after the markers show as the I/O is tri-stated.

Video 1 demonstrates charlieplexing in action, showcasing the LEDs cycling through the states as observed in the logic analyzer.

Information

Note: Tri-state does not have a defined digital value and can be high or low.

Charlieplexing LEDs Output

Figure 3

Charlieplexing LEDs Setup

Video 1: Charlieplexing LEDs Setup

Back to Top

Learn More

Back to Top