PWM Motor Control with BealgeBone Black & DRV-8833 (Ubuntu 16.04, Kernel 4.4)

Motor control is one of the basic tasks in any robotics development. Pulse-Width-Modulation (PWM) is often used to control DC motors or servos. This post describes how to drive PWM signals from BeagleBone Black to control a DC Motor. Also, we’ll see how to use encoder readings to calculate speed & rpm ranges of the motor.

A better but expensive way to control motors is to use a smart actuator like Dynamixel. I’ll write about smart actuators in a different post. Smart actuators are finding more and more ground in robotics for their ease-of-integration and clutter-free designs. Having said that, most hobbyists still start with PWM control.

Adafruit BBIO

Adafruit BBIO is an extremely useful, thus popular, Python IO library for accessing BeagleBone Black IO pins. Install it.

ePWM Modules

TI-3358 Sitara microprocessor on BeagleBone Black has 3 instances of Pulse-Width Modulation SubSystem (PWMSS). Each PWMSS in turn has an instance each of ePWM (enhanced PWM) module and eQEP (enhanced Quadrature Encoder Pulse) module.

ePWM or eHRPWM (high-resolution) modules can be programmed to drive PWM signals on BeagleBone IO pins. The PWM IO pins are usually connected to a Motor Driver chip that in turn drives DC motor(s).

Each ePWM has two outputs – EPWMxA, EPWMxB where x is the ePWM instance id on the Sitara device. Those two outputs together is a PWM Channel. However, both outputs can be used independently. The only catch is that both should operate on the same PWM frequency (at least with BBIO).

PWM IO Pins

PWM InstanceOutput-AOutput-B
ehrpwm0P9_22P9_21
ehrpwm1P9_14P9_16
ehrpwm2P8_19P8_13

eQEP Modules

The magnetic (wheel) encoders are fitted to the motor shaft and rotate along with the shaft. The circuit associated with encoder sends rotation count pulses to BeagleBone IO pins. eQEP modules on BBB can be programmed to count incoming pulses.

eQEP readings can then be used to calculate motor/wheel speed. Quadrature encoding requires two channels to determine the direction of the motor rotation. Hence, each eQEP requires two IO pins of BBB.

eQEP IO Pins

eQEP InstanceInput-AInput-B
eQEP0P9_42P9_27
eQEP1P9_35P9_33
eQEP2P8_12P8_11

DRV-8833 H-Bridge Motor Driver

DRV-8833 connection to Micro-controller (BeagleBone Black) and Motors (Credits: Pololu)

DRV-8833 is a dual H-bridge motor driver IC capable of bi-directional control of two DC motor simultaneously. It’s an ideal driver to Pulse-Width modulate low-voltage DC motors.

Inputs xIN1 & xIN2 are connected to PWM-A & PWM-B outputs of BBB. Outputs xOUT1 & xOUT2 are connected to the two motor leads. VIN of DRV-8833 is the supplied with rated motor voltage.

Below table from DRV-8833 data sheet shows how different input combinations control motor direction and H-bridge functioning:

xIN1xIN2Function
PWM0Forward PWM, fast decay
1PWMForward PWM, slow decay
0PWMReverse PWM, fast decay
PWM1Reverse PWM, slow decay

Example Connections

As an example, to control forward speed of a DC motor in fast decay mode (first configuration in above table) you will have to make connections as shown in table below:

BBB PinBBB Pin ConfigConnected ToPurpose
P9_22 (Out)pwmDRV8833 BIN1Forward PWM, fast decay
P9_21 (Out)gpio/LOWDRV8833 BIN2Forward PWM, fast decay
P9_42 (In)eqepEncoderB-1Encoder Channel 1
P9_27 (In)eqepEncoderB-2Encoder Channel 2
P9_1gpiogndCommon Ground

Also, connect VIN & GND pins of DRV-8833 to motor voltage source and ground respectively.

PWM Programming (Python)

In this section and the next, we’ll see how to use Adafruit BBIO library, BeagleBone Balck & DRV-8833 hardware to control speed of DC motor using PWM.

First, let’s implement some wrapper classes to facilitate PWM and eQEP operations.

pwm_ops class objects need two PWM IO pins. start() function takes three arguments: duty cycle (0-100), PWM frequency and polarity (not used in this example). eqep_ops class objects need eQEP module name. read() function returns encoder position at the time of reading.

Now, let’s implement another class do a few interesting things:

  • Drive a range of PWM frequencies at specific duty cycle.
  • Sweep across a range of duty cycles at fixed frequency.
  • Drive each PWM setting for a specified amount of time.

PWM Frequency Sweep

Let’s control motor speed by changing PWM frequency from 100 Hz to 4000 Hz in steps of 100 Hz at 50% duty cycle. We’ll let the motor run for 4 seconds at each frequency.

PWM Frequency Sweep
PWM Frequency Sweep

We can see that the speed of the motor actually decreases at higher PWM frequencies. This is because PWM signals at higher frequencies give lesser time for current to rise to its peak value.

PWM Duty-cycle Sweep

Another way to control motor using PWM is to change PWM duty-cycle at a fixed frequency. At a PWM frequency of 200 Hz, let’s sweep the duty-cycle range (0-100) in steps of 5 – again, each for 4 seconds.

PWM Duty Cycle Sweep showing Motor RPM and Wheel Speed.  PWM control of DC motor with BeagleBone Black and DRV-8833
PWM Duty Cycle Sweep at fixed PWM frequency (200 Hz)

As expected, motor runs fastest at 100% duty-cycle.

Bottom line, you should consider an RPM sweet-spot for your DC motor based on both above plots. In applications where acceleration/deceleration is permitted, use above plots to determine a decent velocity/rpm range for the motor. Further experiments could include:

  • A different decay mode of DRV-8833.
  • PWM control of forward and reverse motor directions.
  • PWM control of a differential (dual) motor system.

Robotics & AI Enthusiast

Leave a reply:

Your email address will not be published.