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 33 for PWM and pin 6 as ground for the oscilloscope.

3.3 V
1
2
5 V
I2C0 SDA
40PIN_GPIO2
3
4
5 V
I2C0 SCL
40PIN_GPIO3
5
6
GND
GPIO4
40PIN_GPIO4
7
8
40PIN_GPIO14
UART2 TX
GND
9
10
40PIN_GPIO15
UART2 RX
GPIO0
40PIN_GPIO17
11
12
40PIN_GPIO18
PCM BCLK
GPIO11
40PIN_GPIO27
13
14
GND
GPIO74
40PIN_GPIO22
15
16
40PIN_GPIO23
GPIO72
3.3 V
17
18
40PIN_GPIO24
GPIO69
SPI2 MOSI
40PIN_GPIO10
19
20
GND
SPI2 MISO
40PIN_GPIO9
21
22
40PIN_GPIO25
GPIO73
SPI2 SCLK
40PIN_GPIO11
23
24
40PIN_GPIO8
SPI2 CS
GND
25
26
40PIN_GPIO7
GPIO6
I2C2 SDA
40PIN_GPIO0
27
28
40PIN_GPIO1
I2C2 SCL
GPIO1
40PIN_GPIO5
29
30
GND
GPIO21
40PIN_GPIO6
31
32
40PIN_GPIO12
PWM2
PWM1
40PIN_GPIO13
33
34
GND
PCM FS
40PIN_GPIO19
35
36
40PIN_GPIO16
GPIO9
GPIO10
40PIN_GPIO26
37
38
40PIN_GPIO20
PCM DI
GND
39
40
40PIN_GPIO21
PCM DO
PowerGroundGPIOI²CAudioPWMSPIUART

Set an Output​

Header pinPWM controllerLinux PWM channel
3210048000.pwm2
3310048000.pwm1

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