DC Motor Speed Controller

This article 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.

L298 Schematic

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.

Motors

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.

Schmatic 2

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.

Scope reading with resistor

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

Scope reading with maxon

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!

Scope reading with cheap motor

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:

Schmaic 3

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:

Scope reading with resistor (2)

And here is another with the Maxon motor:

Scope reading with maxon (2)

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

Posted in: News October 2007 Martyn


Post A Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>