Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB HID

Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB HID

Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB HID

Controlling a PIC Microcontroller from a PC Graphical User Interface diagramFigure 1: Controlling a PIC Microcontroller from a PC Graphical User Interface diagram

A Graphical User Interface is a man-machine interface device, in which objects to handle are drawn as icons on the screen, so the user can send controls by a pointing device, usually a mouse or a keyboard.

It is always easy and requires less skills to operate a device from a visual representations by simply clicking a mouse or using a keyboard rather than a command line. The GUI can also be used to interface with other external devices located in different places. 

There are a lot of different kinds of software which can be used to design a GUI platform, the choice will usually depend on personal preferences, software capabilities and the operating systems (Windows, Linux, Mac…).
Among the popular ones we have Microsoft Visual studio with its popular programming languages visual basic and C#, there is also Labview, Python, Matlab and many more.

In the project: Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB, we designed a Graphical User Interface (GUI) software using Microsoft Visual C# to control the LEDs connected to the PIC microcontroller. This software could be installed in any computer running windows operating systems. The computer connects to the microcontroller using a USB cable. The PIC microcontroller will receive commands from the computer to control devices connected to it such as motors, LEDs etc.

In this project, we emulated the serial port interface by using the USB CDC class, in this way the same PC GUI software can be used wither with the old RS232 serial connection (COM Port) or with USB cable.

One of the drawbacks of this method, is that the software is not plug and play as it always expected with USB, you plug a device and it’s immediately recognize by the computer operating system, it loads the drivers automatically and you can start using the device immediately without hassle or any setting required.

With the serial port, the USB device will emulate a COMP port, in the software, you will have to know which COMP port number it is to select it, click on Connect before communication between the PC and the microcontroller can be established. If a connection is lost during transfer of data, the same process has to be repeated.

With USB HID (Human Interface Device) usually used for interface devices like mice and keypads, it’s plug and play, you plug a USB mouse and the computer will detect it and you can start using it with zero configuration, no need to install any drivers, the operating system will detect it.

You can also use the USB HID class to transfer custom application data, in this project, we are going to control again three LEDs connected to the PIC microcontroller.

Once the application is started, it will automatically detect the USB device and display a status message in red if the correct USB device is not found as shown on figure 2 below.

Figure 2: Microcontroller not detected

To learn more, please read these article first:

USB Communication With PIC Microcontroller HID – XC8

Controlling a PIC Microcontroller from a PC Graphical User Interface (GUI) through USB


Buy a USB PIC Microcontroller from Our Online Shop

USB is very different to other simpler peripherals like USART or SPI for example where you could simply add the peripheral interface to your project with some few library codes or accessing the registers by reading the datasheet.
With USB it’s different, you really need to write your code off the USB framework like the TCP/IP framework.
USB is an order of magnitude more complicated than the other peripherals. It must be constantly serviced to maintain a connection to a PC.

Fortunately Microchip provides Application Libraries that one can simplify the job for us. You can use Microchip Libraries for Applications (MLA) which have sample projects for different USB classes. Under the folder:

apps –> usb –> device  you can choose the sample projects with the USB class you are developing, in this project, we will modify the hid_custom project. The GUI software, we also modify the sample supplied plug_and_play_example in hid_custom –> utilities. So all credits to Microchip Technology. We used Visual Studio 2019, but almost any version could also be used, note that we used .NET Framework and not .NET Core project.

To learn more about Microchip Libraries for Applications and how to copy the sample projects, please read:

Microchip Libraries for Applications

Using prepared libraries make your job much easier, but you must use them carefully, not just try to bolt them onto an existing application, read the supplied documentations in the doc folder to understand the structure and different files used in the libraries.

You can learn how to create a PC software with Microsoft Visual Studio from this tutorial:

Creating a Serial Port Interface with C#

PIC Microcontroller Project

We are using the PIC18F4550, but almost any other USB PIC could be used in this project. Three LEDs are connected to PORTD, Red LED on RD1, Yellow LED on RD2 and Green LED on RD3. Another STATUS LED is connected to RD0 to indicate the status of the USB communication when used in interrupt mode.

In the Main code, we are calling the function: APP_DeviceCustomHIDTasks(); which has our specific tasks to control the LEDs in the app_device_custom_hid.c file.

When the user Click on one of the three buttons on the GUI, the application will send a command data to the PIC, the PIC will detect which command was sent and toggle the appropriate LED.

We have 4 commands, when the PIC receives 0x80, it will toggle the RED LED, 0x81 the Yellow LED, 0x82 the Green LED and 0x83 to switch OFF all LEDs. This last command is sent when the GUI application closes so that we can switch OFF all LEDs on exist.

PC GUI Software

Figure 2 above shows the GUI software displaying: USB device is not found in red when it does not detect the USB device or if the cable is disconnected.

In order for this program to “find” a USB device with a given VID and PID, both the VID and PID in the USB device descriptor (in the USB firmware on the microcontroller), as well as in this PC application source code, must match.

In the Microcontroller code in usb_descriptors.c, the VID and PID values were declared as:

In the PC GUI software in the function: CheckIfPresentAndGetUSBDevicePath(); the VID and PID are initialized:

Figure 3: PC GUI Software interface sending commands to PIC microcontroller

The GUI software has two more threads beside the main thread:


This thread does the actual USB read/write operations (but only when AttachedState == true) to the USB device.
It is generally preferable to write applications so that read and write operations are handled in a separate
thread from the main form. This makes it so that the main form can remain responsive, even if the I/O operations
take a very long time to complete.

Since this is a separate thread, this code below executes independently from the rest of the code in this application. All this thread does is read and write to the USB device. It does not update the form directly with the new information it obtains (such as information to or from the microcontroller). The information that this thread obtains is stored in atomic global variables. Form updates are handled by the FormUpdateTimer Tick event handler function.

This application sends packets to the endpoint buffer on the USB device by using the “WriteFile()” function.
This application receives packets from the endpoint buffer on the USB device by using the “ReadFile()” function.


This timer tick event handler function is used to update the user interface on the form, based on data obtained asynchronously by the ReadWriteThread and the WM_DEVICECHANGE event handler functions.

When the buttons are clicked, they will update the ToggleLEDPending variables which are checked in the ReadWriteThread.

USB Simulation with Proteus

You can simulate USB communication using one of the supported microcontrollers in Proteus if you don’t want to build the USB hardware. You will be able to test both the firmware and the hardware by simulating the circuit. Communication is modelled down to Windows driver level, with all requests to and replies from the simulated USB device displayed in the USB Transaction Analyser

You can simulate these USB Device classes:

  • Mass Storage Device Class (MSD.)
  • Human Interface Device Class (HID).
  • Communications Device Class (CDC)

For this you will need the correct Proteus license to enable you to simulate USB and Install the USB Drivers.

Figure 4: USB Analyzer

You can download the full project files (MPLAB XC8 source code, C# Project and Proteus Simulation project) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

Download MPLAB X Project: PIC_GUI_USB_HID

Download C# GUI Project: Control PIC GUI USB HID C#

Download Proteus Simulation Project: Proteus Project Control PIC GUI USB HID

Share this post

You've just added this product to the cart:

Select your currency
EUR Euro
USDUnited States (US) dollar