PWM

Generate PWM waveform on 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 frequency of the PWM signal on most pins is ~490 Hz. On the Arduino Uno and similar boards, pins 5 and 6 have a frequency of ~980 Hz.

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

Some limitations:

  • With Arduino Uno, Nano, Pro, Fio, Mini 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.

Dialog Box

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 block, see Pin Mapping on Arduino Blocks.

    Note:   To change the Target hardware parameter, select Tools > Run on Target Hardware > Options.

Was this topic helpful?