computer interfacing tutorial-printer, serial, game, usb port
 

 

BACK

PC Generated Pulse Width Modulation (PWM) For DC Motor Speed Control

by: [email protected]

Figure 1 is a photo of a DC motor controller you can construct in a weekend or two. Motor speed is controlled using pulse-width-modulation (PWM). The key feature of this tutorial is that your PC is used to generate the PWM signal. Thus you can program your PC to contol motor speed in software.

Motivation

PWM is a common technique for speed control. A good analogy is bicycle riding. You peddle (exert energy) and then coast (relax) using your momentum to carry you forward. As you slow down (due to wind resistance, friction, road shape) you peddle to speed up and then coast again. The duty cycle is the ratio of peddling time to the total time (peddle+coast time). A 100% duty cycle means you are peddling all the time, and 50% only half the time.

PWM for motor speed control works in a very similar way. Instead of peddling, your motor is given a fixed voltage value (say +5 V) and starts spinning. The voltage is then removed and the motor "coasts". By continuing this voltage on-off duty cycle, motor speed is controlled.

The concept of PWM inherently requires timing. The classic 555 timer chip and some potentiometers can be used to generate PWM. The pots are manually adjusted for the desired duty cycle. However, if you use a PC, you can automatically change the duty cycle and have your PC control your motor's speed.

One potential application of automatic motor speed is regulating room temperature. Your PC can sense the current temperature (using an analog-to-digital converter) and then automatically increase/decrease the fan's speed accordingly. By using your PC you are no longer burdened to manually adjust the fan's speed as the room heats or cools.

There are many commercial PC motor speed controllers however they tend to be very expensive (hundreds of dollars). This tutorial shows you how to breadboard your own for a fraction of the price. The parts are widely available and inexpensive. The key component is the 8254 timer/counter chip (apx. $4) breadboarded on a ISA protoboard card (apx $15). This card plugs into your PC just like a modem or sound card. A complete tutorial on the 8254 Timer/Counter Card exists on the Boondog website. You need to construct this 8254 card prior to building and programming your DC motor control circuit.

This rest of this PWM tutorial is broken down as follows:

Parts List and Sources

US-based vendors include Jameco, Digikey, JDR and Radio Shack. Note: Boondog has no association with these vendors. The parts list for the 8254 timer/counter card are listed in its tutorial. The motor speed controller circuit requires these additional parts.

 

  • TABLE 1: DC MOTOR SPEED CONTROLLER
    PART DESCRIPTION VENDOR PART PRICE (1999) QTY
    74LS74 D FLIP-FLOP JAMECO 48004 0.29 1
    UDN2993 H-BRIDGE ALLEGRO UDN2993 5.00 1

You can also download the 7474 PDF datasheets.

Allegro Microsystems UDN2993 is a 16-pin DIP packaged dual H-bridge chip that can handle 30 V continous at 0.5 A. An H-bridge is a common way of controlling motor direction. This chip has two H-bridges and the schematic only uses one of them. The UDN2993's enable input pin accepts PWM signals. Although motor direction is not discussed in this tutorial, the UDN2993's phase input can be used to control motor direction.

The UDN2998 is a bigger cousin of the UDN2993 handling 50 V at 3 A continously. It is also available from Allegro. and functions identically. It comes in a 12-pin single in-line (looks similar to a 7805 regulator but fatter) power-tab package.

H-bridge chips are quite common and other alternatives to the UDN2993/2998 exist. STMicroelectronics (also known as SGS-Thomson) makes the L298 dual H-bridge driver which handles 4A at 46V. Alltronics sells the L298 for $9.95. National Semiconductor makes the LMD18200, which is a 3A, 55V H-bridge chip. Digikey, sells it as part number LMD18200T-ND for $23.55

Here are the other H-bridge PDF datasheets:

 

Construction

Schematic


2993PwmSchematic.pdf is the Acrobat file of the same schematic. You will need Adobe's free Acrobat reader to view it.

 

Hightlights

Part placement is not critical for this circuit and can be wirewrapped and/or soldered. There are essentially 3 boards that have to be made. The 8254 Tutorial consists of two: (1) The ISA card that plugs into your PC's motherboard (like a sound or modem card) holds the 8254 IC and (2) a terminal expansion board. These two boards are tethered by a 34-pin ribbon cable. (3) A third board holds the UDN2993 and 7474 flip-flop and connects to the terminal expansion board. You can of course combine the circuits on boards 2 and 3 onto a single breadboard. I just like to modularize things according to function.

Check your circuit before you physically connect your motor up to the third board. If you have an oscilloscope, you can see the PWM signal by executing the code in the Programming section. Figure 2 is a GIF screen shot from an oscillosope. The scope was connected to the output of the 7474 flip-flop (pin 5). The 80% duty cycle can be clearly seen. During one period, the signal is high (+5V) 80% of the time and low (0 V) for the other 20%.

 

Connecting up your motor

The terminal expansion board brings out your PC's +5,-5,+12,-12 volt and ground power supply lines. These lines are fine for powering other TTL circuits. They should not be used to run your motor! This is because even small motors can easily draw currents (surge or continuously) greater than 1A. This is not good for your PC. Instead use a separate motor power supply and tie its ground to the terminal expansion board's ground. The positive lead of your power supply then connects to the Motor Voltage Supply in the schematic.

Figure 1 (right) is a photo of the motor and UDN2993 board. The motor has a black disk mounted on its shaft. I milled/drilled a nice motor stand from plastic to hold the motor. The black disk has one small pencil-sized diameter hole, and works as a simple optical encoder disk. The infrared (IR) encoder/decoder tutorial shows how you can construct your own encoder. The 8254 Timer/counter card can be used to count motor revolutions. This will be explained in a future tutorial.

 

Programming

PWM1_1.C is a Turbo C (for DOS) program that generates the pulse-width-modulated signal seen in Figure 2. The user enters a desired duty cycle and the motor speed runs accordingly. The code also works under a Win3.1 or Win95 under the DOS console window. The first part of the code sets up the necessary variables for the 8254 to work with. A full description of programming the 8254 is found in the the 8254 Timer/Counter Card tutorial. Each counter on the 8254 is 16-bits and requires that the least and most significant bytes (LSB and MSB respectively) are sent sequentially. Variables t0_value and t1_value's LSB and MSB are computed and assigned as t0_valueLSB, t0_valueMSB, t1_valueLSB and t1_valueMSB

Next, the user is prompted to enter the 8254 Card's base address and a desired duty cycle. The baseAddress is used to configure both counters 0 and 1 as rate generators (8254 Mode 2). A rate generator brings an output high (+5V) at a fixed rate. Here, both counter 0 and 1 count down from 10240 (decimal) to 0. Each decrement occurs at the 8254's crystal oscillator frequency of 1.0 MHz or, every 1 microsec. Upon reaching 0, OUT1 will go high momentarily.

After the user enters the base address and desired duty cycle, the user is prompted to either quit, start or enter a new duty cycle. The choice is branched by a switch statement.

If the user doesn't quit, counter 1 faithfully decrements. It's value counterValue is checked against the desired duty cycle (its equivalent in counts), dutyCycleCount . Once they are the same, counter 0 starts counting from 10200 to 0, using the statements:

 
    outportb(counter0, t0_valueLSB); 
    outportb(counter0, t0_valueMSB); 

If the user decides to quite, the timer's gates are released outportb(gateAddress, 0); and counting stops.

In essence what happens is that counter 1 decrements from 10200 to 0. Everytime it reaches dutyCycleCount, counter 0 starts counting from 10200 to 0. By doing this, the 7474 flip-flop's clear (CLR1) and preset (PRE1) value alternate. The output (Q1) then outputs a pulse-width-modulated signal.

Final Words

This tutorial presented a PC interfaced DC motor speed controller that uses pulse-width-modulation. Standalone, manually-adjusted PWM controllers have a long history and are widely available, but rarely are they easy to interface to a PC. A common motivation for a PC interfaced motor speed controller is for automation. However, commercial PC solutions are very expensive ($100-$1000s)

This tutorial shows you how you can easily breadboard your own for a fraction of the price. It is affordable because of the power and flexiblity of the Intel 8254 timer/counter chip. A complete tutorial on this chip exists on this website.

BACK

 

Free Software
Delphi

Lesson 1
Delphi Programming
1.1. IDE Delphi
1.2. Component


Lesson 2
Printer Port
/ LPT
1.1.Basic
1.2.Address
1.3.Port Register
1.4.8 Bit Data Input
1.5.Test Circuitry
1.6.Assignment

Lesson 3
Printer Port / LPT
Experiments

3.1.LED
3.2.Swicht
3.3.Motor Stepper
3.4.DAC
3.5.ADC
3.6.Graph Display

Lesson 4
Serial Port

4.1.Basic
4.2.Hardware
4.3.Port Register

Lesson 5
Serial Port Experiments

5.1.LED
5.2.Stepper Motor
5.3.Swicht
5.4.ADC

Lesson 6
Game Port
Joy Stick
6.1. Basic
6.2. Experiments