Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • 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
  • Guest Post Guidelines
  • Advertise
  • Subscribe

DC Motor Control with VC++

By Ashutosh Bhatt April 29, 2013

 

The above project was dos based and now this is window based program written in Visual C++ to control DC motor. Very efficient, effective more user friendly software. Let us see the main function as well as the additional features of this application.

1) Totally windows based application so very powerful GUI. More user friendly program

2) Mouse as well as keyboard interface provided. All the controlling actions at your fingertips

3) Four degree of motion control is achieved for single motor. (1) Motor rotates continuously at constant speed (2) pulse wise rotation means motor rotates for less then one revolution (3) motor rotates with desired speed for fix duration of time (4) motor rotates till mouse button is pressed and moved.

4)One can vary both the parameters of motor speed and direction

5) DC motor will rotate like stepper motor in pulse wise rotation mode

 General Description:-

Again there are two parts of the project (1) hardware part (2) software part. The hardware part remains same used in previous project only change in software part and that is instead of C++ now it is build using VC++. All the function of previous project are included here (like direction change, speed increase/decrease) and also some additional features are included like pulse wise rotation.

 

 

 Hardware & Software Description

Hardware Part:-

 

Same H-Bridge circuit is used as a DC motor driver and it is connected with LPT port. Connections, operation, working all thing remains same. The figure given in circuit tab 1 will tell you everything.

Software Description:-

 

Complete software is built on the platform of visual C++ programming language. So one must be familiar with VC++ if he wants to develop this project. He must be knowing all the tools, commands as well as operating of VC++. I am explaining a step by step procedure to develop this application so that one can (even a new voice) build it very easily. Follow the step by step procedure to build an application

A) Open a file menu and click on new tab

B) Choose project tag and select MFC app wizard standard exe

Visual C++ Dialog Box

C) Give a suitable name of project like “DCMC” or any else

D) In the first step chose dialog based application

E) In the second step uncheck 3D controls and ActiveX control boxes

F) Give suitable title of dialog based application like “DC Motor Control”.

G) Leave all the options on third step as it is

H) In the last step you will see that VC++ has created two classes for your dialog based application named ‘CDCMCApp’ & ‘CDCMCDlg’. Leave these names as it is and click finish tab. Finally summary of all the details will appear then click OK button.

I) You will see VC++ has created standard dialog based exe and you will see this screen shot

Constant speed remote motor

 

On right hand side you will see application background with two buttons and one text message. On the left hand side you will find three viewing tags class view, resource view & file view. By default you will be in resource view tag. Now the first thing you have to do is to modify the application as per our requirement. So delete both the buttons and the text message and insert the items and set their property as given in table. We shall add 8 push buttons, 1 group box, 1 edit box and last 1 static text. You can add all these using control toolbar. To set the property just right click on any object (button, group box etc.) and choose property from popup menu.

Sr. No.

Object

Property

Setting

1

Button

ID

IDC_FWD

Caption

Start &Forward

2

Button

ID

IDC_REW

Caption

Start &Reverse

3

Button

ID

IDC_UP

Caption

Speed &Up

4

Button

ID

IDC_DWN

Caption

Speed &Down

5

Button

ID

IDC_FRW

Caption

F&orward

6

Button

ID

IDC_RWS

Caption

R&everse

7

Button

ID

IDC_STP

Caption

&Stop

8

Button

ID

IDC_XIT

Caption

E&xit the program

9

Static text

ID

IDC_STATIC

Caption

Speed Factor

10

Edit box

ID

IDC_SF

Caption

0

11

Group Box

ID

IDC_STATIC

Caption

Motor rotate continuously at constant speed

 

 

 

 

 Software Description contd..

After setting the properties of all these 11 objects build and run the application our application will look like

Uptill now we haven’t done any coding but now the coding part will start. You have to attach a function with each button. For this open class wizard from view menu. Follow the steps

  1. Select the button ID like “IDC_FWD” or any other and click on BN_CLICKED event
  2. Then press add function button. A message box will appear with name of function. leave it as it is and click OK. You will be diverted in source code to  this edit function.
  3. Right now don’t edit but follow the same procedure and attach function to each button.
  4. Now you have to attach a variable to edit box. For this again open class wizard and select member variable tag.
  5. You will find all the IDC will appear in dialog box. Select IDC of edit box ‘ IDC_SF ‘.
  6. Now click add member variable button. Second dialog box will appear. Give the name of variable as “m_speedfact”. In a category choose value and in type choose CString click ok.
  7. Now select class view tag. Right click on CDCMCDlg class and choose add member variable from popup menu. Specify int as variable type and give a name m_sf. choose private access for this variable.
  8. Finely open DCMCDlg.cpp file and move to the top of file. Define variable a of type int above the class CAboutDlg starts.
  9. Finely edit code of all function as given in program code page given at the end

Refer the table given below to know the application of each function in program

Sr.No.

Function name

Application

1

OnXit

Used to quit the application

2

OnUp

Used to increase speed by 5

3

OnDwn

Used to decrease speed by 5

4

OnFwd

Rotate a motor in forward direction for 5 second with desired speed

5

OnRev

Rotate a motor in reverse direction for 5 second with desired speed

6

OnFwrd

Starts rotating motor in forward direction at constant speed

7

OnRe

Starts rotating motor in reverse direction at constant speed

8

OnStop

Stops rotating motor

These all are main features of program like speed increase or decrease rotate motor forward or reverse or stop the motor etc. Now we shall talk about additional features that are not included in DOS version. And these are pulse wise rotation and rotation of motor till mouse button is pressed and moved. For these additional features we have to add extra functions. These functions are

  1. OnLButtonDown
  2. OnRButtonDown
  3. OnMouseMove

To add these three function you have to again open class wizard and choose Object ID CDCMCDlg. You will see number of messages in near dialog box. Scroll to down and find out WM_LBUTTONDOWN event. Select is and click on to ‘ add function ‘ button. OnLButtonDown function will be generated and you will be told to edit it’s code. Right now don’t edit the code but do the same procedure to add next two function with WM_RBUTTONDOWN & WM_MOUSEMOVE events.

Now modify the code as as given in program code page given at the end

Sr.No.

Function Name

Description

1

OnLButtonDown

It is used to rotate motor pulse wise in forward direction

2

OnRButtonDown

It is used to rotate motor pulse wise in reverse direction

3

OnMouseMove

When you move the mouse with pressing left button motor will rotate forward till button is pressed. Same way with right button pressed moving a mouse will rotate motor reverse. 

After finishing all these build and run the application. Connect the LPT port with hardware circuit. Connect the DC Motor and switch on the supply. Now observe whether all the functionalities are working or not  !!!!!!! ??????  

 

 

Project Source Code

 

Project Source Code

 ###


void CTest2Dlg::OnXit() 

 

     {
         // TODO: Add your control notification handler code here
             int r;
             r = _outp(0x0378,0x00);
             OnOK();
     }
 void CTest2Dlg::OnUp() 
     {
         // TODO: Add your control notification handler code here
             UpdateData(TRUE);
             if(a<45) a+=5;
             else MessageBox("Maximum speed limit");
             m_speedfact.Format("%d", a);
             UpdateData(FALSE);
     }
 void CTest2Dlg::OnDwn() 
     {
         // TODO: Add your control notification handler code here
             UpdateData(TRUE);
             if(a>-45) a-=5;
             else MessageBox("Minimum speed limit");
             m_speedfact.Format("%d", a);
             UpdateData(FALSE);
     }
 void CTest2Dlg::OnFwd() 
     {
         // TODO: Add your control notification handler code here
             int p;
             int z = 5000/(2*m_sf);
             for(int i=0;i<z;i++) 
                 {
                     p = _outp(0x0378,0x03);
                     Sleep(m_sf + a);
                     p = _outp(0x0378,0x00);
                     Sleep(m_sf - a);
                  }
     }
 void CTest2Dlg::OnRew() 
     {
         // TODO: Add your control notification handler code here
             int p;
             int z = 5000/(2*m_sf);
             for(int i=0;i<z;i++)
                 {
                     p = _outp(0x0378,0x0C);
                     Sleep(m_sf + a);
                     p = _outp(0x0378,0x00);
                     Sleep(m_sf - a);
                 }
     }

void CTest2Dlg::OnFwrd() 
     {
         // TODO: Add your control notification handler code here
         int r;
         r = _outp(0x0378,0x03);
     }
 void CTest2Dlg::OnRe() 
     {
         // TODO: Add your control notification handler code here
             int r;
             r = _outp(0x0378,0x0C);
     }
 void CTest2Dlg::OnStop() 
     {
         // TODO: Add your control notification handler code here
         int r;
         r = _outp(0x0378,0x00);
     }

void CTest2Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
     {
         // TODO: Add your message handler code here and/or call default
         int p;
         if((nFlags & MK_LBUTTON) == MK_LBUTTON)
             {
                 p = _outp(0x0378,0x03);
                 Sleep(50);
                 p = _outp(0x0378,0x00); 
             }
         CDialog::OnLButtonDown(nFlags, point);
     }
 void CTest2Dlg::OnRButtonDown(UINT nFlags, CPoint point) 
     {
         // TODO: Add your message handler code here and/or call default
         int p;
         if((nFlags & MK_RBUTTON) == MK_RBUTTON)
             {
                 p = _outp(0x0378,0x0C);
                 Sleep(50);
                 p = _outp(0x0378,0x00); 
             }
         CDialog::OnRButtonDown(nFlags, point);
     }
 void CTest2Dlg::OnMouseMove(UINT nFlags, CPoint point) 
     {
         // TODO: Add your message handler code here and/or call default
         int p;
         if((nFlags & MK_LBUTTON) == MK_LBUTTON)
             {
                 p = _outp(0x0378,0x03);
                 Sleep(m_sf + a);
                 p = _outp(0x0378,0x00);
                 Sleep(m_sf - a);
             }
         else if((nFlags & MK_RBUTTON) == MK_RBUTTON)
             {
                 p = _outp(0x0378,0x0C);
                 Sleep(m_sf + a);
                 p = _outp(0x0378,0x00);
                 Sleep(m_sf - a); 
             }
     CDialog::OnMouseMove(nFlags, point);
 }

 

 

###

 


Circuit Diagrams

tip-h-bridge-1


Filed Under: Electronic Projects
Tagged With: dc motor, motor, vc++
 

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

Log in to leave a comment:

Lost your password?

Don't have an account? Register here

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Sensors
In this Tech Toolbox, we cover some of those technologies driving the next generation of connected systems, including ultra-low-power sensing strategies that extend node battery life, and 60 GHz CMOS radar for contactless health and presence detection.

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.

  • Solenoid AC Magnetic field water pump
  • how RS-485 transmits and receive logic levels between two MCUs
  • what is missing from this circuit?
  • x86 vs. ARM: Architecture Differences and Application Scenarios
  • Analyses and design of LDO

RSS Electro-Tech-Online.com Discussions

  • What is the most suitable light source for making a book light like this?
  • How charging is done in Transformer Inverters
  • extracting material properties from open load resonance
  • Oshon Basic, programing IF statements
  • USBASP Programming problems

Featured Tutorials

Learn - VHDL Tutorials (22 Part)

  • VHDL Tutorial – 11: Designing half and full-subtractor circuits
    VHDL Tutorial – 11: Designing half and full-subtractor circuits
  • VHDL Tutorial – 12: Design a 8-bit parity generator and circuit checker
    VHDL Tutorial – 12: Design a 8-bit parity generator and circuit checker
  • VHDL tutorial 13: Design 3×8 decoder and 8×3 encoder using VHDL
    VHDL tutorial 13: Design 3×8 decoder and 8×3 encoder using VHDL
  • VHDL Tutorial 14: Design 1×8  demultiplexer and 8×1 multiplexer using VHDL
    VHDL Tutorial 14: Design 1×8  demultiplexer and 8×1 multiplexer using VHDL
  • VHDL Tutorial 17: Design a JK flip-flop (with preset and clear) using VHDL
    VHDL Tutorial 17: Design a JK flip-flop (with preset and clear) using VHDL
  • VHDL Tutorial 18: Design a T flip-flop (with enable and an active high reset input) using VHDL
    VHDL Tutorial 18: Design a T flip-flop (with enable and an active high reset input) using VHDL
More Tutorials >

Recent Articles

  • 3D scanning: How photogrammetry is reshaping digital engineering
  • EPC launches 3-phase GaN motor-drive boards
  • TDK launches 30 nH DC-link capacitors
  • Amphenol RF launches 40 GHz solderless connectors
  • STMicroelectronics adds 200g vibration sensor for factories

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • 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 © 2026 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
      • Sensor Series
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • 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
  • Guest Post Guidelines
  • Advertise
  • Subscribe