This repository contains an MPLAB® X project that implements a Fast Fourier Transform (FFT) spectrum analyzer using the PIC16F13276 microcontroller. The audio input is provided through a cable jack and the frequency spectrum result is displayed on a 16x16 WS2812 LED matrix. The application translates the messages sent by the Serial Peripheral Interface (SPI) into ones intelligible by WS2812 using a Configurable Logic Block (CLB) peripheral.
More details and code examples on the PIC16F13276 can be found at the following links:
- PIC16F13276 Product Page
- PIC16F13276 Code Examples on Discover
- PIC16F13276 Code Examples on GitHub
- WS2818 Data Sheet
- MPLAB® X IDE v6.30 or newer or MPLAB® Tools for VS Code®
- MPLAB XC8 v3.10 or newer
- PIC16F1xxxx_DFP v1.29.444 or newer
-
The PIC16F13276 Curiosity Nano Development board below is used as a test platform:

To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board section.
The FFT is a method to decompose a signal into its frequency components. The spectrum frequency is computed using a 16-bit FFT implementation. FFT computes the frequency spectrum using 64 samples from the Analog-to-Digital Converter (ADC) as input and returns 32 complex numbers as output. To be correctly displayed, it is necessary to calculate the absolute values of the complex numbers from their real and imaginary components. The fix_fft library, ported to PIC® by Simon Inns, is used for the FFT implementation. More details about the fix_fft library are available in the Real-Time Audio Spectrum Analyser Project.
The final 32 values are grouped into 16 intervals corresponding to the 16 bars graph on the matrix. The amplitude values are mapped between 0 and 160. The peak value of 160 was determined experimentally by configuring the Universal Asynchronous Receiver-Transmitter (UART) peripheral to see the values obtained from several tests.
The audio signal from the cable jack is provided through the Curiosity Nano Explorer microphone amplifier (AMP_IN input), configured for the unity gain. The J206 jumper from the MIC CIRCUIT must be OFF when connecting to the audio signal.

The ADC converts the audio signal into digital samples with a sample rate controlled by Timer1 (TMR1). In this code example, the TMR1 period is configured to 25 μs (40 kHz), resulting in a 20-kHz maximum frequency that can be analyzed using the FFT algorithm.
The Frequency Spectrum is displayed on a 16x16 WS2812 LED matrix. The CLB can control and manipulate the transmitted data through the SPI and transform it into equivalent WS2812 data. The figure below shows the implemented solution.

More details about this implementation are provided in the SPI to WS2812 — Use Case for CLB Using the PIC16F13276 Microcontroller with MCC Melody.
The following peripheral and clock configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC16F13276:
-
Configurations Bits:
-
Clock Control:
-
CLB Synthesizer Library:
-
CLB1:
-
MSSP1 (SPI):
-
TMR1:
-
ADC:
-
CRC:
- Auto-configured by the CLB
-
Pin Grid View:
The following table shows the setup connections:
| Pin | Connection | Other |
|---|---|---|
| AMP IN | Jack output | On-board jumper OFF |
| RB2 | MIC OUT | On-board jumper ON |
| RA6 | SPI_to_WS2812 out | WS2812 input data in matrix |
The figure below shows the schematic of the assembly.

Note: The WS2812 matrix must be externally powered up due to higher power consumption.
This example demonstrates how to implement an FFT spectrum analyzer using the PIC16F13276 microcontroller. The 16-band frequency spectrum is displayed on a WS2812 LED matrix using CLB.
This chapter demonstrates how to use the MPLAB X IDE to program a PIC® device with an Example_Project.X. This is applicable to other projects, too.
-
Connect the board to the PC.
-
Open the
Example_Project.Xproject in MPLAB X IDE. -
Set the
Example_Project.Xproject as main project.
Right click the project in the Projects tab and click Set as Main Project.
-
Clean and build the
Example_Project.Xproject.
Right click theExample_Project.Xproject and select Clean and Build.
-
Select PICxxxxx Curiosity Nano under the Connected Hardware Tool section of the project settings:
Right click the project and click Properties.
Click the arrow under the Connected Hardware Tool.
Select PICxxxxx Curiosity Nano (click the SN), click Apply and then click OK:
-
Program the project to the board.
Right click the project and click Make and Program Device.















