PIC24 Tutorial – Part 2 – Getting Started – Your First Program

For this tutorial you’ll need the following tools:

  • A PIC24 hardware platform – such as our picoTRONICS24 or nanoTRONICS24 microcontroller development boards
  • Microchip’s FREE MPLAB X IDE – make sure you’ve got the latest version – they are updating it all the time
  • Microchip’s FREE C30 or XC16 C Compiler – also make sure you have the latest version

From now on we assume you have successfully installed both the MPLAB X IDE and the C30 or  XC16 C Compiler. We also assume you have working knowledge of C/C++.

Creating your first project

At this point, you can either follow through the tutorial below step-by-step or download the resulting project here. If you choose to download the code you can skip straight to compiling the code (near the bottom of tutorial). The project is configured to work seamlessly with our picoTRONICS24 series development boards. Click here to download the example project.

If you’d like to follow through creating your own project, a brief explanation is given below:

From the main menu in MPLAB X select File > New Project. This will bring up the following window:

In the Categories box choose Microchip Embedded.
In the Projects box on the right, choose Standalone Project.
Now Click Next


In the Family combo box, choose your target device’s family – you want the PIC24.
In the Device combo box select the target device for this project > PIC24FJ64GB004.
Now Click Next


Select your hardware debug tool such as a PICkit3, ICD or REAL ICE by selecting the serial number immediately below its name.
Click on the serial number to select the tool.
If you don’t see a serial number, the tool is either not connected or its drivers are not installed.
Always click on a hardware tool’s serial number to select it.
Now Click Next

Leave the check box unticked – no debug header is required for the PIC24FJ64GB004

Now Click Next

Select your C compiler from the list
You should be able to select either C30 or XC16 depending on the compiler you have installed.
Now Click Next


Give your project a name, the IDE will automatically create a project directory with this name for you. We’ve called our example “My First PIC Project”
Choose a directory as a location for your project.
Now Click Finish

At this point your project has been successfully created and you should see a window similar to that shown below:

My First PIC Project - MPLAB X

Adding files to your new project

To Add a new source file to the project do the following:

  • Right Click on source files and select New > C Main File

MPLAB X Adding New File to Project

Call the new file main

Click Finish

MPLAB X Adding Main.c

Your editor should now look similar to that shown below


A really simple example program

The code below is a really simple example program to run on your PIC microcontroller. Certainly a bare bones program. It does two useful things through, verifies that your hardware is setup correctly and that your programmer is working!

All you need to do is copy the code below into the your main.c file open in MPLAB X. Don’t worry too much about the details of the code – we’ll explain these in the following sections.

[code language=”cpp”]
* File: main.c
* Author: Modtronics Australia
* www.modtronicsaustralia.com
* Created on 8 June 2013, 2:55 PM



// Microcontroller config words (fuses) – get these wrong and nothing will work correctly, if at all!!

int main(int argc, char** argv) {

//Loop forever and do nothing

return (EXIT_SUCCESS);


Now it’s time to save your project and check that it compiles. Click the build and clean icon, as shown in the screenshot below.

MPLAB X Compiling Project

Hopefully you should get a message telling you that your build was successful, this will be displayed in the output window of the IDE, as per the screen shot below.

MPLAB X Build Successful

If your build was successful it is now time to program your device, if you received any compile errors, you’ll need to go back and correct those now.

For this next step you’ll need to make sure your programmer (PICKit 3, ICD3 etc), in our case a RealICE is plugged in and connected to your development board. You’ll also need to make sure your development board is powered up.

Once you’ve done all of that it’s time to download your new program to the PIC microcontroller. To do this we’ll use the Debug functionality built into the MPLAB X IDE. Click on the run button as shown below.


It may take a little while, but your new program should now be running on your PIC. You’ll know when this is successful as the output window will say Running as shown in the screenshot below.

MPLAB X Application Running

Well congratulations you have programmed your PIC with your first program!!!

This completes this tutorial, but before we leave it, if you want you can pause, restart and continue your program using the buttons highlighted below

MPLAB X Run and Pause buttons

The example code for this project can be downloaded here.

Comments are closed.