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
TW0 SDA
40PIN_GPIO2
3
4
5 V
TW0 SCL
40PIN_GPIO3
5
6
GND
GPIO
40PIN_GPIO4
7
8
40PIN_GPIO14
UART4 TXD
GND
9
10
40PIN_GPIO15
UART4 RXD
GPIO
40PIN_GPIO17
11
12
40PIN_GPIO18
I2S3 BCLK
GPIO
40PIN_GPIO27
13
14
GND
GPIO
40PIN_GPIO22
15
16
40PIN_GPIO23
GPIO
3.3 V
17
18
40PIN_GPIO24
GPIO
SPI3 SDO
40PIN_GPIO10
19
20
GND
SPI3 SDI
40PIN_GPIO9
21
22
40PIN_GPIO25
GPIO
SPI3 SCLK
40PIN_GPIO11
23
24
40PIN_GPIO8
SPI3 SS0n
GND
25
26
40PIN_GPIO7
SPI3 SS1n
TW1 SDA
40PIN_GPIO0
27
28
40PIN_GPIO1
TW1 SCL
GPIO
40PIN_GPIO5
29
30
GND
GPIO
40PIN_GPIO6
31
32
40PIN_GPIO12
PWM6
PWM7
40PIN_GPIO13
33
34
GND
I2S3 LRCK
40PIN_GPIO19
35
36
40PIN_GPIO16
GPIO
GPIO
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
32e5040000.pwm6
33e5040000.pwm7

List the pwmchip entries under /sys/class/pwm/. Look for e5040000.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=6
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"