Pond Electronics - Home of the µFlash876 & µFlash876B embedded controllers, µStack & µConnect Bus.

DC Motor Control with the µS-DD4

Introduction

This application note describes the use of the DD4 to control small DC motors, starting with simple ON/OFF control and building up to full bi-directional speed control of two motors.

The µS-DD4 is a four channel Darlington Driver board suitable for driving DC loads of up to 1.5A at voltages of 12V-24V (Please refer to the µS-DD4 user manual for a full specification).Each darlington driver is configured as a low side switch, a "freewheel" diode protects the driver from transient voltages encountered when driving inductive loads such as motors.

 

 

 

Setting Up the µS-DD4

For the purposes of this application note we will assume that the DD4 is stacked on a µFlash876/876B SBC. Simple on/off control of a driver channel can be achieved with any of the µStack I/O lines, however for more sophisticated control there are some restrictions. The PWM channels (used for speed control ) on a µFlash876 are P1 & P2, therefore we should ensure that two of the drivers are configured to use these channels. With this in mind it is probably most convenient to assign I/O lines P0 to P3 as driver control channels, this can be done by configuring the DD4 jumpers as shown to the the right.

 

Simple ON/OFF Control

Turning a motor on and off with a µS-DD4 could not be simpler. Attach the µS-DD4 to the motor PSU, now connect the motor to driver channel DD3. By controlling the appropriate µStack I/O line you can now turn the motor on and off.

The CCS PIC C code for the µFlash876/876B to turn on and off the motor (on this channel) is:

 
	output_high(P2); // turn on the motor
	.
	.
	output_low(P2);  // turn off the motor

 

With the each DD4 in a stack you can control up to 4 motors in this way.

Adding Speed Control

While simply turning on and off a motor may be good enough for some applications, controlling the speed of the motor makes it much more useful. You can achieve open loop control of the motor speed using a technique known as PWM (Pulse Width Modulation). Simply put, PWM is done by turning on and off the motor at a fixed rate (very fast ) allowing the inertia of the motor to average out the signal. By keeping the rate constant and varying the pulse width you can control the average power to the motor and therefore its speed. The diagram to the right illustrates a range of PWM signals from 0% to 100%. A 0% signal turns the motor off, 100% is full on, intermediate values result in intermediate speeds.

While it is possible to generate PWM signals in software, the µFlash876/876B include hardware support for PWM generation. By making use of this facility you can achieve speed control of up to two motors with almost no software overhead. The PWM channels on the PIC16F876 are connected to µStack pins P1 (DD2) and P2 (DD3) so speed control is only possible using these two channels.

The PWM module on the PIC16F876 is in fact quite flexible and powerful. Unfortunately this power comes at the price of significant complexity, luckily the CCS PIC C compiler has direct library support for the PWM module greatly simplifying setup and use.

 

Controlling Direction

Now we are ready to make things really interesting, controlling the motor direction. With bi-directional speed control of a motor you open the way to a multitude of applications in robotics, animatronics, automation and who knows what else. To control the direction of a rotation we need to be able to reverse the direction of current flow in the motor. This usually requires a hardware configuration known as a "Full H Bridge". Unfortunately it is not possible to configure the DD4 as a H Bridge, however by using an external relay driven from another DD4 channel it is possible reverse the current flow and therefore achieve bi-directional control.

To do this we need what is known as a Double-Pole-Change-Over (DPCO) relay for each motor we wish to control. A DPCO relay consists of a pair of change over contacts under the control of a single activation coil. By wiring the relay in a specific way we can effect current reversal in the motor. Note that since both the motor and the relay are driven from the same DD4 board, they must have the same voltage rating (ie for a 12V motor, use a 12V relay).

In the diagrams below, DD3 is used as before to control the motor speed using PWM while DD4 controls the relay and therefore the motor direction. With the relay off the motor behaves as before, however by turning on the relay, current is reversed through the motor and therefore the motor rotates in the opposite direction. Note that for the purposes of this diagram we have rearranged the relay terminal layout to make it easier to follow the current path.

 

Two Channel Control - Construction

While the relay circuit could be constructed on prototype board, it is so simple that it probably best to build it by soldering wires directly to the relay pins. The relays themselves can then be glued upside down to a mounting board or directly onto the chassis of your application hardware.

The diagram on the left shows the configuration for bi-directional control of two motors. The motors are wired to the relay common connectors while current reversal is achieved by cross wiring the NO and NC connections between the two contacts. This can be done with short pieces of insulated wire soldered directly to the relay pins.

If having wired up the circuit you find that the default (relay off) direction of motor rotation is not what you require, simply reverse the connections to the motor to achieve the required direction.

Note that changing the direction of the motors without bringing the motor to a full stop will cause massive current transients in the drivers and should therefore be avoided.

 
/*
*****************************************************************************
* uFlash876 sample code
* Compile with CCS PIC C
* Demonstrate dual motor speed & direction control using the uS-DD4
*****************************************************************************
*/
#include <uF876.h>
/*
******************************** main ***************************************
*/
 
void init_motors(void);
void set_speed_m1(BYTE s);
void set_speed_m2(BYTE s);
void set_dir_m1(BYTE d);
void set_dir_m2(BYTE d);
 
void main(void)
	{
	BYTE x;
 
	init_motors();
	while(1)
		{
		set_dir_m1(0);                 // set direction for m1
		for(x=0;x<255;x++)             // ramp up speed
			{
			delay_ms(50);
			set_speed_m1(x);
			}
		set_speed_m1(0);               // stop m1
		delay_ms(250);
		set_dir_m1(1);                 // change dir m1
		for(x=0;x<255;x++)             // ramp up speed
			{
			delay_ms(50);
			set_speed_m1(x);
			}
		set_speed_m1(0);               // stop m1
		delay_ms(250);
 
		set_dir_m2(0);                 // set direction for m2
		for(x=0;x<255;x++)             // ramp up speed
			{
			delay_ms(50);
			set_speed_m2(x);
			}
		set_speed_m2(0);               // stop m2
		delay_ms(250);
		set_dir_m2(1);                 // change dir m2
		for(x=0;x<255;x++)             // ramp up speed
			{
			delay_ms(50);
			set_speed_m2(x);
			}
		set_speed_m2(0);               // stop m2
		delay_ms(250);
 
		}
	}
 
void init_motors(void)
	{
	setup_timer_2(T2_DIV_BY_16,255,1);    // setup timer 1 (used by pwm)
 
	setup_ccp1(CCP_PWM_PLUS_2);           // set up PWM channels
	setup_ccp2(CCP_PWM_PLUS_2);
 
	set_pwm1_duty(0);                     // set initial duty
	set_pwm2_duty(0);
 
	output_low(P0);                       // make drive pins all outputs
	output_low(P1);
	output_low(P2);
	output_low(P3);
	}
 
void set_speed_m1(BYTE s)
	{
	set_pwm2_duty(s);
	}
 
void set_speed_m2(BYTE s)
	{
	set_pwm1_duty(s);
	}
 
void set_dir_m1(BYTE d)
	{
	if(d)
		output_high(P0);
	else
		output_low(P0);
	}
 
void set_dir_m2(BYTE d)
	{
	if(d)
		output_high(P3);
	else
		output_low(P3);
	}
 

A µFlash876B and µS-DD4 with relays providing bi-directional control of two 1.5A gearmotors.

Copyright (c) Paul O Neill & Pond Electronics. 2003.

Home | Products | Technical Info | How to Order | Contact Us | Links