You are on page 1of 7

10/21/11

DC Motor Speed Control

Go Login Register

F H R A T FAQ L E

A
Electronics

Search ... Go Ne
28, 2005 (GST)

DC MOTOR SPEED CONTROL


P M ,M

Articles

Free Robotic Resource O M :// D :// . S . : . / . R . R , :

This tutorial aims to show you how to control a DC motor though an H-bridge driver chip using the PWM output from a PIC to achieve speed control. Although I am using a PIC microcontroller other controllers such as AVR s, Basic Stamps and certain Picaxe s can be used.

Before we get too far along; PWM stands for pulse width modulation. For a given frequency the logic level of the output from the pic will be high (or on) for a certain percentage (duty) of the period (1/frequency in seconds). So with a duty of 50% the PWM signal would be high for 50% of the time. This can be used to create average voltages (i.e. with a PWM signal at 5V half of the time and 0V the rest the average voltage would be 2.5V), by varying the voltage to the motors you can control the speed of the motor!

Comabt robot parts I .

Electrical fle ible conduit and fittings for robot iring ,LT

Here is a basic circuit which does have problem, but it s a good place to start as it shows a common way to use PWM to control speed. Most Popular Articles
DC Motor Speed Control A IC PIC H-B DC .

Robot Sonar A .A 4.

Quick Start Guide to Microcontrollers Q S G M . ,

The operation of the H-Bridge driver chip is fairly simple. With the enable pin you can turn the bridge on and off, the input pins you can change the direction of the voltage across the motor. So with the enable high and one input pin high and one low the motor will turn one way and with them the other way round the motor will go in the opposite direction. If they are both at the same level the and the enable is high then the motor will stall (or break) and with the enable low the motors will freewheel.

.robotbuilder.co.uk/Resources/Articles/152.asp

There are also current sense pins which can be used to identify stalling and excessive current consumption when a resistor is connected to GND, and the voltage across the resistor will be follow V=IR so with a known R the current I can be found. The power rating of the resistor is very important, P = I^2*R so you must make sure your resistor can handle the stall current of your motors. The ones I am currently work with are rated at 2A stall current, making 4W with a 1Ohm Resistor

1/7

10/21/11

0.4W GND

0.1O !

2A .B

DC Motor Speed Control

4W ,

1O

T 2 +5V T GND

, < 46V .

.L .

VERY IMPORTANT 2 .S + + ). GND. EMF / .B

!T RC.

.M , PWM

.I

PWM .

H
.robotbuilder.co.uk/Resources/Articles/152.asp

100O ). I

( I , GND
2/7

10/21/11

DC Motor Speed Control

+5V

0V PWM

DC

A T

GND PWM

, , .T ,

0V.

VERY

T .S
.robotbuilder.co.uk/Resources/Articles/152.asp

, 50% 50%
3/7

10/21/11

again. So even if the duty is 50% the actual motor speed will not be 50% of full speed.

DC Motor Speed Control

So I made a small improvement:

Using this circuit much more control over speed is possible. To start with the enable line is used to control the H-Bridge being on or off; this is done with a logic line from the PIC rather than a PWM signal. The PWM signal is used on one of the directional pins, with the other having a HEX inverter to invert the signal. This means that the motors are never unintentionally in freewheel mode. The only down side is that to achieve a full stop the PWM signal has to be put at 50%, this does mean that stopping will be prompt, but it will be similar to stalling the motor with the same high current consumptions.

Here is a scope reading with a resistor in place of the motor:

And here is another with the Maxon motor:

As you can see, it s much, much improved on before!


.robotbuilder.co.uk/Resources/Articles/152.asp 4/7

10/21/11

As you can see, it s much, much improved on before!

DC Motor Speed Control

Ok so onto the firmware. This was tested using my micromouse main board, a breadboarded L298 and a variable resistor. The micromouse main board uses a 18F452 at 16Mhz. So with this code snippet you should be able to control a motor s speed, it is meant for use in the BoostC compiler which can be downloaded from www.picant.com.

void set_pwm( unsigned short pwm_val, bit module );

void main () {

portc = 0x00; trisc = 0x80; portd = 0x00; trisd = 0x00; set_bit( portd, 0); clear_bit( portd, 1); pr2 = 0x7C; t2con = 0x06; ccp1con = 0x0C; set_pwm( 0x01F4, 0 );

// Clear PortC // 1 = Input, 0 = Output // Clear PortD // 1 = Input, 0 = Output // Set motor direction // Set motor direction // Set period F9 // Set timer2 1:16 prescaler // Set CCP1 Config

void set_pwm( unsigned short pwm_val, bit module ) {

char twobit; union { unsigned short sht; char ch[2]; }temp; temp.sht = pwm_val; twobit = temp.ch[0] & 0x03; temp.sht = temp.sht >> 2;
.robotbuilder.co.uk/Resources/Articles/152.asp 5/7

10/21/11

DC Motor Speed Control

if( !module ) // i.e. module 0 { ccpr1l = temp.ch[0]; if( twobit & 1 ) set_bit( ccp1con, 4); else clear_bit( ccp1con, 4); if( twobit & 2 ) set_bit( ccp1con, 5); else clear_bit( ccp1con, 5); } if( module ) // i.e. module 1 { ccpr2l = temp.ch[0]; if( twobit & 1 ) set_bit( ccp2con, 4); else clear_bit( ccp2con, 4); if( twobit & 2 ) set_bit( ccp2con, 5); else clear_bit( ccp2con, 5); } }

This is designed to work with a 16Mhz clock with a PWM at 2Khz with a 100% duty. By using the equations in the datasheet or this program:

[Program link] - To be added ASAP

It needs the .net framework to run (sorry, but it was originally for use by me, so I used what I had handy). Please check the timer and ccpcon settings in the datasheet!

So now you should have no worries about PWM control, its not that hard .

.robotbuilder.co.uk/Resources/Articles/152.asp

6/7

10/21/11

DC Motor Speed Control

PS. An image of m setup - bus but it worked!

Average Rating: Add Your Comment

Comments:

Flat

Oldest First

RE :H BRIDGE

B eeefriend on Wednesda

, April 26, 2006 (GST) diag am<b >

he e plana ion i good,<b > b if e plain abo he PWM ope a ion i h he ci c of PIC, hen i ill be effec i e ,<b > h i con e e called H b idge.<b ><b > eeef iend<b >

Repl to this Comment


RAVI ABA D AAN MAGARIA

B lomsika on Thursda
o ogo a a <b >

, June 17, 2010 (GST)

Repl to this Comment

.robotbuilder.co.uk/Resources/Articles/152.asp

7/7

You might also like