Contenido principal

writePWMDutyCycle

R2026b

Writes PWM duty cycle to PWM pin

Description

writePWMDutyCycle(mypi, pinNumber, dutyCycle) writes the duty cycle of the output square wave to the output pin.

The PWM uses a software implementation allowing all GPIO pins on the Raspberry Pi® hardware board to operate as a PWM pin. Each PWM pin outputs a square waveform with variable frequency, duty cycle, or average voltage. A duty cycle of 0 means the waveform is always low and a duty cycle of 1 means the waveform is always high.

To use a PWM pin, reconfigure one of the multiple-use GPIO pins as a PWM pin.

Note

To generate C/C++ code for this function, you must have an Embedded Coder® license.

example

Examples

collapse all

Enable PWM pin 12 and set the frequency and duty cycle.

mypi = raspi();
configurePin(mypi, 12, 'PWM');
writePWMDutyCycle(mypi, 12, 0.5);
writePWMFrequency(mypi, 12, 2000);

The result is a square wave with a 50.0% duty cycle, frequency of 2000 hertz, and average voltage of 1.65 V.

Input Arguments

collapse all

Connection to the Raspberry Pi hardware board, specified as a raspi object.

GPIO pin number, specified as a scalar. This argument does not accept vectors because the hardware cannot access multiple pins simultaneously.

To get a list of valid pin numbers, enter mypi.AvailableDigitalPins.

Example: 12

Data Types: double

Duty cycle of output waveform, specified as a numeric scalar between 0 and 1. This value is assigned to output pin.

Data Types: single | double

Extended Capabilities

expand all

Version History

Introduced in R2016b