Skip to main content

PWM

This guide shows how to find a PWM pin on the board's 40-pin header and generate a signal from the Linux shell. You'll set the frequency and duty cycle, change the duty cycle while the output is running, and stop the signal.

Before You Start​

Open a shell on the board as root. Run the commands in the same shell so the variables stay available. You can use an oscilloscope to view the waveform.

danger

Check the header orientation, pinout, and PWM output voltage before using the header. Power off the board before attaching test equipment and avoid shorting adjacent pins.

See Hardware Resources for the board datasheet and schematic.

Board Header Mapping​

The diagram shows each pin's physical number and default function. This example uses physical pin 32 for PWM and pin 6 as ground for the oscilloscope.

3.3 V
1
2
5 V
TW3_SDA
40PIN_GPIO2
3
4
5 V
TW3_SCL
40PIN_GPIO3
5
6
GND
GPIO36
40PIN_GPIO4
7
8
40PIN_GPIO14
UART3_TXD
GND
9
10
40PIN_GPIO15
UART3_RXD
GPIO15
40PIN_GPIO17
11
12
40PIN_GPIO18
I2S3_BCLK
GPIO7
40PIN_GPIO27
13
14
GND
UART3_CTSn
40PIN_GPIO22
15
16
40PIN_GPIO23
UART3_RTSn
3.3 V
17
18
40PIN_GPIO24
SM_GPIO0
SPI1_SDO
40PIN_GPIO10
19
20
GND
SPI1_SDI
40PIN_GPIO9
21
22
40PIN_GPIO25
SM_GPIO1
SPI1_SCLK
40PIN_GPIO11
23
24
40PIN_GPIO8
SPI1_SS0n
GND
25
26
40PIN_GPIO7
SPI1_SS1n
GPIO44
40PIN_GPIO0
27
28
40PIN_GPIO1
GPIO45
GPIO14
40PIN_GPIO5
29
30
GND
GPIO4
40PIN_GPIO6
31
32
40PIN_GPIO12
PWM0
PWM1
40PIN_GPIO13
33
34
GND
I2S3_LRCKIO
40PIN_GPIO19
35
36
40PIN_GPIO16
SM_GPIO11
GPIO13
40PIN_GPIO26
37
38
40PIN_GPIO20
I2S3_DI
GND
39
40
40PIN_GPIO21
I2S3_DO
PowerGroundGPIOI²CAudioPWMSPIUART

Set an Output​

Header pinPWM controllerLinux PWM channel
32f7f20000.pwm0
33f7f20000.pwm1

List the pwmchip entries under /sys/class/pwm/. Look for f7f20000.pwm in the target path on the right.

Board shell (root)
ls -ld /sys/class/pwm/pwmchip*

Replace pwmchipN below with the matching entry's name. To use another pin from the table, set CHANNEL to the corresponding value.

Board shell (root)
PWMCHIP=/sys/class/pwm/pwmchipN
CHANNEL=0
PWM="$PWMCHIP/pwm$CHANNEL"

Export the channel and generate a 1 kHz signal with a 50% duty cycle. A period of 1000000 nanoseconds is 1 ms. A duty_cycle of 500000 nanoseconds makes the signal active for half that period.

Board shell (root)
echo "$CHANNEL" > "$PWMCHIP/export" &&
echo 1000000 > "$PWM/period" &&
echo 500000 > "$PWM/duty_cycle" &&
echo 1 > "$PWM/enable"

If export fails, stop and check the chip and channel. A channel in use is unavailable for export. If a later write fails, run just the unexport command under Stop the Output before trying again.

Change the Duty Cycle​

Write 250000 to duty_cycle to set the duty cycle to 25%. The frequency stays at 1 kHz. On an oscilloscope, each active pulse lasts about 250 µs.

Board shell (root)
echo 250000 > "$PWM/duty_cycle"

Stop the Output​

Set the duty cycle to zero, disable PWM, and release the channel.

Board shell (root)
echo 0 > "$PWM/duty_cycle" &&
echo 0 > "$PWM/enable" &&
echo "$CHANNEL" > "$PWMCHIP/unexport"