www.eXtremeElectronics.co.in

DC Motor and Its Control.

xBoard v2.0

Easy to Use learning and development tool for Atmel AVR family of MCUs.

 

<< Return To Help Index

DC Motor is the most commonly used actuator in robotics applications. An actuator is a device used to produce motion. DC motors are used to drive wheels in order make the robot move. They are also used to power grippers, arms and weapons( fighting robos). Normally a DC Motor with inbuilt gear box is preferred. One such motor is shown below.

dc motor
A Geared DC Motor.

They normally consume 400ma to 1000ma current and works off 12v DC Supply. The shaft is compatible with commonly available hobby robotics wheels. They are available in many different RPM(Revolution Per Minutes). Example: 60 RPM, 100 RPM and 200 RPM.

In this tutorial we will learn the basics about these motors and how to control them using a microcontroller.

NOTE:

Many hobby robotics items are available at best rates from eXtreme Electronics Web shop.We deliver them any where in India at low shipping rates.

Control

DC Motor Rotates in one direction when you apply power to its terminals. When you reverse the polarity of the supply it will rotate in other direction.

As a DC Motor require current in the range of 400ma to 1000ma we cannot supply them directly from the MCUs I/O PINs. We need some kind of driver circuit that can deliver more current to the motors. Also a MCU generally works off 5v supply but normal motors require a 12v (or 24v) supply. This circuit which is used to control a Motor from MCUs I/O line is called H-BRIDGE circuit. Many easy to use H-Bridge ICs are available, like the L298. One L298 IC has two H-BRIDGE circuits. So it can control 2 DC Motors.

xBoard has two L298 ICs that can control up to four DC Motors with upto 1A current rating each.

An Example of driving motors by using L298 is given below.

Basic L298 Connection
Basic L298 Connection

The Motor is controlled in the following way by MCU's i/o lines

Motor A

Motor B

In xBoard v2.0 Primary L298 is connected to the PORTC bit (0,1,2 & 3) in the following way.

PC0 Input A
PC1 Input B
PC2 Input C
PC3 Input D

Following Code can be used to control the direction of Motor A.

Motor Stop

   PORTC&=(~(1<<PC0));  //PC0 = LOW
   PORTC&=(~(1<<PC1));  //PC1 = LOW

Motor Rotate Clockwise

   PORTC|=(1<<PC0);	//PC0 = HIGH
   PORTC&=(~(1<<PC1));	//PC1 = LOW

Motor Rotate Counter Clockwise

   PORTC&=(~(1<<PC0));	//PC0= LOW
   PORTC|=(1<<PC1);	//PC1= HIGH

Similarly Motor B can be controlled by applying proper logic to PC2 and PC3 in place of PC0 and PC1

Tip:

Hands On Experiment.

We will go through a simple experiment that will show you how to control motor A and Motor B from your program. The program will first start Motor A in Clockwise Direction then It will Stop it for some time, after that it will start it in Counter Clock wise direction. The complete program is given below.


/*********************************************************************

                 xBoard(TM) v2.0 Sample Programs

               ------------------------------------


Description :  Simple Motor Control Demo.
            Start Motor A in Clockwise direction.
            Then Stops the Motor
            Again Starts the Motor in Counter Clockwise Direction.

Notes:         Jumper JP2 Must be in OFF position. 
            This means NO SPEED Control.

Author      : Avinash Gupta 2008
Web         : www.eXtremeElectronics.co.in
                   
**********************************************************************/

#include <avr/io.h>

#include <util/delay.h>

void Wait()
{
   uint8_t i;
   for(i=0;i<80;i++)
      _delay_loop_2(0);
}

void main()
{
   //First Set up I/O Ports as OUTPUT

   //PC0 and PC1 as output
   DDRC|=((1<<PC0)|(1<<PC1));

   //Repeat the sequence forever
   while(1)
   {
      //Clockwise Motion

      PORTC|=(1<<PC0);  //PC0 = HIGH
         PORTC&=(~(1<<PC1));  //PC1 = LOW

      Wait();


      //Stop

      PORTC&=(~(1<<PC0));  //PC0 = LOW
         PORTC&=(~(1<<PC1));  //PC1 = LOW

      Wait();


      //Counter Clock Wise Motion

      PORTC&=(~(1<<PC0));  //PC0= LOW
         PORTC|=(1<<PC1);  //PC1= HIGH 

      Wait();


      //Stop

      PORTC&=(~(1<<PC0));  //PC0 = LOW
         PORTC&=(~(1<<PC1));  //PC1 = LOW

      Wait();
   }
}


Create an AVR Studio Project Named "Motor" and copy/paste the above code in "Motor.c" file. Compile and Build the project as described in basic tutorials. Then burn the final "Motor.hex" file to the board.

Note:

Now connect the motor with the xBoard as shown below. Connect it to Connector labeled MOTOR-A.

motor connector
Motor Connectors On xBoard v2.0

 

dc motor connector xboard
Motors Connected with xBoard v2.0

 

dc motor
A Geared DC Motor.

 

motors with xboard
The Whole Setup

 

NOTE: Make sure that Jumper JP2 is in OFF position. This will configure the board so as to DISABLE speed control of MOTOR-A. Since it is a very basic experiment so to keep it simple we are NOT using PWM speed control. PWM Speed control will be described in next tutorials.

USART, JP4, JP2 and JP3 Location

 

After that you are ready to power up the board. When powered up the Motor will first Rotate clockwise then stop and then rotate counter clockwise. This process is repeated as long as the board is powered.

NOTE: If the motors rotates opposite of the expected direction then you must have wired the motor in wrong polarity. The left wire in the connector should be RED and the right one should be BLACK as shown in above image.

NOTE: You need a powerful Battery or Adaptor (current supply of 1A or More). The project would not run properly with small 9V battery or small adaptor(500ma).

NOTE: The sample programs are available under “Samples” folder in support CD. The HEX files ready to burn are available under “HEX” folder. HEX File Name: “MotorTest.hex”

<< Return To Help Index