This step-by-step tutorial walks you through creating your first MPLAB Blockset project β€” a simple LED blink application that runs on a Microchip development board.

What You’ll Learn

  • How to create a new Simulink model
  • How to configure the Master block for your target device
  • How to add and connect peripheral blocks
  • How to build and deploy firmware to hardware

Time to complete: ~15 minutes

Prerequisites

Before you begin, ensure you have:

  1. MATLAB + Simulink (R2018b or later)
  2. MPLAB Device Blocks for Simulink installed (installation guide )
  3. XC-DSC or XC16 compiler installed
  4. A Microchip development board (e.g., dsPIC33CK Curiosity board, MCLV-2, or similar)
  5. MPLAB X IDE (for programming, if not using auto-programming)
Don’t have hardware yet? You can still follow this tutorial and build the model. The code will generate successfully even without a connected board.
  1. Launch MATLAB
  2. Open Simulink by typing simulink in the command window and pressing Enter
  3. In the Simulink Start Page, click Blank Model
  4. Save the model immediately:
    • Press Ctrl+S (or File β†’ Save)
    • Name it my_first_blink.slx
    • Save it in a working directory where you have write permissions
Tip: Always save your model in a location where you have full write permissions. Avoid saving directly in the MATLAB installation folder or the blockset examples folder.

Step 2: Add the Master Block

The Master block is required in every MPLAB Blockset model. It configures the target device and system clock.

  1. Open the Simulink Library Browser:

    • Press Ctrl+Shift+L or click the Library Browser button in the Simulink toolbar
  2. Navigate to MCHP_Blockset β†’ System Configuration

  3. Drag the Microchip Master block into your model

  4. Double-click the Master block to open its configuration dialog

  5. Select your target device:

    • In the Target dropdown, search for and select your chip (e.g., 33CK256MP508 for the dsPIC33CK Curiosity board)
    • Click Apply β€” the dialog will close and reopen with device-specific settings

Common target devices:

  • dsPIC33CK Curiosity: 33CK256MP508
  • dsPIC33CK Curiosity Nano (EV88G73A): 33CK64MC105
  • dsPIC33AK Curiosity Nano (EV17P63A): 33AK512MPS506
  • MCLV-2 with dsPIC33CK PIM: 33CK256MP508
  • MCLV-48V-300W + Perseus DIM: 33AK128MC106
  • MCLV-48V-300W + BlueRidge DIM: 33AK256MPS306
  • MCLV-48V-300W + Sagitta DIM: 33CK256MP508
  • MCLV-48V-300W + Andromeda DIM: 33CK64MC105
  • MCHV-230VAC-1.5kW + Perseus DIM: 33AK128MC106

Use the search box to quickly find your device.

  1. Verify the oscillator settings:

    • Switch to the Oscillator tab
    • For most development boards, the default Internal Fast RC (FRC) oscillator works out of the box
    • If your board has an external crystal, select Crystal / Ceramic resonator and enter the frequency
  2. Note the Time-Step synchronisation field on the General tab β€” we’ll set this in Step 6

  3. Click OK to close the dialog

Step 3: Add a Digital Output Write Block

This block controls a GPIO pin to drive an LED.

  1. In the Library Browser, navigate to MCHP_Blockset β†’ Digital IO

  2. Drag the Digital Output Write block into your model

  3. Double-click the Digital Output Write block to configure it

  4. Select the LED pin for your board:

    • Port: Select the port (e.g., E for Port E)
    • Pin: Enter the pin index (e.g., 0 for RE0)

Common LED pins by board:

  • dsPIC33CK Curiosity: RE0 (Port E, Pin 0)
  • dsPIC33AK Curiosity Nano (EV17P63A): RD0 (LED0)
  • dsPIC33CK Curiosity Nano (EV88G73A): RD10 (LED0)
  • MCLV-2: Varies by PIM β€” check your board’s schematic
  • MCLV-48V-300W: Board-specific LED on M1_LED1 / M1_LED2 (see template PINOUT)
  • MCHV-230VAC-1.5kW: RD5 (M1_LED1)

If you’re unsure, consult your board’s user guide or schematic.

  1. Set Initial Output to 0 (LED starts OFF)

  2. Leave Output mode as Push-Pull (default)

  3. Click OK

Step 4: Add a Pulse Generator

The Pulse Generator creates a square wave to toggle the LED.

  1. In the Library Browser, navigate to Simulink β†’ Sources

  2. Drag a Pulse Generator block into your model

  3. Double-click the Pulse Generator to configure it:

    • Amplitude: 1
    • Period (secs): 1 (toggles every 1 second β†’ 1 Hz blink rate)
    • Pulse Width (% of period): 50 (LED ON for 50% of the time)
    • Phase delay: 0
  4. Click OK

Step 5: Add Data Type Conversion

The Digital Output Write block expects a boolean input, but the Pulse Generator outputs a double. We need to convert it.

  1. In the Library Browser, navigate to Simulink β†’ Signal Attributes

  2. Drag a Data Type Conversion block into your model

  3. Double-click the Data Type Conversion block

  4. Set Output data type to boolean

  5. Click OK

Step 6: Connect the Blocks

Now connect everything together:

  1. Draw a signal line from Pulse Generator output to Data Type Conversion input

    • Click the output port (right side) of the Pulse Generator
    • Click the input port (left side) of the Data Type Conversion block
  2. Draw a signal line from Data Type Conversion output to Digital Output Write input

Your model should now look like this:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Pulse      │─────▢│  Data Type       │─────▢│  Digital Output      β”‚
β”‚  Generator   β”‚      β”‚  Conversion      β”‚      β”‚      Write           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Microchip Master    β”‚  (not connected β€” system configuration only)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Note: The Master block does not connect to other blocks. It configures the system globally.

Step 7: Configure Model Settings

Set up code generation and solver settings:

  1. Open Model Configuration Parameters:

    • Press Ctrl+E or click Modeling β†’ Model Settings
  2. Navigate to Solver:

    • Type: Select Fixed-step
    • Solver: Select discrete (no continuous states)
    • Fixed-step size: Enter 0.001 (1 ms time step)
  3. Navigate to Code Generation β†’ System target file:

    • Select MCHP_dsPIC_PIC24.tlc for dsPIC devices
    • Or select MCHP_PIC32.tlc for PIC32 devices
  4. Navigate to Code Generation β†’ MCHP Target:

    • Verify your device is correctly selected
    • Verify the compiler version is detected
  5. Click Apply, then OK

Important: The fixed-step size (1 ms in this example) determines how often the model executes. For a simple LED blink, 1 ms is more than adequate. For fast control loops (e.g., motor control), you might need 50 Β΅s (0.00005) or faster.

Step 8: Set Time-Step Synchronisation

Return to the Master block to finalize timing:

  1. Double-click the Microchip Master block

  2. On the General tab, locate Time-Step synchronisation

  3. This field should now display the fixed-step size you set (e.g., 0.001)

  4. If it’s blank or different, re-check your Solver settings in Step 7

  5. Click OK

Step 9: Build the Model

Now generate code and compile:

  1. Click the Build Model button in the Simulink toolbar (or press Ctrl+B)

  2. Watch the Diagnostic Viewer at the bottom of the Simulink window:

    • Code generation starts
    • C code is created in a folder named my_first_blink_mchp_pic_ert_rtw
    • XC compiler compiles the code
    • If successful, you’ll see: Build succeeded
  3. Check the output files in the model folder:

    • my_first_blink.elf β€” Executable with debug symbols
    • my_first_blink.hex β€” Programming file for MPLAB X IPE
Build failed? Check the Troubleshooting section below.

Step 10: Program the Device

If you have a programmer connected (ICD4/5, PICkit4/5, PKOB4, Snap):

  1. In Model Configuration Parameters β†’ Code Generation β†’ MCHP Target:

    • Enable Program the device after successful build
  2. Click Build again β€” the firmware will flash automatically after compilation

Option B: Using MPLAB X IPE

If auto-programming is not configured:

  1. Open MPLAB X IPE (not MPLAB X IDE)

  2. Select your Device and Tool (programmer)

  3. Click Connect

  4. Browse to the .hex file in your model folder

  5. Click Program

  6. Wait for Programming/Verify complete message

If everything went well:

  • The LED on your development board should now blink at 1 Hz (1 second ON, 1 second OFF)
  • If it doesn’t blink, see Troubleshooting

Congratulations! You’ve successfully created, built, and deployed your first MPLAB Blockset model.

Troubleshooting

Build Fails

  • Compiler not found:

    • Verify XC-DSC or XC16 is installed
    • Run picInfo('check') in MATLAB to verify compiler detection
    • Restart MATLAB after installing compilers
  • Device Family Pack (DFP) missing:

    • Install the DFP from MPLAB X IDE: Tools β†’ Packs β†’ MPLAB Packs
  • Wrong pin selected:

    • Verify LED pin from your board’s schematic or user guide
    • Check the pin assignment in the Digital Output Write block
  • Oscillator misconfigured:

    • If using an external crystal, verify the frequency matches your hardware
    • Try switching to Internal Fast RC (FRC) in the Master block’s Oscillator tab
  • Code didn’t program:

    • Verify the programmer is connected and recognized by MPLAB X IPE
    • Check power supply to the board

Pulse Generator Output is Wrong

  • Ensure Data Type Conversion is set to boolean
  • Verify Pulse Generator Amplitude is 1 (not 0)

What’s Next?

Now that you’ve mastered the basics, explore these next steps:

Learn More Configuration

Add Real-Time Tuning

  • External Mode & PIL β€” Change parameters while the model runs on hardware
  • Adjust LED blink rate in real-time without reprogramming

Understand the Generated Code

Try More Complex Examples

  • PWM Output β€” Drive a motor or servo with pulse-width modulation
  • ADC Input β€” Read analog sensors (potentiometers, temperature sensors)
  • UART Communication β€” Send/receive data over serial
  • Motor Control β€” Implement field-oriented control (FOC) algorithms

Explore Pre-Built Examples

The blockset includes dozens of ready-to-run examples. Access them via:

cd(fullfile(fileparts(which('MCHP_dsPIC_stf.tlc')), '../examples'))

Copy an example to your working directory before modifying.

See Also