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

DC Motor Speed And Rotation Control

By Ashuthosh Bhat February 5, 2016

DC motors are widely used in different machines, mechanism and motion control in manufacturing and processing industries. In these machines/mechanisms it is required to control speed of motor or number of rotations of motor or may be both. For example

  1. To release thread (or wire) of desire length from a bobbin, it is required to rotate bobbin for specific number of rotation. Also there is provision for controlling a speed at which thread is released. So here it is required to rotate bobbin at required speed and for required number of rotations only

  2. In coil winding machine it is required make specific number of coil turns. It is done by rotating motor for specific number of rotations. Also it is required to vary the motor speed to speed up/down the operation

  3. In conveyor belt application, to move the object to exact distance it is required to rotate motor for desire number of rotations

Thus we may list out many numbers of such examples and applications where it is required to control rotations and speed of DC motor.

The given project is demonstration of controlling rotations and speed of DC motor. Its an example of close loop control system. It utilizes feedback loop. It counts actual number of motor rotations and gives as a feedback to system. The system compares set rotations with actual motor rotations and stops motor when they match. It also controls speed of motor from 10% to 100%. The project utilizes micro controller ATMega32 to control DC motor, LCD for display parameters and opto-interrupt sensor count motor rotations and provide feedback.

Circuit description:

As shown in figure, the circuit is built using AVR micro controller ATMega32, darlington chip ULN2003A, 16×2 LCD, opto-interrupt sensor MOC7811 and other few components like push buttons, resistors, crystal etc.

Four push buttons are connected to PORTA pins PA0 – PA3 such that when button is pressed it gives logic ‘1’ input to pin. They are used to enter rotation and speed and to rotate motor.

LCD is connected to PORTC and PORTD. Its data pins are connected to PORTC and two control pins Rs and En are connected to PD0 and PD1 respectively. LCD displays number of rotations and speed of DC motor.

PORTD pin PD7 drives DC motor through Darlington pair chip ULN2003A. This pin is connected to input (1) of ULN2003A and the output (16) of ULN2003A is connected to one terminal of motor. Other terminal of motor is connected to 12 V supply.

So when logic ‘1’ input is given from micro controller to PD7, the Darlington pair conducts and motor starts rotating.

Opto-interrupt sensor MOC7811 internally consists of IR LED and photo transistor. The internal IR LED is forward biased to make it continuously on. A current limiting resistor of 330E is used to limit the current through it. The output of photo transistor is given as input to another NPN transistor that is connected in switch mode. The final output of sensor circuit is taken from collector of NPN transistor. This output is given to external interrupt 0 input pin PD2 (17) of ATMega32.

Here are the snaps of circuit arrangement

Prototype of AVR ATMega16 based DC Motor Speed Control Circuit

Fig. 1: Prototype of AVR ATMega16 based DC Motor Speed Control Circuit

Prototype of AVR ATMega16 based DC Motor Speed Control after installation

Fig. 2: Prototype of AVR ATMega16 based DC Motor Speed Control after installation

Circuit operation:

The motor is stop initially. The message is display on LCD to enter required number of motor rotations as “Set rotations”

User can select required number of rotations by pressing button 1 or 2. Button 1 is used to increase number of rotations by 5 and button 2 is to decrease number of rotations by 5. Minimum numbers of rotations are 10.

Once desire numbers of rotations are selected, user has to press enter (button 3). The LCD shows message as “Rotations set to xx”

After 2 second, the user is asked to enter required speed. The message is displayed on LCD as “Set motor speed”

User can set speed from 10% to 100% in step of 10 by pressing same button1 and button2. Button 1 is used to increase speed by 10% and button 2 is to decrease speed by 10%.

Once desire speed is set, again user has to press enter. The LCD shows message as “Speed is set to xx%”

Now user is asked to start the motor and LCD shows message “press rotate”. When user presses rotate button (button 4), the micro controller applies PWM as per selected speed to motor and motor starts rotating at desired speed. The LCD shows speed and rotations of motor.

As the motor rotates one revolution, a strip attached to the shaft of motor passes through the gap of opto-interrupt sensor. That interrupts IR light falling on photo transistor for fraction of second. So photo transistor gives very short duration positive pulse. This positive pulse is inverted by NPN transistor and it gives short duration negative pulse. This negative pulse generates interrupt for micro controller. Micro controller counts these numbers of interrupts as numbers of rotations of motor.

When motor completes desire numbers of rotations, micro controller stops applying PWM so motor stops

After 2 second, again user is asked to enter desire rotations for next operation and this cycle continuous.

Software program:

The micro controller ATmega32 is the main building block of this project because it performs all the tasks like

  1. Take user input from push buttons to set speed and rotations, run motor etc

  2. Rotates motor at desire speed using PWM technique

  3. Displays motor speed, rotations and various messages on LCD

  4. Counts actual motor rotations and

To implement all above functionalities the software program is downloaded into internal ROM (FLASH) of this micro controller. Here is the C program with necessary comments.

Project Source Code

###


#include

#include

#include

#include


 

#define lcd_databus PORTC // PORTC is LCD data bus

#define lcd_cntr_port PORTD // PORTD is LCD control port

#define rs PD1

#define en PD0


 

//////////////////////// variable declaration /////////////////////////////


 

unsigned int speed_set_flag=0,rotation_set_flag=1;

unsigned int speed=25,speed_value=10,num_of_rotations=10,motor_rotations=0;


 

///////////////// function to send data to LCD/////////////////////////////


 

void senddata(unsigned char data)

{

_delay_ms(2); // wait till LCD is busy

lcd_cntr_port=(1<

lcd_databus=data; // send data

lcd_cntr_port=(1<

lcd_cntr_port =(1<

}


 

/////////////////// function to send command to LCD /////////////////////

void sendcmd(unsigned char cmd)

{

_delay_ms(2); // wait till LCD is busy

lcd_cntr_port = (0<

lcd_databus=cmd; // send command

lcd_cntr_port = (1<

lcd_cntr_port = (0<

}


 

////////////////////// function to print string on LCD ////////////////

void printstr(char *s)

{

unsigned int l,i;

l = strlen(s); // get the length of string

for(i=0;i

{

senddata(*s); // write every char one by one

s++;

}

}


 

/////////////////// function to print integer value on LCD ///////////////

void display_data(unsigned int value)

{

unsigned char ascii_value[3];

unsigned int tmp,t;

if(value>=100) // if value is of 3 digits

{

tmp = value%10; // separate last digit

ascii_value[2] = tmp+0x30; // convert it into ASCII

value = value/10;

tmp = value%10; // likewise convert

ascii_value[1] = tmp+0x30; // all 3 digits into ASCII

value = value/10;

ascii_value[0] = value+0x30;

}

else if (value>=10)

{

tmp = value%10; // else if value is of 2 digits

ascii_value[1] = tmp+0x30; // separate digits and convert

value = value/10; // them into ASCII

ascii_value[0] = value+0x30;

ascii_value[2] = 0x20; // make last digit space

}

else if(value<10) // if value is of 1 digit

{

ascii_value[0] = value+0x30; // convert it into ASCII

ascii_value[2] = 0x20; // rest two digits space

ascii_value[1] = 0x20;

}

for(t=0;t<3;t++) senddata(ascii_value[t]); // print value


 

}

////////////////////// function to initialize LCD ////////////////////////


 

void lcd_init()

{

sendcmd(0x3C); // 8-bits/char 7x5 dots/char

sendcmd(0x0E); // screen and cursor ON

sendcmd(0x01); // clear LCD

printstr("DC Motor speed &"); // display project name

sendcmd(0xC0);

printstr("rotation control");

}


 

/////////////////////// function for up-arrow key //////////////////////

void up_arrow_key()

{

if(speed_set_flag==1) // either set speed

{

if((speed<250) && (speed_value<100))

{

speed +=25; // inc speed by inc PWM width

speed_value+=10; // inc display speed value

}

sendcmd(0xC0);

display_data(speed_value); // display the value

senddata('%');

}

else if(rotation_set_flag==1) // or set rotations

{

num_of_rotations+=5; // inc rotations

sendcmd(0xC0);

display_data(num_of_rotations); // display them

}

}


 

///////////////////// function for down-arrow key ///////////////////////

void down_arrow_key()

{

if(speed_set_flag==1) // either set speed

{

if((speed>25) && (speed_value>10))

{

speed -=25; // dec speed by dec PWM width

speed_value-=10; // dec display speed value

}

sendcmd(0xC0);

display_data(speed_value); // display speed

senddata('%');

}

else if(rotation_set_flag==1) // or set rotations

{

if(num_of_rotations>10) num_of_rotations-=5; // dec rotations

sendcmd(0xC0);

display_data(num_of_rotations); // display rotations

}

}


 

///////////////////////// function for enter key ////////////////////////

void enter_key()

{

if(speed_set_flag==1) // when pressed to set speed

{

sendcmd(0x80);

printstr("speed is set to "); // display message and

sendcmd(0xC0);

display_data(speed_value); // speed value

senddata('%');

speed_set_flag=0; // reset the flag

_delay_ms(2000); // and after 2 second

sendcmd(0x01);

printstr("press rotate"); // display message

}

else if(rotation_set_flag==1) // when pressed to set rotations

{

sendcmd(0x01);

printstr("rotations set to"); // display set rotation

sendcmd(0xC0);

display_data(num_of_rotations); // value

rotation_set_flag=0; // reset the flag

_delay_ms(2000); // wait for 2 sec

sendcmd(0x01);

printstr("set motor speed"); // again display message

sendcmd(0xC0);

display_data(speed_value);

senddata('%');

speed_set_flag=1;

}

}


 

//////////////////////// function to rotate motor ///////////////////////


 

void rotate_motor()

{

sendcmd(0x01);

printstr("speed:"); // display motor speed

display_data(speed_value);

senddata('%');

sendcmd(0xC0); // and actual number of

printstr("rotations:"); // rotations

OCR2 = speed; // set width for PWM

 

///////// rotate motor till desire rotations complete///////

while(num_of_rotations!=motor_rotations)

{

TCCR2 = 0x6A; // start timer to generate PWM

PORTC=0x01;

}

PORTC = 0x00;

TCCR2 = 0x00; // stop timer

motor_rotations=0; // reset motor rotations

rotation_set_flag=1; // set rotation flag again

_delay_ms(2000); // delay for 2 sec

sendcmd(0x01); // clear LCD

printstr("set rotations"); // again display message for

sendcmd(0xC0); // next operation

display_data(num_of_rotations);

}


 

//////////////////////////// main function ////////////////////////////////


 

int main(void)

{

DDRB=0xFF; // configure ports as input or output

DDRC=0x01;

DDRD=0x83;

DDRA=0x00;

PORTD=0x00;

PORTB=0x00;

PORTC=0x00;

MCUCR = (1<

GICR=(1<

sei(); // enable global interrupt

lcd_init(); // initialize LCD

_delay_ms(2000); // wait for 2 seconds

sendcmd(0x01); // clear LCD and

printstr("set rotations"); // display message

sendcmd(0xC0);

display_data(num_of_rotations);

while(1)

{

while(PINA==0xF0); // wait till no button is pressed

switch(PINA)

{

case 0xF1: // for 1st button

up_arrow_key(); // inc speed or num of rotations

_delay_ms(200); // key debounce delay

break;

case 0xF2: // for 2nd button

down_arrow_key(); //dec speed or num of rotations

_delay_ms(200);

break;

case 0xF4: // for 3rd button

enter_key(); // set speed or rotations

_delay_ms(200);

break;

case 0xF8: //for 4th button

rotate_motor(); // rotate motor

_delay_ms(200);

break;

}

}

}

////////////////////// external interrupt 0 function //////////////////////


 

ISR(INT0_vect)

{

motor_rotations++; // inc motor rotations

sendcmd(0xCA);

display_data(motor_rotations); // display it on LCD

}

###

 


Circuit Diagrams

Circuit-Diagram-AVR-ATMega16-Based-DC-Motor-Speed-Controller

Project Video


Filed Under: Electronic Projects

 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

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