delay_us(100) // delay 100 micro sec
DC motor as it was intended, we turn the motor on in
forward or reverse, or place it in the brake condition to stop
it. We can produce different speeds by pulsing the ON
states with different duty cycles. These capabilities are easily
accomplished by the function in Figure 2. A value of 1 for
dir produces forward movement, while -1 indicates reverse.
The speed parameter is a percentage (0-100) of the
Note that the SmallDC() function produces only one
pulse, so it must be called continuously to produce the
desired movement. (More on this shortly.) The 100
microsecond delay in the for-loop ensures that the total
time spent in the loop is 10,000 microseconds (100*100).
The motor is always turned ON in the proper direction
before the loop starts, and then is turned off when the
proper percentage of on-time is reached. If this routine is
called continuously, the motor will be pulsed 100 times per
second creating a smooth rotation.
The techniques shown for small DC motors are well
understood by most seasoned hobbyists, but let’s see how
we can use this same hardware to pulse a servomotor.
68 SERVO 08.2012
Servomotors are controlled by the width of a 5V pulse
applied to the control line. In general, a width of 1,500
microseconds will cause the motor to stop. As the pulse
increases toward 2,000 microseconds, the motor will move
forward faster and faster. Decreasing the pulse toward
1,000 microseconds also moves the motor faster, but in
reverse. The next step is to see how we can produce 5V
pulses with the H-bridge circuitry.
It is very important that we do NOT use the ON
condition of the existing hardware because the voltage
used to power the H-bridge — and thus the motors — is
usually larger than 5V. We can, however, use the COAST
state along with a pull-up resistor to produce a five volt
pulse for controlling a servomotor; this is shown in Figure
3. Notice we are using the right motor-lead connection to
drive the servomotor, but either motor-lead will work fine as
both will react identically to our servomotor software.
The software needed to produce the appropriate pulse
for a servomotor is shown in Figure 4. It starts by
calculating the necessary pulse width (pw) by effectively
adding or subtracting up to 500 (based on the current
value of speed) from the off-time of 1,500. When the
motor is set to COAST, the output will go high to 5V and
stay there for the time calculated. The BRAKE condition
terminates the pulse by bringing the output line to zero.
Finally, a delay is used to ensure that the entire process
takes 20 ms which will produce 50 pulses per second when
this function is called continuously.
It is worth mentioning that some continuous-rotation
servomotors we have used reach their peak speeds at pulse
widths well inside the normal limits of 1,000 and 2,000
microseconds. This is easily corrected by reducing the
multiplier of five in the first line of the Servomotor()
function in Figure 4. Just experiment to find what is right
for your situation.
Large DC Motors
The TB6612FNG motor controller on the Baby
Orangutan can only handle enough current for small
motors. This is true for nearly all IC controllers because of
an IC’s limited ability to dissipate heat. In order to power
larger motors, we would need an H-bridge built with high
current MOSFET transistors. The RoboClaw motor controller
from Basic Micro shown in Figure 5 not only has a high
The soon to be released RobotBASIC Robot Operating
System is designed to make building a robot easier than ever.
It provides both the hardware connections and the software
drivers for interfacing a wide variety of motors and sensors
with RobotBASIC. The interface itself is just the beginning
though, because, as with any operating system, the RROS truly
manages the resources being controlled. For example, it can
automatically interface with and control all the motor types
described in this article using ramping, wheel encoders, and
much more. If you are curious about the features of our RROS,
a draft of the User's Manual can be downloaded from