Menu

How to use the PWM interface in Raspberry Pi

By Surya Igor | Internet of Things

Mar 13
RPi-PWM-FeatureImg

PWM(Pulse Width Modulation) is a widely used technique in many electronic circuits and device functions. In this blog post, we discover how to use Raspberry Pi’s PWM interface to control a servo motor.

Basics of Pulse Width Modulation [PWM]

PWM is the technique of controlling the power that is fed to an electronic device, in order to control certain parameters, such as the speed of the motor or the intensity of an LED. A PWM signal is characterized by duty cycle which is the percentage of “on time” out of the entire time duration of the pulse.  A low duty cycle corresponds to low power and a 100% Duty Cycle corresponds to a situation when the device consumes the maximum power.

PWM Duty Cycle

 

This technique can be used to encode information also, but in this post we will see how to control a servo motor using this PWM technique. And yes, we will use the popular Raspberry Pi for this.

Servo Motor

A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration. It consists of a suitable motor coupled to a sensor for position feedback. It also requires a relatively sophisticated controller, often a dedicated module designed specifically for use with servo motors. (Source,  Wikipedia)

Servo motor Types

Raspberry Pi

Raspberry Pi Applications

Raspberry Pi is a popular, credit card sized computer which has rich peripherals to interface various sensors. The Raspberry Pi packs a lot of hardware interfaces such as serial communication SPI, UART, I2S, I2C, etc. Apart from that, it also has standard GPIO pins and can support analog signal interfacing with the help of external ADC.

Hardware Schematic for PWM Interface

Here is how the Raspberry Pi 3 pin configuration looks like.

Raspberry Pi Pin Diagram

Check the hardware connections for PWM as shown in the Raspberry Pi 3 pin configuration and the connection diagram below. Note that we are using the GPIO No. 26 for PWM.

Hardware Requirements

You will need the following hardware to execute this project.

 

The schematic looks like this.

Hardware Schematic

And here is a detailed look at the wires for the servo connection.

Servo Connections

Which leads to the actual hardware setup as follows.

Servo Motor Circuit

Servo Motor Operation

I have written a small python script to demonstration the rotation control of servo motor.

In this demo, the servo is rotated from the selected position and brought back to the initial position by varying duty cycle. These position are defined with the PWM value to be set within the python script.

# PWM Value: 7.5
servo.ChangeDutyCycle(7.5)

The formula for Selecting the rotation angle based on duty cycle is
{DutyCycle = 1/18* (DesiredAngle) + 2}, where DesiredAngle is in degrees.

Software

Follow the procedure below to run the python program.

  • Log on to the Raspberry pi console and clone this GitHub repository under your home directory.
  • Under the “rpi-series” directory navigate to “pwm_servo” subdirectory.
cd rpi-series 
cd pwm_servo
  • Run the script
python servoPWM.py
  • You can see the servo motor rotating as per the PWM value set in the python script.

 

Servo Working

Take Charge!

I encourage you to try your hands on this demo. You could also try this with a LED and observe the intensity of the LED light with the varying PWM duty cycle. I will get back to you very soon with a tutorial post on yet another hardware interface supported by Raspberry Pi.


Disclosure: * Denotes an affiliate link – if you click and make a purchase we may receive a small commission.

Follow

About the Author

Surya is a hardware buff and tinkerer who loves all the stuff around IoT. He is a specialist in building proof-of-concepts around electronic hardware and conceptualizing use cases and solutions for Internet of Things. When not working with hardware, Surya can still be found fiddling with some crazy hardware design, trying to bring out an idea out of a raw circuit.