PC Based Stepper Motor Controller

By Ashutosh M. Bhatt


Mouse interfacing

This is the most interesting part of the program. It enables you to perform a task at a click of mouse. To understand how the mouse is interfaced, you have to go through the entire theory of hardware interfacing using ‘C++.’ Here, only some references have been made. For details, refer to the ‘Mouse Interfacing’ chapter of ‘Let Us C’ book by Kanitkar.

In this program, the functions that handle the mouse event are initmouse( ), resmptr(int p, int q, int r, int s), showmptr( ) and getmpos(int *t, int *u, int *v).


The initmouse( ) function loads mouse driver into the program. You’ve to pass ‘0’ value through input union REGS to the int86( ) function. This function will return some non-zero value through output union REGS to the main program. If this function returns ‘0,’ it means the mouse driver is not loaded. So the program displays the message “mouse driver is not nhbloaded” and shuts the screen off using the exit( ) function.

The resmptr(int p, int q, int r, int s) function restricts mouse movement within the boundary specified by the four variables passed to it. Pass all these boundary limits through input union REGS to the int86( ) function. So the resmptr(int p, int q, int r, int s) function will restrict mouse movement out of this boundary.

The showmptr( ) function displays mouse pointer on the program screen. For this, you just have to pass value ‘1’ through input union REGS to the int86( ) function. The showmptr( ) function will now show mouse pointer on the screen.

The getmpos(int *t, int *u, int *v) function performs two tasks: determines whether the mouse button is pressed or not, and captures the current mouse pointer position from the screen. You have to pass value ‘3’ through input union REGS to the int86( ) function. This function will return ‘x’ and ‘y’ coordinates of the mouse pointer and also return value ‘1’ if the mouse button (left) is pressed or ‘0’ if the button is not pressed.

How the program works?

The program output screen includes the control panel for speed, direction and number of rotations of the stepper motor.

The program continuously checks for mouse-click event. Whenever there is a mouse click, the getmpos( ) function instantly captures ‘x’ and ‘y’ coordinates of the mouse pointer and passes them to the main program.

The main program decides on which position the click event has occured. If the click event occured on any button (clockwise, anti-clockwise, etc), it performs the desired task. For example, if you click the ‘RPM increase’ button, the program gets the coordinates and directly switches them to ‘if’ loop, increases the RPM and also displays it on the screen.

Construction and operation

Construct the hardware on a breadboard or on a PCB. A single side PCB layout is shown in Fig. 5 and its component layout in Fig. 6. Connect the bases of the transistors to the respective data pins of the port DB25 (25-pin, D type male connector) as shown in Fig. 1. Insert DB25 into the PC’s LPT-port female connector. Connect all the coil terminals (red, orange, green and yellow) to respective points as shown in the schematic.

Apply 5V DC supply to the circuit and connect the 5V stepper motor with its terminals as shown in Fig. 1. Now run the program on a computer powered by Windows 95/98 operating system. You will see the control panel on the computer screen. Switch on the 5V supply and LED1 will glow. Move the mouse pointer to any of the buttons as desired.

Fig. 5: Actual-size, single-side PCB layout for PC-based stepper motor controller
Fig. 5: Single side PCB layout for PC based stepper motor controller
Fig. 6: Component layout for the PCB
Fig. 6: Component layout for the PCB

Download PCB and component layout PDFs: click here

To rotate the motor clockwise, press and hold ‘clockwise’ button with left mouse button on the control panel. Similarly, for anticlockwise rotation, press and hold ‘anticlockwise’ button. The motor will rotate in the desired direction along with the beep sound as long as the button is kept pressed. When you release the button, the beep sound as well as the motor will stop.

If the motor rotates in anticlockwise direction when you press ‘clockwise’ button, just reverse any pair of terminals of the motor coils.

Run the stepcnt.exe file on your computer. You will see the control panel for the PC based stepper motor controller on your screen. Default RPM and number of rotations are 60 RPM and 1, respectively. If you press ‘clockwise’ or ‘anticlockwise’ button, the motor will rotate until the desired rotations complete. You can increase or decrease the RPM or even the number of rotations by simply left-clicking that button once. Pressing these buttons more than once will increase/decrease the RPM/number of rotations by the same amount.

The RPM and the number of rotations are perfectly calibrated for this particular stepper motor and you will get the accurate result for the RPM. That means if you choose 1 RPM, the motor will complete one revolution in one minute exactly. For the number of rotations also, if you choose one rotation, the motor will complete only one rotation. If you choose 0.25, the motor will complete only quarter revolution (90o).


The stepper motor you choose should have the same specifications as given in this project. For checking the specifications, the stepper.cpp sample program in ‘C++’ has been included in the EFY-CD. In the program, first enter RPM as ‘60’ and then the number of rotations as ‘1.’ Select clockwise or anticlockwise direction and then press ‘Enter’ key.

The program will now display the PRF (=20 Hz) and the current RPM (=60) with a melodious sound output.

If the motor doesn’t rotate, it means you have connected its four terminals wrongly. You can correct this using trial-and-error method. If the motor runs in a direction opposite to that you have selected, just reverse one pair of the coil terminals.

Because you have entered one rotation with 60 RPM, the motor will complete only one rotation in one second. If it doesn’t, your stepper motor has some different specification. To check the specifications of the stepper motor, in the sample program select RPM as ‘5’ and change the number of rotations (like 1.25, 1.5, etc) to get the step resolution. The delay factor (d) can be changed in the software. When you are satisfied with the result, switch to main program ‘stepcnt.exe’.

Download source code: click here

The article was published n October 2005 and has recently been updated.


Please enter your comment!
Please enter your name here