Introduction: Serial Controlled Variable Speed Motor

Picture of Serial Controlled Variable Speed Motor

Control the speed of a small DC motor with nothing but the serial port on your computer, a single MOSFET, and some trivial software. (The MOSFET and the serial port make up the "speed control"; you'll still need a motor and an appropriate power supply for that motor; while the serial port can provide the voltage to turn a mosfet on and off, it can't supply the current needed by a typical motor.)

Step 1: Look at the Circuit

Picture of Look at the Circuit

We're going to do Pulse Width Modulation using a generic N-channel power MOSFET connected to the Transmit data pin from the computer's rs232 port. When the serial port is idle, the pin will sit at the "1" state, which by the time it's translated to rs232, is something like -12V (depending on drivers, it might be closer to -9V or -5V), and the transistor will be quite OFF. When we transmit "0" bits on the serial port, the rs232 pin will go to +12V or so, which is enough to turn on most mosfets pretty well.

If we transmit a lot of "0" bis in a row, the motor will be close to fully ON and the motor will run fast. If we transmit mostly "1" bits, the motor will run more slowly.

Step 2: Wire It Up

Picture of Wire It Up

Since there's only a single component and only a few connections, you can just add wires "freeform."
MOSFETs are static sensitve, so be a little bit careful, but very little is critical.

Step 3: Set Up Files With PWM Values

Picture of Set Up Files With PWM Values
One way to control the motor without having to write ANY software is to prepare some files containing appropriate bytes (with more or less 0 bits), and simply COPY them to the COM port where you have the motor connected. I prepared several files (using emacs, but whatever works for you is fine):

  • 0.pwm:: contains 5000 NULL characters (control-space on most keyboards)[br] This is about as close to "full speed" as we'll be able to get with this technique.
  • 1.pwm:: contains 5000 control-A characters (ascii 01) (one "1" bit per char)
  • 3.pwm:: contains 5000 control-C characters (ascii 03) (two "1" bits per char)
  • 7.pwm:: contains 5000 control-G characters (ascii 07) (three "1" bits per char)
  • 15.pwm:: contains 5000 control-O characters (ascii 15) (four "1" bits per char)
  • 31.pwm:: contains 5000 control-_ characters (ascii 31) (five "1" bits per char)
  • 63.pwm:: contains 5000 "?" characters (ascii 63) (six "1" bits per character)
  • 127.pwm:: contains 5000 DEL characters (ascii 127) (seven "1" bits per character)

(Now that I've drawn pictures, you'll notice that the actual bit patterns aren't ideal. Since rs232 serial transmits LSB first, we really want to shift in zeros instead of ones. An excercise for the student!)

Step 4: Play With DOS: Configure Your COM Port and Copy the Files

Picture of Play With DOS: Configure Your COM Port and Copy the Files

9600 bps is a common bitrate. It matches nicely to "about" one byte per millisecond, so in this case it corosponds to a PWM frequency of 1000Hz, which I think ought to be ok for smallish motors. You can experiment with different bit rates to see how things work, which is one of the advantages of this method.

Create a DOS (or "Command prompt") window (assuming you're using a windows OS), and configure
your com port like:

mode com1: 9600,n,7,1"

That tells the comm port to run at 9600bps, and send 7 bits in each character (to match up with our 7 different bit-lengths.) The "n" means NO parity, so those will be the only data bits. The "1" means there will be one "stop" bit, which will prevent us from turning the motor all the way on (oh well.)

So now you can turn on the motor with commands like:

copy 0.pwm com1:

Since we're sending 5000 characters at about 1 per millisecond, the motor should turn on at close to full speed for about 5 seconds. If you want less than 5 seconds, make a shorter file. Similarly, you can do:

copy 127.pwm com1:

to run the motor at the lowest possible speed. With the setup I had, the motor wouldn't turn at all with anything "slower" than 31.pwm, but YMMV (I think I had a 12V motor running off 5V of batteries.)

The COPY command lets you string files together, so if you want your motor to speed up and then slow down again, you can do something like:

copy 31.pwm+15.pwm+7.pwm+0.pwm+7.pwm+15.pwm+31.pwm com1:

Step 5: Control the Motor From a Program

Picture of Control the Motor From a Program

If you're writing a program, you can probably open COM1: as a file and simply write to it as if it were any other file. Being able to time the periods that the motor is on by outputting a particular number of characters would seem to be very handy. Don't forget that the system is very likely to buffer the characters that you send to the serial port, so just because a WRITE call returns doesn't mean that the motor has finished doing whatever you told it to. Since we don't do anything "fancy" with the com port signals, you shouldn't have to investigate the arcane options that it might support. (although, if you can figure out how to send a BREAK sequence to the com port, that's a continuous "0" state, and will drive the motor ALL the way on; more than sending continuous 0 characters.)

If your programming language doesn't let you output to COM1:, you may still be able to control the motor by "calling" DOS to do copy commands.

(OK. I've downloaded Microsoft's Visual Basic Express 2005 (which is free) and managed to tie a horizontal scroll bar to the motor speed, controlled via the serial port. zip attached. It's probably got more than it needs to duplicate the program on your system, but I couldn't figure out exactly which bits were needed. The program is both simplified and made more difficult to understand (sorry) by being multi-threaded. One thread does nothing but output to the serial port, and the main thread reads the scroll bar and updates info used by the serial thread.)

Step 6: Experiment!

Picture of Experiment!
If things are basically working, this provides an awful lot of room for experimentation.

  • Fix my bit patterns!
  • Does the bitrate matter much?
  • Do you have to control the width of "on" and "off" pulses, or is simply controling their ratio sufficient?
  • If you only have to control the ratio, you can consider multi-character sequences at higher bit rates to get more speed levels. Outputing 0 followed by 127 would be about half on.
  • This should work for dimming flashlight bulbs, too.

Comments

Claudiaf6 (author)2015-08-04

This is a nice idea indeed. I just would like to controll (dim) several lights this way. Is this also possible with a usb to parralell converter? so i can manage at least 8 lights?

joshiii92 (author)2015-06-27

Hi, just wondering if you can help? I tried your circuit using a mosfet from Freetronics and connected it according to their diagram (using GDS etc, because your diagram varied slightly to theirs).. I have a small relay connected to the mosfet, but after trying it, nothing happened, so I swapped the serial Ground & TxD pins and it worked... kinda. The problem is that its kinda working backwards - the relay is always energised (its on, cos I can hear it click right away) and when I send a command to the com/serial port, it switches it off instead. It's almost like the mosfet is working backwards - but im guessing i've just made a dumb mistake... any help would be appreciated. ohhh ignore the Arduino board thing in the pic, im just connecting it to my PC. thanks heaps...

Claudiaf6 (author)joshiii922015-08-04

A relays is to slow to switch high frequency's. The mosfet will stay high all the time then. The mosfet handles high frequencies so that it switches on and of very fast, and with that the engine goes faster or slower if the frequency on the rs232 port is changing. So remove the relay and choose the right mosfet that can handle the power from the engine, but also switches at the voltage from the RS232 port.

westfw (author)joshiii922015-07-18

Which MOSFET, exactly? A P-channel mosfet will in fact behave essentially "opposite" an N-channel mosfet...

cylove (author)2010-07-29

can i have the codes for this?thank you!

westfw (author)cylove2010-07-29

They should be right there in step 5 in the ComSpeed.zip file...

profpat (author)2010-04-04

 greetings,

i tried this design using a desktop instead, an IBM P3 model 300PL, it didnt work, whenever i type copy 0.pwm com1:, the dos prompts writes " WRITE FAULT ERROR WRITING DEVICE COM1" 
abort, retry, ignore, fail?

in order it to work, i wired the 9pin dsub as  a null modem...now it worked..

to make a 9pin null modem..
connect pins 3 to 2, pins 7 to 8, pins 1,4,6 and 9...

now connect gate of mosfet to pin 2 and 3, and ground wire to pin 5...

it works great...

have a nice experiments..



profpat (author)2010-01-21

 great! will try this, hope it will work on my laptop using a usb to serial port cable..

MACKattacksnipe (author)2009-10-11

i used this for my pc tube light and giant cooling fan

HADJISTYLLIS (author)2009-04-19

I founded one on old graphics card!

geeklord (author)2009-03-26

Will one of these work? I found them on an old mother board.

geeklord (author)westfw2009-03-28

Thanks, what kind of voltage/current is at the gate of the MOSFET when its on?

westfw (author)geeklord2009-03-28

See "step 1"

TOCO (author)2009-02-27

I have downloaded your link. How do I use it? If I click on it, it will say illegal command or somthing like that. Do I need to hook up the motor first or can it be opened without pluging it in?

westfw (author)TOCO2009-02-28

It doesn't need to have the motor first, but you may need to get visual basic studio and compile with the proper com port defined? I really don't remember exactly what's in there; the instructable was initially published over two years ago...

westfw (author)westfw2009-02-28

(I'll try to take a look at it if I get a chance. But I'm pretty behind on a lot of things right now...)

TOCO (author)2009-02-25

Could you send me all of the software or links for the use of your serial port controlled motor? Please! I think that you should probably put the links onto the instructable so people can use it easier. I mean so they can build it and use it. And also, I have a few differend mosofets and they say different things on them. Can I use them or do I have to buy a new one?

raj007 (author)2009-02-14

yaa pls send me a sotware to control multiplestepper motors

cupnoodles2 (author)2009-01-30

Can this sistem controll 2 motors? i mean is there anyway? thanks my email is positiveguy11@yahoo.com Thanks

westfw (author)cupnoodles22009-02-07

Not really. (you could use two com ports simultaneously, I guess.) It's pretty much just a cute hack; probably not useful for anything real except to demonstrate the principle. For real PWM apps, you ought to go and put a $5 microcontroller in there... (alas, by the time you have the $5 microcontroller on a PCB with the extra electronics that will let it drive MOSFETs and talk to your computer, you're usually talking about something a lot more than $5. Which is one of the things that makes this particular hack so neat.)

rab63 (author)2008-11-27

thanks for prompt reply westfw. will look at getting a usb to serial adaptor. thanks again.

moordere (author)rab632008-12-08

yes

rab63 (author)2008-11-26

hi, all How can i do the same using pinter port.

westfw (author)rab632008-11-26

significantly harder, since the parallel port isn't "self-timed" the way serial is.

Metaferia (author)2008-08-20

interesting ... just a quastion, how many volts can a serial port can give us? what about a USB port? thanks

combomatrix (author)2006-12-15

Really nice thing to work on. Controlled by a program.... Is there any way to do this using an USB cable attached to a similar circuit from a USB port?

westfw (author)combomatrix2006-12-15

It should work fine connected to a USB->serial adaptor. I don't think there's a simple circuit that would connect directly to USB. There's too much software negotiation that has to go on.

Derin (author)westfw2008-06-22

the only way is a ftdt chip

adi12104 (author)2008-05-04

sorry about my previous comments i justy realize that i havent install the vb.net program so i cannot run the execute file and ask for some serial number by the way sir i still cannot find the 0.pwm 1.pwm 3.pwm 7.pwm 15.pwm 31.pwm 63.pwm 127.pwm in your zipped file

westfw (author)adi121042008-05-05

Ah. I hadn't realized that you had to install the shared libraries (or whatever) before it would work; they must have been present on the several systems I tried it out on (I think.) The 0.pwm and etc were examples; they should be easy for anyone to create from scratch. Although putting them in the zip file is a fine idea and I'll see what I can do.

adi12104 (author)2008-05-01

pardon me somebody please give me the file of 0.pwm 1.pwm 3.pwm 7.pwm 15.pwm 31.pwm 63.pwm 127.pwm by the way can any one tell me ho to use the com speed zip file because when i opened it it want the pass

westfw (author)adi121042008-05-02

That's odd; I just downloaded the zip to a different system, and it didn't ask for any password...

Sonic (author)2007-11-14

how risky is this project? I mean there is some obvious risk of damaging the mobo or killing the computer... isnt it? ..or is it safe?

westfw (author)Sonic2007-11-14

I would think that it is relatively safe. The biggest danger would be that you could somehow mis-wire things, and get the motor power supply connected (wrongly) to your serial port. Even then, serial ports are relatively "robust", and I don't think you should burn out more than the rs232 driver chip (which is before the more important functions of the motherboard.) I supposed that will really beefy motors and really beefy motor power supplies, there might be some danger of getting the backEMF spikes onto the motherboard, causing problems, so ... don't do that. Why do you think there is "obvious risk"? I could be missing something... A separate serial card or USB/serial converter would provide additional isolation, but sorta defeats the "simple and cheap" aspect of the project.

Silence (author)westfw2008-01-18

Use diodes... will isolate the wires from feedback.

robin4286 (author)2007-11-11

Is there a way I can use the serial port to turn a single LED on or off using similar methods? Thanks!

robin4286 (author)robin42862007-11-12

hmmm okay so it would seem I have a problem: So when I copy a file (made in notepad, using alt+01 repeatedly, it is a .txt format) of 5000 bytes, in windows xp to com4, which is a USB-to-db9 serial adapter (by cables to go, if it helps), it sits there and does absolutely nothing, no errors, but also no confirmation of sending. I checked the settings multiple times and had them set properly. I used a multimeter to check charge and it stayed at about -6.10 volts(Or maybe positive, but I didn't think so). The multimeter(and an LED) were connected from pin 3 to pin 5. I think the actual problem was in the copy in dos, as it gave no confirmation as it did when I tried a parallel port, and basically locked up, so I had to close the window. Could it be that this does not work in windows XP? Any Ideas on what the problem would be? Thanks!

westfw (author)robin42862007-11-12

Did you use "mode" to set the serial parameters first? I've had similar problems with a usb->serial adaptor on a Mac; I wonder if the USB converter loses some initialization information in between "open" calls (you wouldn't think so, but...) Do you have a dumb ascii upload utility you can try instead? Someone that you know successfully talks to the serial port via USB (hyperterm?)

robin4286 (author)westfw2007-11-13

Oh wow I feel like an idiot now... This feels like Java all over again (or javascript, or C#). 3 years of programming and I still forget the Semi/colons I forgot to put the colon at the end of the copy statement... Really sorry to trouble you!

westfw (author)robin42862007-11-12

You can probably get enough current from a serial port output pin to drive a (small) LED directly, and there are at least three pins you can use for output (Data, DTR, RTS.) After all, I made this "breakout box", and similar commercial devices used to be very common (and very necessary!) back when serial ports were common.

foxwestjava (author)2007-05-13

Does anyone know the most universal mosfet series i use for that ? Thank's

Sonic (author)2007-02-17

I still haven't tried this yet, but does it go backwards also? if not, can it be done with reprogramming? or do you have to use a different circuit?

westfw (author)Sonic2007-02-17

No; you could probably add backwards with a DPDT relay driven from one of the modem control signals. It would have to be a relatively low current relay, though.

squares (author)2006-12-24

I notice that although you haven't listed a peripheral battery power supply in the materials, but you've shown one in some pictures and included it in the circuit's schematic. The serial port is capable of outputting up to 15VDC, why is an extra supply required for such small DC motors?

westfw (author)squares2006-12-24

The serial port has a signal up to18V or so, but the spec says it can only supply something like 10mA max, which isn't enough to run even a very efficient motor. I added some additional text to step 1 to clarify this.

phatso (author)2006-11-16

Here's an opportunity for somebody to make a buck: a box that plugs into USB and emulates a serial or parallel port.

_soapy_ (author)phatso2006-11-18

What would be a market success would be a simple USB powered relay system, like the parallel and serial break-out boxes you used to get which would drive things directly. The enhanced data rate would be great, and a simple HID driver would be all you would need.

_soapy_ (author)_soapy_2006-11-25

I found that there is a board to do this! It's USB, and made by Velleman. In fact, they make quite a few neat sounding things for USB interfacing. See http://www.vellemanusa.com/us/enu/product/search/?id=usb for the range. I'm going to have a browse.

westfw (author)phatso2006-11-16

You can buy those off the shelf for about $20, or in chip form for about $4. They would work for this hack, but the problem is that by the time you get through WXP and a USB driver AND some third party hardware and firmware in the emulator, the port is (potentially, anyway) no longer good for some of the nefarious purposes for which hackers used to use the serial and parallel ports (assorted PIC programmers, for instance.) You no longer have time-accurate bit-twiddling available (and I think some of the parallel port emulators won't do much of anything other than drive a printer.)

About This Instructable

56,293views

94favorites

License:

Bio: Middle aged geek username also works at yahoo.com, mac.com, comcast.net, wharton-10.arpa
More by westfw:Visualizing EAGLE Edits with a Gerber ViewerMake a Double CD/DVD Case From a Single Sheet of PaperSingle Sided Really Bare Bones Board Arduino in EAGLE.
Add instructable to: