Main Content

PWM

Generate pulse width modulated signal on analog output pin of Arduino SAMD21 core

Since R2023b

Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.

  • Arduino Advanced SAMD PWM Icon

Libraries:
Simulink Support Package for Arduino Hardware / Advanced / SAMD

Description

Use the PWM block to generate square pulses of varying duty cycle on the Arduino® SAMD 21 hardware pin.

Note

The PWM block supports C/C++ code generation. This feature allows you to generate C and C++ code using Embedded Coder®.

Supported Arduino Boards

  • Arduino MKR 1000

  • Arduino MKR ZERO

  • Arduino MKR Wi-Fi® 1010

  • Arduino Nano 33 IoT

Ports

Input

expand all

The value at the block input port determines the width of the square wave, called duty-cycle, that the Arduino hardware outputs on the specified PWM pin.

For example:

  • Sending the maximum value, 255, 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 204 to the block input port produces a duty cycle of 80%, and power (204/255 = 0.8).

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

Parameters

expand all

Specify the Arduino hardware board pin number to which the PWM input signal is connected. 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 Pin Mapping for Arduino Timer Dependent Blocks.

Note

Do not assign the same pin number to multiple blocks within a model.

This read-only parameter displays the output frequency of the PWM signal.

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.

Select one of these options to determine when the block generates a dual-slope timer waveform.

  • Dual-slope, interrupt/event at TOP (DSTOP) — The block generates the PWM interrupt/event when the timer/counter value reaches the maximum value.

  • Dual-slope, interrupt/event at ZERO (DSBOTTOM) — The block generates the PWM interrupt/event when the timer/counter value reaches the minimum value.

  • Dual-slope, interrupt/event at TOP and ZERO (DSBOTH) — The block generates the PWM interrupt/event when the timer/counter value reaches both, the maximum and the minimum value.

Select this option to generate an interrupt at each compare match of the Arduino timer.

Select this option to generate an interrupt when the timer/counter reaches the maximum value (TOP) during up counting and when the timer/counter reaches the minimum value (ZERO) during down counting. For dual-slope mode of timer/counter operation, TOP value is defined by the Period (PER) register value.

Select this option to invert the polarity of the PWM block output waveform.

Version History

Introduced in R2023b