Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Contents

Arduino 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 hardware, the Arduino PWM block cannot use digital pins 9 or 10 when the model contains Servo blocks.

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

  • With Arduino Mega 2560 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 to multiple blocks within the model.

If you set the Target hardware parameter to:

  • Arduino Mega 2560, enter a pin number from 2 to 13.

  • Arduino Uno, enter one of the following pin numbers: 3, 5, 6, 9, 10, and 11. The board identifies these with a ~ symbol.

  • Arduino Nano, enter one of the following pin numbers: 3, 5, 6, 9, 10, and 11.

  • Arduino Due, enter a pin number from 2 to 13.

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

See Also

External Web Sites

Was this topic helpful?