Login
Register
Robot Builder Articles
 
 


DC Motor Speed Control

Posted by on Monday, March 28, 2005 (GST)

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!

 

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.

 

Schematic One

 

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.

 

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 or 0.4W with a 0.1Ohm resistor. But if you do not need this, just connect these pins to GND and all will be fine!

 

There are 2 voltage inputs for the driver, logic and motor supply. Logic should be +5V and supply should be < 46V and will be the voltage supplied to the motors. The GND of both supplies should be connected.

 

One other VERY IMPORTANT set of components that need to be added are the diodes to protect against back EMF which can damage your circuit. Basically on each motor terminal 2 high voltage / current diodes should be placed with reverse bias. So on each terminal there should be a diode with the negative side connected to the +ive supply and one with the +ive side connected to the GND. (please see the datasheets for more detailed info).

 

And here is a quick pic of the motors i used, erm, motors and resistor! The small motor is the expensive maxon and the larger one is a cheapo from a old toy RC.

 

motors1.jpg

 

Ok so speed control. My first attempt used the following system. I used a PWM signal on the enable pin, the idea was that the PWM would in affect turn the motors on and off very fast resulting in a average voltage across the motors.

 

Schematic Two

 

Here is a scope image when using a 100Ohm resistor in place of a motor (there is no back emf or noise to worry about). It is as I expected, the GND is not constant due to the characteristics of the transistors in the driver and one channel shows a +5V to 0V PWM signal.

 

 

Method 1 - Resistor

 

But here is one using a high quality DC motor from Maxon.

 

Method 1 - Motor

 

As you can see the GND channel is okay, but the other channel never goes to 0V. This is because the PWM signal is off in these periods, according to the truth table in the datasheet this means that the motor will be in freewheel mode. This would be a iron core passing through a magnetic field, so a voltage will be induced, this is what is being shown here.

 

And just for the record, here is a VERY cheap motor, very very very bad signals!

 

cheap.jpg

 

 

This means that speed control is not very accurate, as it would be more akin to a car coasting than the car going a distance then stopping then going a distance again. So even if the duty is 50% the actual motor speed will not be 50% of full speed.

 

So I made a small improvement:

 

Schematic Three

 

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:

 

 

r2.jpg

 

And here is another with the Maxon motor:

 

s2.jpg

 

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

 

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;               // Clear PortC

      trisc = 0x80;                // 1 = Input, 0 = Output

      portd = 0x00;               // Clear PortD

      trisd = 0x00;                // 1 = Input, 0 = Output

      set_bit( portd, 0);          // Set motor direction

      clear_bit( portd, 1);       // Set motor direction

      pr2 = 0x7C;                 // Set period F9

      t2con = 0x06;              // Set timer2 1:16 prescaler

      ccp1con = 0x0C;          // Set CCP1 Config

      set_pwm( 0x01F4, 0 );

 

}

 

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;

      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….

 

PS. An image of my setup - busy but it worked!

 

Set-up


Average Rating:

Add Your Comment

Comments:


re:H bridge
By eeefriend on Wednesday, April 26, 2006 (GST)
the explanation is good,<br>
but if u explain about the PWM operation with the circut diagram<br>
of PIC, then it will be effective ,<br>
why it converter called H bridge.<br><br>
eeefriend<br>

Reply to this Comment



Search ...

Go

New Articles


electrical liquid tight flexible metallic conduit,LT fittings




A buck-mode converter for a stable output
voltage and/or current from an unstable
or variable input. Preliminary.




A brief introduction to using a H-Bidge IC and a PIC microcontroller to control the speed of a DC motor.





Most Popular Articles


A brief introduction to using a H-Bidge IC and a PIC microcontroller to control the speed of a DC motor.




A brief introduction to the use of ultrasonics in measuring distances to objects. A circuit for a 4-channel sonar system is described.




Quick Start Guide to Microcontrollers, how to choose and where to go.