Main Content

PWM

Generate square waveform on specified analog output pin

Library

Simulink Support Package for Arduino® Hardware/Common

Description

Use pulse-width modulation (PWM) to change the duty-cycle of square-wave pulses output by a PWM pin on the Arduino hardware. PWM enables a digital output to provide a range of different power levels, similar to that of an analog output.

The value sent to the block input determines the width of the square wave, called duty-cycle, that the target hardware outputs on the specified PWM pin. The range of valid input values is 0 to 255.

For example:

  • Sending the maximum value, 255, to the block input produces 100% duty-cycle, which results in full power on a PWM pin.

  • Sending the minimum value, 0, to the block input produces 0% duty-cycle, which results in no power on a PWM pin.

  • Sending an intermediate value to the block input produces a proportional duty-cycle and power output on a PWM pin. For example, sending 204 to the block input produces 80% duty cycle and power (204/255 = 0.8).

  • Sending out-of-range values, such as 500 or -500, to the block input has the same effect as sending the maximum or minimum input values.

The block input inherits the data type of the upstream block, and internally converts it to uint8.

Some limitations:

  • With Arduino Uno, Nano hardware, the Arduino PWM block cannot use digital pins 9 or 10 when the model contains Servo blocks.

  • With Arduino Mega 2560, Mega ADK hardware, the Arduino PWM block cannot use digital pins 11 or 12 when the model contains more than 12 Servo blocks.

  • With Arduino Due hardware, the Arduino PWM block cannot use digital pins 9 or 10 when the model contains Servo blocks. For Arduino Due hardware, all the supported PWM pins have a frequency 1000 Hz.

  • In Connected I/O, configuring frequency is not supported. The block generates PWM signal only with default frequencies.

Parameters

Pin number

Enter the number of the PWM pin.

Do not assign the same pin number for different types of blocks as this may cause resource management conflicts.

Click View pin map to open the Arduino Pin Mapping table .

To know how to assign pins or to know about the fixed pins for the blocks, see the Arduino Pin Mapping for Timer Dependent Blocks.

Frequency

Configure the PWM frequency of the PWM signal.

When you set the Frequency as:

  • Default –– The block generates PWM signal with default frequency. For information on default PWM frequencies and PWM supported pins for different Arduino boards, see PWM supported pins and frequenciespw.

  • Specify –– Using this parameter, you can specify the frequency of the PWM signal to be generated. The block generates PWM signal with an Achievable(Hz) frequency. When you set Frequency as Specify, the Desired(Hz) and Achievable(Hz) parameters become available. In the Desired(Hz) parameter, specify the desired frequency of the PWM signal. The block generates PWM signals with achievable frequency that is closest to the frequency specified in the Desired(Hz) parameter.

Desired(Hz)

Specify the desired frequency of the PWM signal.

Achievable(Hz)

The block generates PWM signals with achievable frequency that is closest to the frequency specified in the Desired(Hz) parameter. For proper calculation of achievable frequency, you must specify your Arduino board in the Hardware board parameter of the Model Configuration Parameters for Simulink Support Package for Arduino Hardware dialog box.