Homepage Wiki Forum Buy



severity (level) requirement Gnublin family
Gnublin logo easy.png Kernel compile, Toolchain All

Pulse-width modulation serves possibilities vor different areas. Areas of operation are: Create analog voltages, digital amplifier, servo controlling etc. How PWM works, you can read e.g. in Wikipedia [1].

You can fint the interfaces of the different GNUBLIN boards and their location here


Technicall specification

  • 12 Bit counter
  • Adjustable output frequenzy
  • Programmable Pulse-width modulation
  • Supports pulse density modulation (PDM)
  • Endless-mode

PWM with gnublin-pwm

To easily interact with the PWM interface of Gnublin we wrote a small program called gnublin-pwm. An example call looks like this:

gnublin-pwm -v 0x400

The value after "-v" sets the Duty-Cycle, where 0x000 is a Duty-Cycle of 0% and 0xfff a Duty-Cycle of 100%.

If the clock source of 1400Hz is too fast you can set a divider with the following command:

gnublin-pwm -c <Divider>

Where divider could be one of the following:

1    (Clock-Divider off)
2    (half Clock)
4    (fourth Clock)
8    (eighth Clock)

PWM via console (Debian)

In the newest version of the Debian Distribution (20.2.13) you can find the PWM driver in a revised version:

Loadin the module (if not integrated in the kernel):

modeprobe lpc313x_pwm

The 12-bit PWM value will be send as a chain of three Hex-values to the device file lpc313x_pwm:

echo fff > /dev/lpc313x_pwm

This command set the duty-cycle to almost 100%.

If the clock source of 1400Hz is too fast you can set a divider:

echo clk1 > /dev/lpc313x_pwm (Clock-Devider off)
echo clk2 > /dev/lpc313x_pwm (half Clock)
echo clk3 > /dev/lpc313x_pwm (fouth Clock)
echo clk4 > /dev/lpc313x_pwm (eighth Clock)

PWM via console (ELDK)

The 12-Bit PWM value will be sent as a chain of two bytes:

root@armv5te:~# echo <lowbyte> <highbyte> > /dev/lpc313x_pwm

The driver is described here:


PWM via Python and ELDK

PWM demo for Gnublin LPC3131 You can find this example in the directory /root/examples (Debian) under the file name pwm.py

Python module for comfortably use of PWM output:

import time

def d2s(v):
    ''' v is between 0 and 4095 '''
    b0 = v & 0xff
    b1 = (v & 0xfff) >> 8
    return chr(b0) + chr(b1)

def pwm_raw(v):
    if v < 0 or v > 4095:
        print "v is out of range"
    p = open("/dev/lpc313x_pwm", "wb")
    s = d2s(v)

def pwm(r):
   '''Set PWM output to 0.0% <= r <= 100.0%'''
   v = int( r/100.0 * 4095 )

if __name__ == "__main__":

On the shell prompt make the following input (if you use the standard GNUBLIN SD-Card you can skip this step):

root@armv5te:~# python Python 2.6.6 (r266:84292, May 20 2011, 22:44:14) [GCC 4.5.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pwm >>> pwm.pwm_raw(1000) [lpc313x pwm debug message] pwm to 1000 (24%)


>>> pwm.pwm(50)
[lpc313x pwm debug message] pwm to 2047 (49%)


>>> pwm.pwm(1)
[lpc313x pwm debug message] pwm to 40 (0%)


>>> pwm.pwm(99)
[lpc313x pwm debug message] pwm to 4054 (98%)


If pwm was used as a module, so you can also remove the module after use:

root@armv5te:~# rmmod lpc313x_pwm
[lpc313x pwm] cleanup
In other languages