Step 1: Look at the Circuit
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
MOSFETs are static sensitve, so be a little bit careful, but very little is critical.
Step 3: Set Up Files With PWM Values
- 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)
Step 4: Play With DOS: Configure Your COM Port and Copy the Files
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
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!
- 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.