Main Content

PWM

R2026b

Generate pulse width modulated waveform on Teensy 4.0 and 4.1 output pins and enable PWM interrupts

Since R2026b

  • Arduino Advanced Teensy4 PWM Icon

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

expand all

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.

Block diagram showing a Constant block providing duty cycle to a Teensy PWM block with overflow interrupt enabled on pin 4, and a Teensy Hardware Interrupt block configured for FLEXPWM2_0 overflow interrupt with its function-call output triggering a Function-Call subsystem on a Teensy board.

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.

Block diagram showing a Constant block providing a duty cycle value to a Teensy PWM block with compare match interrupt enabled for center, rising edge, and falling edge events on pin 4, and a Teensy Hardware Interrupt block configured for FlexPWM2_0 compare match events with multiple function-call outputs each triggering a separate Function-Call Subsystem on a Teensy board.

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

Ports

Input

expand all

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 512 to the block input port produces a duty cycle of 50%, and power (512/1023 = ~0.5).

  • Sending out-of-range values, such as 1500 or -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

expand all

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

expand all

Extended Capabilities

expand all

Version History

Introduced in R2026b