Main Content

PWM

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

Since R2024a

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

  • Arduino Advanced AVR PWM Icon

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

Description

Use the PWM block to generate square pulses of varying duty cycle on the Arduino® AVR 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 Uno

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Leonardo

  • Arduino Micro

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 of 0.8 (204/255).

  • 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 parameter is read-only.

This read-only parameter is set to Specify. You can specify the frequency of the PWM signal in the Desired(Hz) parameter.

Specify the desired output frequency of the PWM signal.

This parameter is read-only.

The block generates the PWM signals with the frequency that is closest to the frequency specified in the Desired(Hz) parameter. This is a read-only parameter.

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

Note

Select an appropriate interrupt handler in the Interrupt name parameter of the Hardware Interrupt block to generate an interrupt at each timer compare match.

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 the timer/counter operation, TOP value is defined by the period (PER) register value.

Note

Configure the same interrupt handler in this parameter as in the Interrupt name parameter of the Hardware Interrupt block as in the PWM block.

Version History

Introduced in R2024a