Home / Tutorials / Raspberry Pi Tutorial / Raspberry Pi PWM and Servo Motor Tutorial
Raspberry Pi PWM Servo Motor example

Raspberry Pi PWM and Servo Motor Tutorial

For the next part of series on Raspberry Pi, I will show how to produce a PWM signal from a pin. Moreover, I will show you how to control single and multiple servo motors in this Raspberry Pi PWM tutorial.

Raspberry Pi PWM Pin

Strictly speaking, there is only one user accessible PWM pin on all Raspberry Pi models and it’s GPIO18.

Raspberry Pi PWM Pin

This pin can be used using the same RPi.GPIO Python module I used in the previous tutorial.

Similar to using any GPIO pin, we must first import the GPIO module:

Then, the pin name convention to be used must be set up:

Next, the pin should be declared as output:

After that, we create a PWM object like this:

Where 1000 is the PWM frequency in Hertz. The PWM signal appear on the pin only after this line is called:

Where 50 is the duty cycle of the PWM signal.

You can stop the PWM signal using:

Also, you can change the frequency once it’s running using:

Furthermore, you can change the duty cycle using:

Fading a LED

We can use PWM to fade a LED connected to GPIO18 using this diagram:

Raspberry Pi PWM Fade LED example

This will be the Python script for fading the LED:

There are some new things on this script not found on my previous tutorial. Of course, import RPi.GPIO is required while import time is used for introducing a time delay.

I used a try-except syntax for a more orderly program exit. When the program detects CTRL+C or KeyboardInterrupt, it exits the while loop and then stops the PWM. Finally, it proceeds to cleanup the used GPIO pin.

Inside the while loop are two for loops:

The first for loop increments the duty cycle by 5 starting from 0 to 100 (101 is not included in the loop). This continually increases the intensity of the LED. The second for loop decrements the duty cycle by 5 from 100 to 0. Similarly, this decreases the intensity of the LED.

Use PWM to Control a Servo Motor

A servo motor is controllable through PWM. Specifically, its arm position depends on the width of the pulse applied to it. This diagram summarizes how a servo motor is controlled through PWM.

Image result for servo motor pwm

Centering the servo arm requires a 1.5 ms pulse width. Furthermore, if we are to produce this using Raspberry Pi PWM, we must consider both the duty cycle and frequency of the signal.

A 1.5 ms PWM signal can be produced with a frequency of 500 Hz and a 75% duty cycle:

Increasing the duty cycle increases the pulse width, ending with 2 ms at 100% duty cycle. Similarly, the duty cycle can be decreased up to 50% for a 1 ms pulse width. That covers the range of the pulses for controlling the servo motor!

I will be using this diagram for the next example script:

Raspberry Pi PWM Servo Motor example

Here, I used servo as the name of the PWM object attached to GPIO18 with frequency of 500 Hz.

Then to sweep the arm back and forth, I used to same for loop as the fading LED example, only that I changed the range of duty cycle from 50 to 100 and then back to 50:

The rest is just like the fading LED script. That’s it!

Multiple Raspberry Pi PWM Pins

The problem with using the RPi.GPIO library is that it’s restricted to GPIO18 only. What if I want to use multiple PWM signals?

Thankfully, an external Python library named pigpio exists. This library allows us to use any GPIO pin for PWM.

To install pigpio, use these commands:

Pigpio uses a daemon called pigpiod. This daemon must be running in the background for us to use the servo functions of the library. Thus, we must run the daemon once the whole pigpio library is installed using:

After that, we can now freely use all the pigpio functions in the python terminal or in a python script.

Now to use the pigpio library, we need to import it:

And then declare an object using whatever name you like. For example:

Generating a pulse only requires this function:

Where 18 is the GPIO pin number and 800 is the pulsewidth in microseconds. This is how to center a servo motor:

Again, you can use this function with any GPIO pin. So all of these will generate PWM signals on the specified pin:

You can checkout my Hexapod Walker Project where I attached four servo motors to my Raspberry Pi using pigpio!

On my next tutorial, we’ll look at how to use USART with the Raspberry Pi.

Check Also

Raspberry Pi Zero Bluetooth

Setting Up Raspberry Pi Zero Bluetooth

The Raspberry Pi Zero W comes with both WiFi and Bluetooth connectivity. I have already …

Leave a Reply

Your email address will not be published. Required fields are marked *