PWM control with the Raspberry Pi

September 26, 2013 OpenSystems Media

Pulse-Width Modulation (PWM) is a modulation technique that is widely used to control power. It is commonly used in motor control applications to control the speed of a motor. A Single Board Computer (SBC) such as the Raspberry Pi can be used to generate PWM signals for motor speed control with one limitation: there is only one PWM hardware pin available on the Raspberry Pi. However, using an Arduino’s PWM-capable I/O pins, one can find a feasible solution for not just one, but multiple PWM outputs.

PWM overview
A PWM signal is a square waveform and is used in electronic control systems to control things like a servos position, motor speed, and brightness of an LED. It does this on the basis of two important parameters: frequency and duty cycle. Frequency is the number of repeating cycles a waveform makes in one second. Duty cycle represents the percentage of time that a signal is high versus low. The resulting average value of the waveform sets the amount of power delivered to the load.

Raspberry Pi and Arduino
The Raspberry Pi is a credit card-sized microcomputer that has garnered quite a bit of attention thanks in part to its computing power, size, and price. The Raspberry Pi is based on the Broadcom BCM2835 embedded multimedia applications processor. It comes equipped with multiple connection ports and can be easily operated by plugging into a television or monitor and a USB keyboard. It is also compatible with many operating systems; however, Raspbian, a version of Linux tailored to the Raspberry Pi, is one of the most common operating systems in use.

Arduino is a single-board microcontroller-based platform that gives inexperienced and experienced users alike an easy and quick start in embedded electronics design. It is an open source hardware design that provides analog-to-digital signal conversion capabilities as well as input and output control and PWM signal generation. Arduinos also provide an abundance of connectivity options through its onboard I/O connections and available shields.

PWM on the Raspberry Pi: Limited controlling capability
Consider a task that involves building a robotic car where you are required to control two motors via Raspberry Pi, which has 26 General Purpose Input/Output (GPIO) pins. Out of these pins there is only one easily accessible PWM output pin, which is insufficient to drive anything that requires more control than a mere on/off function. Servo motors and RGB LEDs cannot be driven through this pin alone.

Strategies to enhance PWM capabilities of the Raspberry Pi
Numerous solutions have been presented to overcome this limitation of the Raspberry Pi. A lot of people use an I2C (Inter-Integrated Circuit) or SPI (Serial Peripheral Interface) enabled accessory for this purpose. These devices are controlled by the Raspberry Pi and they generate PWM signal for controlling purposes. A PWM generator accessory can also be used to add PWM-capable output pins to the Raspberry Pi.

Another approach being used is doing software PWM on other GPIO capable pins. The WiringPi library is an example library that allows for this type of control. It uses a real-time scheduler to prioritize the generation of PWM signals by running individual threads, but since the operating system that is used on the Raspberry Pi performs multitasking, this solution is not suitable because it provides low timing resolution and high jitter.

Arduino: An appropriate solution
One solution for easily developing PWM signals is through the use of an Arduino development kit. Arduino boards come in multiple configurations. For instance, the Arduino Due has 54 digital pins (of which 12 are available for PWM output) and 14 analog pins. With this many configurable I/O pins, the Arduino Due, and Arduino boards in general, are ideal candidates as an external sensing and control board to interface with the Raspberry Pi.

Interfacing Arduino and Raspberry Pi via USB
A solution to having robust PWM capability with a highly configurable user interface can be found in the form of mating the Raspberry Pi and Arduino platforms. An Arduino-based board can be linked with the Raspberry Pi to add the functionalities of a PC, the communication and multimedia tools of the web, the capability to interact with the environment through the use of a microcontroller, and the ease of transportability of mobile devices. As noted earlier, Arduinos are a microcontroller-based platform that are ideal for running code for applications that involve sensing and control. A very convenient, low-cost and reliable approach to interface the Raspberry Pi and an Arduino board is through a USB cable. This is a convenient solution because it requires a minimal amount of hardware. Moreover, it offers motor and power control in general with a precise real-time response, such as providing high precision and low jitter, which is important for many control applications.

By using the USB interfaces on each board as well as the readily available pySerial library, one can send commands from the Raspberry Pi to control the Arduino’s multiple I/Os. And, in reverse fashion, serial data can be sent back to the Pi from the Arduino when it reads multiple data types such as temperature, speed, or other external data that needs to be monitored.

About Cliff Ortmeyer
Cliff Ortmeyer is the Technical Marketing Manager for Newark element14, a division of Premier Farnell. He has over 20 years experience in the semiconductor and electronics design industry in various capacities including FAE, Market Development and Technical Marketing. Cliff also holds multiple patents in both analog and digital design.

Cliff Ortmeyer, Newark
Previous Article
CES 2017: Bosch XDK turns sensors into solutions

Why do I go to CES every year? That's a question I'm sometimes asked by technologists in the embedded space...

Next Article
Embedded memory systems 101
Embedded memory systems 101

Memory subsystems needn't require a significant learning curve. The complexity of today's embedded systems ...


Follow our coverage of hardware-related design topics with the Hardware edition of our Embedded Daily newsletter.

Subscribed! Look for 1st copy soon.
Error - something went wrong!