The PWM and Servo Kernel Module

Author Gravatar Image SIMON MONK

Adafruit and Sean Cross have created a kernel module that is included with the Occidentalis distribution. For details of creating an Occidentalis follow this link. If you want to use the module with Raspbian or some other distribution, then there is help on installing the kernel module into your environment here.

The module is called PWM and Servo because as well as controlling servo motors, the module can also produce PWM (Pulse Width Modulation) signals that can be used (with extra electronics) to control the power to motors or lights. We will not be using the PWM feature in this lesson.

The PWM and Servo Module uses a file type of interface, where you control what the output pin and therefore the servo is doing, by reading and writing to special files. This makes it really easy to interface with in Python or other languages.

The files involved in using the module to drive a servo are listed below. All the files can be found in the directory /sys/class/rpi-pwm/pwm0/ on your Raspberry Pi.




This will be 1 for active 0 for inactive. You can read it to find out if the output pin is active or write it to make it active or inactive.


If this is set to 1, then any changes that you make to the other files will have no effect until you use the file above to make the output active.


Write to this file to set the mode of the pin to either pwm, servo or audio. Obviously we want it to be servo. Note that the pin is also used by the Pi's audio output, so you cannot use sound at the same time as controlling a servo.


Write to this file to set the maximum value for a servo position. We will set this to be 180 so that we can easily set the servo to anu angle between 0 and 180.


The value that you write to this file sets the servo pulse length in relation to servo_max. So if we set it to 90, with a servo_max of 180, it will set the servo to its center position.

Last updated on 2014-04-05 at 07.22.28 PM Published on 2013-01-09 at 02.22.53 PM