Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Speed and Direction Control of DC Motor using AVR Microcontroller – (Part 20/46)

By Ashutosh Bhatt February 11, 2014

 

Controlling direction and speed of DC motor is very essential in many applications like

·         Robotic application – to change direction and speed of moving robot

·         Industrial application – to change direction and speed of rotating machinery

·         Domestic application – to vary speed of battery (DC) operated portable fan

·         Defence application – to rotate radar, automatic gun, tank gun in either direction

·         Communication application – rotate dish antenna upward – downward or clockwise – anticlockwise

Thus we can count number of such applications where there is a need to change direction and/or speed of DC motor. The direction of DC motor can be controlled by just reversing the polarity of given supply. And for varying speed, there are various ways to vary speed of DC motor but the best amongst them is PWM – pulse width modulation technique. In this technique we shall vary the width of applied pulse that will vary average voltage applied to motor and its speed will change.

So this project demonstrates how to vary the speed and change the direction of simple 12V 500 RPM DC series motor using AVR microcontroller ATmega32.

 

System block diagram: –

Block Diagram of AVR ATMega32 based DC Motor Controller

Fig. 1: Block Diagram of AVR ATMega32 based DC Motor Controller

The major building blocks of this system are as shown in figure that are ATmega32 and DC motor driver chip L293D. Along with them we need some push buttons for controlling and LEDs for indications. Each block is described briefly here.

 

Push buttons – five push buttons are given to run, stop, change speed and direction of DC motor

Button

Function

Button 1

Run motor in clockwise direction

Button 2

Run motor in anticlockwise direction

Button 3

Stop motor

Button 4

Increase speed

Button 5

Decrease speed

 

L293D chip – this chip takes input from micro controller and drives DC motor. The micro controller output current is not enough to drive DC motor directly. The chip has quad half H bridge drivers. It will provide up to 600 mA current to motor that is enough to drive it.

 

LED indications – six LEDs are used for different indications

LED

colour

indication

LED 1

red

Blinks when motor runs in clockwise direction

LED 2

green

Blinks when motor runs in anticlockwise direction

LED 3

Blue

Blinks when speed in increased

LED 4

Orange

Blinks when speed is decreased

LED 5

red

Lits ON when speed limit is maximum

LED 6

red

Lits ON when speed limit is minimum

AVR micro controller ATmega32 is low power, high performance, 8-bit, Advance RISC architecture based micro controller. It has 32 KB of in system programmable FLASH. Also it has built in peripherals like 10-bit, 8 channel ADC, 4 PWM channels, 1 16-bit and 2 8-bit timers/counters, USART, SPI, IIC etc. In this system it performs following tasks

·         Scans push buttons continuously to get user input

·         Give different indications on LEDs

·         Run or stop DC motor

·         Change direction or speed of motor

  The circuit is made up of ATmega32, L293D and few discrete components like LEDs, registers, capacitors, push buttons etc

·         5 push buttons SW1 – SW5 are connected to PORTA pins PA0 to PA4. Also all port pins are pulled high through external pull up resistors as shown

·         4 LEDs LED1 – LED4 are connected to PORTD pins PD0 to PD3 through current limiting resistors

·         PORTC pins PC0 and PC1 are connected to input of L293D chip. 2 LEDs LED5 and LED6 are also connected to these pins through current limiting resistors

·         DC motor is connected to output of L293D chip

·         A 16 MHZ crystal with two 22 pF capacitor is connected to crystal input pins XTAL1 & XTAL2

·         Vcc supply of 5 V is connected to required pins of both chips and pull up resistors

·         Additional 12 V supply is given to L293D chip for motor only

Working & Programming

System working and operation: –

·         Initially motor is stop. All the LEDs are OFF

·         When SW1 is pressed momentarily, the motor starts rotating in clockwise direction. The green LED blinks at rate of PWM frequency

·         Now if SW4 or SW5 button is pressed, the motor speed will increase or decrease. It is indicated by blink of LED1 or LED2

·         If anyone keep on pressing SW4 or SW5 then speed will keep on increasing or decreasing. When max or min limit is reached, the corresponding LED3 or LED5 will be ON

·         While motor is running in clockwise direction if SW2 is pressed, the motor will immediately start rotating in anticlockwise direction and vice versa

·         Pressing SW3 will stop running motor

Program: –

AVR microcontrollers have built in feature to generate PWM using internal timer. We can use this feature to generate PWM and vary speed of DC motor. But such type of feature is not available in all micro controllers so in that case we have to generate PWM using program. So here i am not using inbuilt feature but generating PWM using program. Program is written in C language. Its compiled and simulated using AVR studio software. Complete program is made up of different functions

·         Keydly() function – this function generates key debounce delay of approx 0.2 sec that is required when key is pressed

·         Incspeed() function – this function increases width of pulse fed to motor by increasing ON time and decreasing OFF time. It also checks for maximum speed limit. If maximum speed limit is reached it turns ON LED3

·         decspeed() function – this function decreases width of pulse fed to motor by decreasing ON time and increasing OFF time. It also checks for minimum speed limit. If minimum speed limit is reached it turns ON LED4

·         clockwise() function – it continuously gives PWM to motor to rotate it in clockwise direction till any button is not pressed

·         anticlockwise() function – it continuously gives PWM to motor to rotate it in anticlockwise direction till any button is not pressed

·         main() function – it initializes ports as input or output and then it scans all the buttons in continuous loop. 

 

 

Project Source Code

###



#include <avr/io.h>
#include <util/delay.h>
uint16_t ton=5000,toff=5000;
void keydly()                         // key debounce delay
{
   uint8_t i;
   for(i=0;i<7;i++)
  _delay_loop_2(10000);           // delay loop library function
}
void incspeed()                  // increase width of pulse
{
  PORTD = (0<<PD3);            // led indication
  if(toff>1000)               // check for minimum off time
  {
      toff=toff-500;          // if not then decrease off time
    ton=ton+500;              // and increase on time
  }                           
  if(toff==1000) PORTD = (1<<PD2);
// if max limit is reached - LED on             
}
void decspeed()               // decrease width of pulse
{
  PORTD = (0<<PD2);          // led indication
  if(ton>1000)              // check for minimum on time
  {
     toff=toff+500;        // if not then increase off time
     ton=ton-500;         // and decrease on time
  }                           
  if(ton==1000) PORTD = (1<<PD3);  
                            // if min limit is reached – LED off
}
void clockwise()           // rotate motor clockwise
  {
    while(PINA==0xFF)     // till any key is not pressed
    {
          PORTC=0x01;    // apply PWM to motor
          _delay_loop_2(ton);
          PORTC=0x00;
         _delay_loop_2(toff);                                    
  }           
 }
void anticlockwise()     // rotate motor anticlockwise
  {
    while(PINA==0xFF)   // till any key is not pressed
    { 
          PORTC=0x02;  // apply PWM to motor
          _delay_loop_2(ton);
           PORTC=0x00;
          _delay_loop_2(toff);                                     }                         
  }
int main(void)
{
    uint8_t r=0,d;        // run and direction flag
    DDRC=0x03;           // initialize ports as input and output
    DDRD=0x0F;
    DDRA=0x00;
    PORTC=0x00;
    PORTD=0x00;
    while(PINA==0xFF);  //wait till any key is pressed       
loop:switch(PINA)
            {
            case 0xFE:      // for 1st key                                   keydly();
            r=1;            // set run flag                                  d=0;    clear direction flag for CLK direction
            clockwise(); // start rotating motor clockwise
            break;
            case 0xFD:      // for 2nd key
            keydly();
            r=1;           // set run flag
            d=1;   // set direction flag for ACLK direction
           anticlockwise(); // start rotating motor anticlockwise
              break;
             case 0xFB:       // for 3rd key
             PORTC = 0x00;   // stop motor
             PORTD = 0x00;   // all indications off
             r = 0;         // clear run flag
             Break;
             case 0xF7:      // for 4th key
             PORTD=(1<<PD0);    // blink LED1
              keydly();
              PORTD=(0<<PD0);
              incspeed();               // increase speed
              if(r==1)               // if motor was running
              {
              if(d==0) clockwise();  // keep it running clockwise
              else anticlockwise();       // or anticlockwise
               }
               break;
               case 0xEF:                // for 5th key
               PORTD=(1<<PD1);          // blink LED2
               keydly();
                PORTD=(1<<PD1);
                decspeed();                 // decrease speed                    if(r==1)                 // if motor was running
               {
                 if(d==0) clockwise();       // keep it running
                  else anticlockwise();
                }
                break;                                 
                }                                                    goto loop;
}




###

 


Circuit Diagrams

Circuit-Diagram-AVR-ATMega32-Based-DC-Motor-Controller

Project Video


Filed Under: Electronic Projects
Tagged With: atmega16, avr, dc motor
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Industrial Relay Board Design for Motorcycle Use
  • Finding past posts on edaboard?
  • I think i have devised a new type of "super_transformer" for the Electricity grid?
  • What is the purpose of this relay?
  • mosfet driver problem in regeneration mode

RSS Electro-Tech-Online.com Discussions

  • Pic18f25q10 osccon1 settings swordfish basic
  • Sump pit water alarm - Kicad 9
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe