MPLAB® Harmony v2 USB Audio Speaker Tutorial


MPLAB® Harmony is a flexible firmware development platform for PIC32 microcontrollers. This integrated software framework provides driver libraries and APIs that make it easier for you to use PIC32 peripherals (Timers, GPIO, UART, I2C, SPI, etc.). It also includes middleware libraries for USB (Host and Device), TCP/IP (Ethernet and Wi-Fi®), Graphics, Bluetooth®, and others.

This tutorial shows you how to use the MPLAB Harmony Configurator (MHC) to create a USB speaker application running on the PIC32 Multimedia Expansion Board II (MEB II). You will also learn how to use the MPLAB Harmony Graphics Composer (MHGC) to create a touch-controlled display to control the USB speaker.

Two ways to use this tutorial:

  1. Create the project from scratch
    • Use the provided source files and step-by-step instructions below.
  2. Use the solution project as an example
    • Just build the solution project and download it to the MEB II Board to observe the expected behavior.

Lab Objectives:

  1. Create an MPLAB X IDE Harmony project for a PIC32 microcontroller from scratch.
  2. Use the MPLAB Harmony Configurator (MHC) to configure and generate Harmony code.
  3. Use MHC to configure the audio CODEC on the PIC32 Multimedia Expansion Board II.
  4. Create new application states and variables for Harmony projects.
  5. Identify the proper place to add your own application code to the Harmony project.
  6. Demonstrate the use of Harmony peripheral libraries and drivers to control PIC32 peripherals (I2S, I2C, Timers etc.).
  7. Demonstrate the use of Harmony Framework libraries to use the USB middleware and Graphics Library.


Hardware Tools

Tool About Purchase
In-Circuit Emulator
PIC32MZ Embedded Connectivity with FPU
Starter Kit
PIC32 Multimedia Expansion Board II (v5)
For PIC32 Starter Kits

It is not necessary to have an MPLAB Real ICE to program/debug the code. The PIC32MZ EF Starter Kit includes a PICkit on-board (PKOB) debugger. The PKOB debugger is connected to the host PC through the USB Mini-B connector on the PIC32MZ EF Starter Kit for programming/debugging (see the red cable in the Hardware Setup).

Apart from the hardware tools listed above, the following items are required to play audio from a PC over the USB interface:

  • Wired Stereo headphone.
  • USB Type-A male to micro-B male cable for USB audio speaker functionality.
  • USB Type-A male to mini-B male cable for programming/debugging.

Software Tools

This project has been verified to work with the following versions of software tools:
MPLAB X IDE v3.26, MPLAB XC32 Compiler v1.40, MPLAB Harmony v1.08.

Note: Because we regularly update our tools, occasionally you may discover an issue while using newer versions. If you suspect that to be the case, we recommend that you double-check using the same versions that the project was tested with.

Archived versions of our tools can be found on the following Microchip websites:
MPLAB Harmony (see "Archived Downloads" tab)
MPLAB X IDE and XC32 Compiler (see "Downloads Archive" tab)
Note that multiple versions of all these tools can co-exist on the same computer.

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C/C++ Compiler
MPLAB® Harmony 1.xx
Integrated Software Framework


This lab enables the MEB II Board to act as a USB speaker by playing audio data (sampling rate: 48 kHz, 16-bit data), streamed by the USB host (typically a PC). This is done by adding a CODEC driver and the USB audio device library to the project.

Figure 1.1 APP_Tasks()

Figure 1.1 shows the application tasks. The APP_USB_AUDIO_Tasks function handles USB audio application related states and events. The APP_DISPLAY_Tasks function manages volume changes and mute/un-mute requests from the GUI.

Click image to enlarge.
Click image to enlarge.

Figure 1.2 APP_USB_AUDIO_Tasks()

The USB audio state machine (as shown in Figure 1.2) first opens the USB Device driver and registers a Device event handler to receive Device layer USB events. The state machine then opens the audio CODEC and registers a buffer event handler. It then waits for the USB host to configure the Device. After the Device is configured, the state machine enters an idle loop waiting for USB host events.

When the user opens an audio player application on the PC (USB host), the device receives (alternate) interface settings through the USB audio driver event handler. If the device is capable of servicing the new settings, the state machine then requests the host to send a stream of audio data. Once the device receives the audio stream, it enters the process data state where it submits the received audio data to the CODEC driver for playing, and requests more data from the USB host. The cycle then repeats.

Click image to enlarge.
Click image to enlarge.

Lab Source Files and Solutions:

This .zip file contains the completed solution project for this lab. It also contains the source files needed to perform the labs per the following step-by-step instructions (see the "Procedure" section on this page).

The contents of this .zip file need to be placed in this folder:
<Harmony install path>/apps/training/middleware/
(example Harmony install path = c:/microchip/harmony/v1_08)

If this is not done, MPLAB X IDE will not be able to find all source files for the lab solution (see the project in the firmware folder) and the solution project will not build successfully.

Extracting the .zip file will create the following folders:

  • usb_speaker contains the lab folder usb_speaker_lab.
  • usb_speaker_lab contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
  • dev_files contains application source files and other support files (if any) required to perform the lab (see "Procedure" section below).
  • firmware contains the completed lab solution project. It can be directly built and downloaded on the hardware to observe expected behavior.


All steps must be completed before you will be ready to build, download, and run the application.

Lab Index

Step 1: Create project and configure the PIC32MZ

  • Step 1.1 – Create an MPLAB Harmony project in the MPLAB X IDE
  • Step 1.2 – With MHC, Select the Board Support Package (BSP)
  • Step 1.3 – With MHC, Verify Configuration Bits are correct
  • Step 1.4 – With MHC, Verify Oscillator Settings
  • Step 1.5 – With MHC, Verify I/O pins using the Graphical Pin Manager

Step 2: Configure Audio CODEC, I2C driver and I2S driver

  • Step 2.1 - Configure CODEC AK4953
  • Step 2.2 - Configure I2C driver for CODEC
  • Step 2.3 - Verify/Set the I/O Pins used by I2C module using the Graphical Pin Manager
  • Step 2.4 - Configure I2S driver for CODEC
  • Step 2.5 - Configure External Master Clock Input (MCKI) to the CODEC
  • Step 2.6 - Configure/Verify DMA system service for allocating I2S transmit DMA channel
  • Step 2.7 - Verify/Set the I/O Pins used by I2S module using the Graphical Pin Manager

Step 3: Configure USB Library for Audio Device operation
Step 4: Design Display GUI, and Configure the Touch and I2C Driver

  • Step 4.1 - Rename the Screen Name
  • Step 4.2 - Import Images and Fonts (Assets)
  • Step 4.3 - Create Schemes (assign standard colors and fonts)
  • Step 4.4 - Add Primitive Objects (rectangle, image, text)
  • Step 4.5 - Add Widget Objects (list-box, buttons, slider)
  • Step 4.6 - Configure the Graphics display and Graphics Touch Driver
  • Step 4.7 - Enable Touchscreen Control using I2C
  • Step 4.8 - Enable the Graphics Touch System Service

Step 5: Generate Harmony code
Step 6: Include application specific source code and files
Step 7: Review the application code
Step 8: Debug your application

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.