This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Writes average PWM voltage to a GPIO pin


writePWMVoltage(mypi, pinNumber, voltage)


writePWMVoltage(mypi, pinNumber, voltage) writes the frequency of the output square wave to the output pin.

The PWM uses a software implementation allowing all GPIO pins on theRaspberry Pi™ hardware board to output a PWM signal. 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.

Input Arguments

collapse all

Connection to a specific 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

Mean voltage of the square wave at the output pin, in volts. The value is constrained between 0 V and 3.3 V. A value of 0 V produces no output. A value of 1.65 V produces a square wave with a 50% duty cycle at the output pin.


collapse all

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

mypi = raspi();
configurePin(mypi, 12, 'PWM');
writePWMFrequency(mypi, 12, 2000);
writePWMVoltage(mypi, 12, 1.65);

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

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2016b