The PWM Kernel Module

Adafruit and Sean Cross have created a kernal 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.

You used the PWM and Servo Kernel Module in Lesson 8, to control a Servo. This time, you will use the same module to control the speed of the motor.

The modules interface 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/

File

Description

active

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.

delayed

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

mode

Write to this file to set the mode of the pin to either pwm, servo or audio. Obviously we want it to be pwm. 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 motor.

frequency

This is the number of pulses to be produced per second.

duty

The value here needs to be between 0 and 100 and represents the percentage of the pulse during which the power to the motor is on. The higher the number, the faster the motor will spin.

This guide was first published on Jan 14, 2013. It was last updated on Jan 14, 2013. This page (The PWM Kernel Module) was last updated on Aug 17, 2019.