|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
WarningThe PC parallel port can be damaged quite easily if you make incorrect connections. If the parallel port is integrated to the motherboard, repairing a damaged parallel port may be expensive, and in many cases, it is cheaper to replace the whole motherboard than to repair that port. Your safest bet is to buy an inexpensive I/O card which has a parallel port and use it for your experiment. If you manage to damage the parallel port on that card, replacing it will be easy and inexpensive. DisclaimerWhile every effort has been made to make sure the information in this article is correct, the author cannot be made liable for any damages, whatsoever, for loss relating to use or implementation of this article. Use this information at your own risk. IntroductionIn this article, we will connect a 3½ inch floppy drive to our computer's parallel port and write a program to control its stepper motor. We won't be taking the stepper motor out from the floppy drive because a floppy drive has a built in controller which can be easily used for controlling its stepper motor. The advantages of this are:
However, if you have a good background of electronics and you're interested in controlling a stepper motor without the disk drive electronics, read Stepper Motor Control through Parallel Port by Bhaskar Gupta. Before we begin, I would recommend you to go through I/O Ports Uncensored - 1 - Controlling LEDs (Light Emitting Diodes) with Parallel Port by Levent Saltuklaroglu and be sure to read the sections on Parallel Ports and Hexadecimal / Decimal / Binary if you haven't already done so. Also, make sure that the floppy drive you use is in working order. I've wasted an entire day trying to make a broken one work. It's a waste of time. Stepper MotorsWhat they are?
So, what are stepper motors and how are they different from conventional electric motors? Simply put, a stepper motor is a brushless, synchronous electric motor that can divide a full rotation into a large number of steps. Conventional motors spin continuously while a stepper motor moves only one step at a time. Therefore, stepper motors are useful for precise motion and position control. How they work?
The simplest way to think of a stepper motor is a bar magnet and four coils. When current flows through coil "A" the magnet is attracted and moves one step to the right. Then, coil "A" is turned off and coil "B" turned on. Now, the magnet moves another step to the right and so on… A similar process happens inside a stepper motor, but the magnet is cylindrical and rotates inside the coils. For a stepper motor to move, these coils should be turned on in the correct sequence. However, we don't have to worry about this since we will be using the floppy drive's built in controller. The Floppy CableThe floppy cable is usually a flat, gray ribbon cable similar to the standard IDE cable. The floppy cable has 34 wires (odd colored wire is wire 1). There are normally five connectors on this cable, but some cables, like the ones I'm using, have only three. These connectors are grouped into three sets:
An image from The PC Guide illustrates:
3.5" Drive Connector pin-out
Floppy Power Connector
Pin-out
NoteSome connectors might supply only two wires, usually the +5 V and a ground pin. This is because the floppy drives in most new systems run only on +5 V and do not use the +12 V at all. Making the connectionsThis part is really easy. All you need are:
Connect the 3.5" Drive "A" Connector on your floppy cable to your floppy drive. Now, make sure your computer's off and unplugged. Open your computer and take out the floppy power connector. Carefully plug this connector to your floppy drive. Reversing the red and yellow wires could fry your floppy drive. You'll see five notches on the power connector. They should point upward when they're installed. Fortunately, these connectors are keyed and therefore difficult to insert incorrectly. Check out the picture below:
NoteIf you want, you can extend the power connector cord so that the floppy drive can reside outside the PC case. Just cut the wires (1 red, 1 yellow, 2 black) and patch in a couple feet. There's no need to extend the yellow wire since it carries +12 V, which will not be used by the floppy drive. As you can see in the picture above, I've only extended 1 red and 1 black. Now, take the motherboard end of the floppy cable and follow the instructions below to connect it to the parallel port cable. Here's a diagram I've made to show the connections:
Parallel Port Floppy Cable ConnectorPin # 2 (D0)------> Pin # 20 (Step Pulse) Here's a picture of the connections I made:
I was using a defective parallel port cable and so my parallel port pins are in reverse order. Don't let that confuse you. Just go with the diagram. Finally, connect the other end of the parallel port cable to your computer. That's it! Make sure all your connections are correct and there are no short circuits. Writing the codeIt's time to write some code. This is a fun and tricky part. It's tricky because even a small bug in your program could prevent the stepper motor from moving. I have used inpout32.dll for interoping. Download it from http://www.logix4u.net/inpout32.htm [^]. After downloading it, put it in your System32 folder and import it into your project: using System;
using System.Runtime.InteropServices;
private class PortAccess
{
[DllImport("inpout32.dll", EntryPoint="Out32")]
public static extern void Output(int address, int value);
}
For sending values to our parallel port, we'll be using For moving the stepper motor, we will have to pulse pin 20 on the floppy drive connector. The direction of movement will depend on the high/low state of pin 18. Now, since pins 20 and 18 are connected to pins 2 (D0) and 3 (D1) on the parallel port, pulsing pin D0 will move the stepper motor and its direction will depend on the high/low logical state of D1. So, here's a sample code for moving the stepper motor 10 steps in one direction: for (int i = 0; i < 10; i++)
{
PortAccess.Output(888, 1);
System.Threading.Thread.Sleep(50); // Delay
PortAccess.Output(888, 0);
System.Threading.Thread.Sleep(50); // Delay
}
I'm sending the values 1 and 0. 1 (Decimal) = 0001 (Binary) Here, I'm changing the high/low state of D0 but I'm keeping D1 constantly low. Therefore, the stepper motor will move 10 steps in one direction. Notice that I'm delaying the execution of the code after sending a value. This delay is needed to provide enough time for the magnetic field inside the coils to build up and move the magnet. Without this delay, the coils will switch on and off so fast that the magnet wouldn't move. To move the stepper in the other direction, send the values 3 and 2: for (int i = 0; i < 10; i++)
{
PortAccess.Output(888, 3);
System.Threading.Thread.Sleep(50); // Delay
PortAccess.Output(888, 2);
System.Threading.Thread.Sleep(50); // Delay
}
3 (Decimal) = 0011 (Binary) Here, I'm changing the high/low state of D0 but I'm keeping D1 constantly high. In future, if you plan to use pins other than D0 and D1, always make sure that the values you send are correct. The Windows Calculator can be helpful for performing binary to decimal conversions.
The first time I tried controlling a floppy drive stepper motor, I chose wrong values and my stepper wouldn't budge! I was checking the connections over and over but I had no clue that the problem was in my program! I wasted at least two days because of this. Well, here's a screenshot of my 'working' application:
ConclusionWe have reached the end of this article. I hope you enjoyed it and successfully controlled your floppy drive stepper motor. Now what? Just let your imagination go wild!! Stepper motors can be used to perform a variety of small tasks which require precise motion/position control (for e.g. in robotics). I used mine to pan a camera! Check it out on my blog: http://ashishrd.blogspot.com/2006/11/camera-panning-using-parallel-port.html[^]. If you end up making something interesting, I'd love to hear about it. Happy coding!! History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||