PIC24/dsPIC33 Digital Input Example

Last modified by Microchip on 2024/06/24 06:33


This example project demonstrates how to configure and use digital inputs on a PIC24F MCU. You will be shown how to configure a pin on the MCU, which is connected to a mechanical switch as a digital input. You will also configure another pin, connected to a Light-Emitting Diode (LED), as an output. The source code will be added to the project which reads the immediate value of the input switch and uses that value to drive the LED.

As a result of this lab:

  • When the switch is pushed down, the LED will be on.
  • When the switch is released, the LED will be off.

digital input diagram

This project uses the PIC24F Curiosity Development Board. This board has two LEDs and two input switches:

  • LED1: connected to pin RA9 (PORTA pin 9).
  • LED2: connected to RA10.
  • S1: connected to RC9.
  • S2: connected to RC8.

PIC24F curiosity board showing Leds1 and 2 and switches 1 and 2.

We will first show you how to use MPLAB® Code Configurator (MCC) to generate code to configure RA9 (LED1) as an output pin, and RC9 (S1) as an input pin. You will then enter the code to drive LED1 high when S1 is pushed.


To follow along with this example, you will need the following software and hardware:

Software Tools

Hardware Tools

Information on how to download the software tools or acquire the development board can be found on the "PIC24F and dsPIC33 Examples" page.

Back to top


Create the Project

After installing the software, connect the PIC24F Curiosity Development Board to a USB port on your computer. Create a new standalone project in MPLAB X IDE for a PIC124FJ128GA204. The PIC124FJ128GA204 is the microcontroller on the PIC24F Curiosity Development Board. When the project creation wizard asks for a hardware tool (Step 2 in the New Project window), select the PIC24F Curiosity Board as shown.

new project dialog showing how to select the board

If this is your first time creating an MPLAB X IDE project, please visit the "Create a Standalone Project in MPLAB® X IDE" page to follow step-by-step instructions on how to do this.

After the project has been created, the Projects tab in the upper-left corner of the IDE shows that the project has been created with no source or header files.

shows the projects tab

Back to top

Open MCC

Open MCC under the Tools > Embedded menu of MPLAB X IDE.

launch MCC

MCC will place a Resource Management tab on the left-hand side of the IDE. Inside this tab, you will see a section for Project Resources and Device Resources. For each MCC-generated project, you will need to verify/modify the System Modules under the Project Resources window.

shows the systems resources tab

Back to top

Set the Project Resources

There are three system elements that need attention:

  • Interrupt Module: controls the MCU's interrupts.
  • Pin Module: configures the I/O pins.
  • System Module: selects and configures the clock source for the MCU.

Interrupt Module

This project does not use interrupts so this section will not be used.

System Module

The System Module allows the user to configure the MCU's clock and the Watchdog Timer (WDT), and make changes to the debug pin assignments. This feature of the PIC24F MCU has numerous options, which are typically modified to fit the needs of the application. MPLAB® Code Configurator (MCC) provides default settings if no changes are selected by the developer. For this lab, accept the default clock settings:

  • 8 MHz Internal Free Running Oscillator with no Prescaler, but a 1:2 Postscaler (4 MHz Fosc)
  • Watchdog Timer - disabled
  • Unchanged debug pins

To verify the default settings, click on the System Module tab and verify the following selections have been made:

System Module tab showing default settings

Pin Module

Click on the Pin Module in the Project Resources window. Three windows will open in the IDE:

  • Pin Module Window
  • Package View
  • Grid View

pin manage window

You may need to resize the IDE window to replicate the screen layout.

The Pin Manager: Grid View window shows that pins RB1 and RB0 have been reserved as the programming pins.

We will now set the pin connected to LED1 (RA9) as an output and the pin connected to S1 (RC9) as an input. In the Grid View window, click on the output box under RA9 and the input box under RC9. The grid view will display the padlocks in green, indicating these pins have been configured for use.

pin manager grid view

  • Ensure that RA9 is set as an output pin.
  • Rename RA9 as LED1.
  • Ensure that RC9 is set as an input pin.
  • Rename RC9 as S1.

pin names window

Back to top

Generate Code

To generate the code, click the Generate button in the MCC window.

generate button

The Projects tab will show the source and header files created by MCC.

projects tab showing mcc files

The main(void) function is located within the main.c file. The main(void) function calls the MCC-generated SYSTEM_Initialize() function before it enters the while(1) loop.

main.c in the editor window

SYSTEM_Initialize() in turn calls PIN_MANAGER_Initialize() to configure the I/O pins. PIN_MANAGER_Initialize() loads the TRISA and TRISC registers with the values needed to set RA9 as an output pin and RC9 as an input pin.

system initialize function

Please consult the PIC24FJ128GA204 datasheet for the values used to configure TRISA and TRISC registers.

Back to top

Modify the Program to Use the Value of S1 to Drive LED1

We will now modify main.c to drive RA9 (LED1) with the value of pin RC9 (S1). An inspection of the MCC-generated pin-manager.h header file, shows MCC has created several control functions for the I/O pins we have configured and named. Among these macros are LED1_SetLowLED1_SetHigh, and S1_GetValue().

Make the following modifications to main.c:

Insert the text #include "mcc_generated_files/mcc.h" near the top of the file.
Insert LED1_SetLow and LED2_SetHigh into main().


#include "mcc_generated_files/system.h" 
         Main application

int main(void)
  // initialize the device
  while (1)
     if (S1_GetValue())
  return 1;

#include "mcc_generated_files/mcc.h" is required to be placed in any application source file that accesses MCC-generated functions. This line must be placed above the application's call to an MCC function. Not all versions of MCC correctly include this code into main.c. You will also need to manually add this line to each of the application source files you create.

Back to top

Build, Download, and Run the Code

To run the program on the development board, click on the Make and Program Device Main Project button Main_Program_Target_Project.png. This will build the program into the Flash memory of the PIC® device. The output window of the IDE will tell you when the device has been programmed and the application is running.

output window showing when the device is programmed

After the board is programmed, neither LED1 nor LED2 will be on; they are both turned off.

Back to top


When S1 is pushed, LED1 will turn on. When S1 is released, LED1 will turn off.

show the led state chnges on the board.

Back to top