PWM
R2026bGenerate pulse width modulated waveform on Teensy 4.0 and 4.1 output pins and enable PWM interrupts
Since R2026b
PWM Teensy4 block
To add a block to a model, double-click the canvas and start typing the block name. Then, select the block from the list.
Libraries:
Simulink Support Package for Arduino Hardware /
Advanced /
TEENSY4
Description
Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.
The Teensy PWM block generates a pulse width modulated (PWM) square waveform on a specified PWM output pin for Arduino® compatible Teensy 4.0 and 4.1 boards.
Use this block to:
Drive loads such as motors or power stages using PWM duty cycle control.
Enable PWM overflow interrupts or compare match interrupts to synchronize control algorithms and acquisitions.
Configure alignment-related interrupt events (center, rising, and falling) for supported Teensy PWM channels.
Supported Arduino Boards
Teensy 4.0 (Arduino Compatible)
Teensy 4.1 (Arduino Compatible)
Examples
To drive a PWM pin and update the duty cycle over time, start by adding the Teensy PWM block to your model.
Add the Teensy PWM block to your model.
In the model settings, set Hardware board to either
Teensy 4.0 (Arduino Compatible) or
Teensy 4.1 (Arduino Compatible).
In the Teensy PWM block parameters, set Pin number to a PWM-capable Teensy pin.
At the Data input port, provide a signal between
0 and 1023. Run the model. Use an
oscilloscope to verify the duty cycle and frequency on the selected Teensy pin. If
the Data value changes with time, the duty cycle updates accordingly.
Generate an interrupt at the PWM period boundary by enabling the overflow interrupt on a Teensy PWM output. Then, trigger a function-call subsystem using the Teensy Hardware Interrupt block.
Add the Teensy PWM, Teensy Hardware Interrupt, and Function-Call Subsystem blocks to your model. Connect the function-call output of the Teensy Hardware Interrupt block to the trigger port of the Function-Call Subsystem.

In the model settings, set Hardware board to either
Teensy 4.0 (Arduino Compatible) or
Teensy 4.1 (Arduino Compatible).
In the Teensy PWM block, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map PWM Pins to FlexPWM Timer Modules.
Enable Enable overflow/underflow (OVF) interrupt.
In the Teensy Hardware Interrupt block parameters, specify a value for the Interrupt group parameter.
Set Interrupt
name to the handler that corresponds to the FlexPWM module used by
the Teensy PWM block. For example, use
FLEXPWM2_0_Handler when the Teensy
PWM block is using a pin mapped to FlexPWM module 2, sub-channel 0. For
more information, see Map PWM Pins to FlexPWM Timer Modules.
In the Teensy Hardware Interrupt block parameters, in the Events to serve table, select the overflow event. For example, select FlexPWM2_0_Overflow to handle the PWM overflow interrupt.
To prevent preemption by other interrupts while servicing the PWM interrupt, enable Run interrupt service routine as atomic unit.
Generate interrupts at specific points in the PWM cycle (center, rising edge, and falling edge) by enabling compare match events on a Teensy PWM output and handling them using the Teensy Hardware Interrupt block.
Add the Teensy PWM, Teensy Hardware Interrupt, and Function-Call Subsystem blocks to your model. Connect the function-call output of the Teensy Hardware Interrupt block to the trigger port of the Function-Call Subsystem.

In the model settings, set Hardware board to either
Teensy 4.0 (Arduino Compatible) or
Teensy 4.1 (Arduino Compatible).
In the Teensy PWM block, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map PWM Pins to FlexPWM Timer Modules.
Enable Enable compare match interrupt.
Select one or more match events:
In the Teensy Hardware Interrupt block parameters, specify a value for the Interrupt group parameter.
Set Interrupt
name to the handler that corresponds to the FlexPWM module used by
the Teensy PWM block. For example, use
FLEXPWM2_0_Handler when the Teensy
PWM block is using a pin mapped to FlexPWM module 2, sub-channel 0. For
more information, see Map PWM Pins to FlexPWM Timer Modules.
In the Events to serve table, select the required FlexPWM events. For example, select FlexPWM2_0_A_RisingEdge to handle the PWM rising-edge interrupt or FlexPWM2_0_A_FallingEdge to handle the PWM falling-edge interrupt.
To prevent preemption by other interrupts while servicing the PWM interrupt, enable Run interrupt service routine as atomic unit.
Extended Examples
Run PMSM Motor in Open Loop on Teensy Hardware
Use Simulink® Support Package for Arduino® Hardware to run a permanent magnet synchronous motor (PMSM) using open-loop voltage-frequency (V/F) control on a Teensy® development board.
- Since R2026b
- Open Model
Measure Phase Currents of PMSM Motor in Open Loop Teensy Hardware
Use Simulink® Support Package for Arduino® Hardware to measure three-phase currents of a permanent magnet synchronous motor (PMSM). The example deploys an open-loop voltage-frequency (V/F) control algorithm on a Teensy® development board.
- Since R2026b
- Open Model
Estimate Angle and Speed of PMSM Motor in Open Loop on Teensy Hardware
Use Simulink® Support Package for Arduino® Hardware to estimate the rotor angle and speed of a permanent magnet synchronous motor (PMSM) using an extended EMF observer. The example deploys an open-loop voltage-frequency (V/F) control algorithm and the observer on a Teensy® development board.
- Since R2026b
- Open Model
Sensorless Field-Oriented Control of PMSM Motor Using Teensy Hardware
Use Simulink® Support Package for Arduino® Hardware to implement a sensorless field-oriented control (FOC) of a permanent magnet synchronous motor (PMSM). The example deploys the control algorithm to a Teensy development board and drives the motor through the DRV8305EVM inverter.
- Since R2026b
- Open Model
Estimate Battery Current of PMSM in Open-Loop Control Using Arduino Hardware
Use Simulink® Support Package for Arduino® Hardware to estimate the battery current of a permanent magnet synchronous machine (PMSM) in open-loop control using Motor Control Blockset™. This example also shows how to actuate a PMSM motor and calculate phase currents that are indirectly used to estimate the current of the battery.
Ports
Input
The value at the block input port determines the width, or duty-cycle, of the square wave that the Arduino hardware outputs on the specified PWM pin.
For example:
Sending the maximum value,
1023, to the block input port produces a duty cycle of 100%, which results in full power at the PWM pin.Sending the minimum value,
0, to the block input port produces a duty cycle of 0%, which results in no power at the PWM pin.Sending an intermediate value to the block input port produces a proportional duty cycle and power output at the PWM pin. For example, sending a value of
512to the block input port produces a duty cycle of 50%, and power (512/1023 = ~0.5).Sending out-of-range values, such as
1500or-1500, to the block input port has the same effect as sending the maximum or minimum input values, respectively.
The block input port inherits the data type of the upstream block, and
internally converts the data to the uint8 data type.
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean
Parameters
Set this parameter to the pin on the Teensy board where the board outputs the PWM waveform. Teensy 4.0 and 4.1 support different FlexPWM pin mappings and pins map to different PWM channels. Click View pin map to open the Arduino pin mapping table. For more information on how to assign pins or view the pins for the PWM block, see Map PWM Pins to FlexPWM Timer Modules.
Note
Do not assign the same pin number to multiple blocks within a model.
This read-only parameter displays the PWM output frequency in Hz.
Specify the desired output frequency of the PWM signal.
The block generates the PWM signals with the frequency that is closest to the frequency specified in the Desired (Hz) parameter. This read-only parameter displays that frequency.
Enable this interrupt at each compare match event of the Arduino timer. Selecting this parameter enables the PWM Center, Rising Edge, and Falling Edge parameters, which let you specify where in the PWM signal the interrupt occurs.
Enable a center compare match interrupt for supported PWM channels. Center-aligned behavior is particularly useful in motor-control sampling and synchronization workflows.
Dependencies
To enable this parameter, select Enable compare match interrupt.
Enable a rising edge compare match interrupt for supported PWM channels.
Dependencies
To enable this parameter, select Enable compare match interrupt.
Enable a falling edge compare match interrupt for supported PWM channels.
Dependencies
To enable this parameter, select Enable compare match interrupt.
Enable this interrupt when the timer or counter reaches the maximum value (TOP) while counting up and when the timer or counter reaches the minimum value (ZERO) while counting down. For the dual-slope mode of timer or counter operation, the TOP value is defined by the Period (PER) register value.
Enable this interrupt to configure the PWM output to invert polarity, swapping the logical high and low portions of the waveform.
More About
The Desired (Hz) block parameter specifies your target PWM frequency. The actual PWM signal generated at pin can differ from this value because of these factors:
Arduino MCU clock
Timer resolution and prescalers
FlexPWM counter mode (center-aligned or edge-aligned)
The block calculates the nearest achievable frequency based on these hardware constraints and displays it in the Achievable (Hz) parameter. If the achievable frequency does not meet your requirements, adjust the Desired (Hz) value until the achievable value is acceptable.
Verify the actual output frequency on the Arduino pin you specify in the Pin number parameter.
For example, suppose that you set Desired (Hz) to 20000 for a
motor-control application on a Teensy board. When you run the model, however, the block
displays 19531.25 in the Achievable
(Hz) parameter. The difference occurs because the timer can divide the
MCU clock only by integer prescaler values. If this deviation is unacceptable for your
application adjust the desired value.
Teensy 4.0 and 4.1 boards route PWM output through multiple FlexPWM timer modules. Each FlexPWM module contains subchannels, and each subchannel maps to specific digital pins through two output paths: PWM_A and PWM_B. The pin mapping table shows these associations.
Use this table to:
Determine which FlexPWM timer module and sub-channel a given pin belongs to.
Identify which pins share the same timer and therefore the same PWM frequency.
Select the correct ADC trigger timer in the Teensy Analog Input block for PWM-synchronized ADC conversions.
Select the corresponding interrupt group in the Teensy Hardware Interrupt block for PWM interrupts.

Consider reading the table as follows:
Each table row represents a FlexPWM module and subchannel combination. For example, the FlexPWM2_0 row prescribes module 2, subchannel 0. The pin numbers listed in that row are the physical pins routed to that timer. The PWM_A or PWM_B outputs on the same subchannel share the same timer frequency but can have independent duty cycles. Use complementary PWM_A and PWM_B outputs when your application requires paired signals, such as H-bridge motor control.
Once you know the FlexPWM module for your pin:
Enter the pin number in the Pin number parameter of the Teensy PWM block.
To synchronize ADC conversions, set the ADC trigger timer parameter of the Teensy Analog Input block to the same FlexPWM module (for example,
FlexPWM2_0).To handle PWM interrupts, set the Interrupt group parameter of the Teensy Hardware Interrupt block to the matching FlexPWM group (for example,
PWM Interrupt FLEXPWM2_X).Do not assign the same pin number to multiple blocks within a model.
Extended Capabilities
The Teensy PWM block supports C/C++ code generation using Embedded Coder®.
Version History
Introduced in R2026b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)




