Control Con Computadora
Control Con Computadora
by Denkimono
This is a simple method to quickly enable USB control of external devices without using microcontrollers or expensive dev boards. Both input and output are possible allowing two way interaction with the environment. The pro!ect is based around a generic USB"Serial converter and a small number of discrete components likely to be found in any well"stocked !unk box. #ode examples are given for control of the interface and simple to construct examples are given throughout. $ originally developed this interface to build an office control system but have since used it on many pro!ects where $ wanted quick results and didn%t need the complexity of full USB control. Preview of the steps involved: &. 'verview " how does it work( ). 'utput *. 'utput examples+ ,-D control and motorised flashing beacon notification .. $nput /. $nput example+ signature hotkey 0n arbitrary number of interfaces can be controlled by the same host machine via hubs etc. which combined with the low cost of USB"Serial converters makes this a great starting point for many pro!ects.
Step 1: Overview
The pinout of a standard 1"pin serial connector is shown in the diagram. $n addition to the 2xD and TxD data lines a number of signals are exposed that can be put under software control. These are the modem control lines. 3or input these are D#D DS2 2$ #TS while for output we can use DT2 and 2TS. The 2S)*) logic levels for the modem control lines are logic 4$54 6&7+ anywhere between *8 and &/8 logic ,'9 6:7+ anywhere between "*8 and "&/8. ;ote that this is the reverse of those used by the 2xD and TxD serial data line levels. These levels mean they can%t be interfaced directly with the TT, logic levels of 4$54 </8 and ,'9 :8. The signals also have a high source impedance. 9hile this means that they can stand being short circuited it also means that you really can%t power anything with a significant load. So in order to be useful our interface needs to convert these signals to TT, levels and reduce the source resistance to allow higher power devices to be controlled. $n this example we%ll !ust be controlling one input 6#TS7 and one output 62TS7 but the same method can be applied to use the other available lines for control. 3or instance by using both the 2TS and DS2 lines we could control external $=# devices by bit"banging the SD0 and S#, lines. So let%s start by enabling output...
Step 2: Output
To convert an 2S)*) output to TT, levels we simply apply the 2S)*) signal to the base of a transistor in common emitter configuration. This allows us to set the voltage to our desired levels of
:8 and /8. The addition of the diode prevents damage to the transistor due to the reverse base" emitter voltage when the 2S)*) input is logic ,'9 6"*8 to "&/87.
The USB specification allows USB ports to drive a load of up to &::m0 which even allowing for the power required for the adapter is plenty to drive an ,-D. The 2S)*) to ,-D driver is shown in the first image. $f you can open your USB serial adapter then you can tap the </8 USB supply to drive this low power circuit. 4owever it%s much more rewarding to use an external supply and drive higher power devices. 3or example a rotating beacon. $ found these battery powered beacons recently and converted one for USB control. The circuit in the second image uses two transistors in Darlington configuration with U) a higher power device than U& enough to supply a few hundred milliamps to the beacon. The beacon is simply a motor in parallel with an incandescent bulb so $%ve added a small capacitor to suppress noise from the motor and a diode to prevent the back ->3 from the motor damaging the transistor. The attached file %serialcontrol.c% can be used to control both these circuits. $t allows the level on the
2TS line to be switched from the command line for example+ serialcontrol ?dev?ttyUSB: on This makes it simple to control the beacon from a script allowing you to make an incoming email indicator or a server monitor indicator etc. ;ote that the circuits in both of these examples invert the input so you%ll have to turn the port %off% to switch the device on. ;ow we !ust need to capture some input and we can really begin to interact with our environment@ serialcontrol.c& AB
Step #: $nput
$nput is a little more tricky. 9e can use the </8 supply to signal logic 4$54 as this is !ust within the 2S)*) spec but we need to generate a negative voltage to signal 2S)*) logic ,'9. 9hat we can do is use the voltage present on the adapter%s redundant TxD pin. 9hen the port is idle 6no data output7 this line will be at logic 4$54 6"*8 to "&/8 " remember the 2xD and TxD lines use inverted logic levels7. 9e can use this as a negative voltage source for our converter. This time we use a B;B transistor in common emitter configuration to set the new voltages. The value of 2* needs to be chosen based on the serial converter used as it%s sensitive to the converter%s input impedance. $ use a value of &A but you may need to experiment.
0 simple input example is the Csignature hotkeyC. This allows a preset word or phrase to be automatically pasted when an external button is pressed. This is particularly useful if like me you have an awkward surname or if you have to end all your documents with a set phrase. 3or example a British railways employee might want to assign the phrase C9e apologise for any inconvenience caused to your !ourney.C The attached file %hotkey.c% gives an example of a daemon process that monitors the external hotkey. 9hen the hotkey is pressed the daemon sends the phrase to the currently active window as a series of keypresses. This saves me considerable time when filling in online forms or writing letters. The daemon uses T$'#>$90$T to sleep while waiting for a keypress and then sleeps for a few milliseconds afterwards to debounce the key before sending the phrase.
hotkey.c* AB
#ontrol the speed of a small D# motor with nothing but the serial port on your computer a single >'S3-T and some trivial software. 6The >'S3-T and the serial port make up the Cspeed controlCD you%ll still need a motor and an appropriate power supply for that motorD 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.7
9e%re going to do Bulse 9idth >odulation using a generic ;"channel power >'S3-T connected to the Transmit data pin from the computer%s rs)*) port. 9hen the serial port is idle the pin will sit at the C&C state which by the time it%s translated to rs)*) is something like "&)8 6depending on drivers it might be closer to "18 or "/87 and the transistor will be quite '33. 9hen we transmit C:C bits on the serial port the rs)*) pin will go to <&)8 or so which is enough to turn on most mosfets pretty well. $f we transmit a lot of C:C bis in a row the motor will be close to fully '; and the motor will run fast. $f we transmit mostly C&C bits the motor will run more slowly.
Step 2: ,ire it up
Since there%s only a single component and only a few connections you can !ust add wires Cfreeform.C >'S3-Ts are static sensitve so be a little bit careful but very little is critical.
'ne way to control the motor without having to write 0;E software is to prepare some files containing appropriate bytes 6with more or less : bits7 and simply #'BE them to the #'> port where you have the motor connected. $ prepared several files 6using emacs but whatever works for you is fine7+ :.pwm++ contains /::: ;U,, characters 6control"space on most keyboards7FbrG This is about as close to Cfull speedC as we%ll be able to get with this technique. &.pwm++ contains /::: control"0 characters 6ascii :&7 6one C&C bit per char7 *.pwm++ contains /::: control"# characters 6ascii :*7 6two C&C bits per char7 H.pwm++ contains /::: control"5 characters 6ascii :H7 6three C&C bits per char7 &/.pwm++ contains /::: control"' characters 6ascii &/7 6four C&C bits per char7 *&.pwm++ contains /::: control"I characters 6ascii *&7 6five C&C bits per char7 J*.pwm++ contains /::: C(C characters 6ascii J*7 6six C&C bits per character7 &)H.pwm++ contains /::: D-, characters 6ascii &)H7 6seven C&C bits per character7 6;ow that $%ve drawn pictures you%ll notice that the actual bit patterns aren%t ideal. Since rs)*) serial transmits ,SB first we really want to shift in Keros instead of ones. 0n excercise for the student@7
Step #: Pla( with "OS: confi&ure (our )O+ port and cop( the files
1J:: bps is a common bitrate. $t matches nicely to CaboutC one byte per millisecond so in this case it corosponds to a B9> frequency of &:::4K which $ think ought to be ok for smallish motors. Eou can experiment with different bit rates to see how things work which is one of the advantages of this method. #reate a D'S 6or C#ommand promptC7 window 6assuming you%re using a windows 'S7 and configure your com port like+ mode com1: -.00/n/0/11 That tells the comm port to run at 1J::bps and send H bits in each character 6to match up with our H different bit"lengths.7 The CnC means ;' parity so those will be the only data bits. The C&C means there will be one CstopC bit which will prevent us from turning the motor all the way on 6oh well.7
So now you can turn on the motor with commands like+ cop( 02pwm com1: Since we%re sending /::: characters at about & per millisecond the motor should turn on at close to full speed for about / seconds. $f you want less than / seconds make a shorter file. Similarly you can do+ cop( 1202pwm com1: to run the motor at the lowest possible speed. 9ith the setup $ had the motor wouldn%t turn at all with anything CslowerC than *&.pwm but E>>8 6$ think $ had a &)8 motor running off /8 of batteries.7 The #'BE 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+ cop( 312pwm31%2pwm302pwm302pwm302pwm31%2pwm3312pwm com1:
$f you%re writing a program you can probably open #'>&+ 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 !ust because a 92$T- call returns doesn%t mean that the motor has finished doing whatever you told it to. Since we don%t do anything CfancyC with the com port signals you shouldn%t have to investigate the arcane options that it might support. 6although if you can figure out how to send a B2-0A sequence to the com port that%s a continuous C:C state and will drive the motor 0,, the way onD more than sending continuous : characters.7 $f your programming language doesn%t let you output to #'>&+ you may still be able to control the motor by CcallingC D'S to do copy commands. 6'A. $%ve downloaded >icrosoft%s 8isual Basic -xpress )::/ 6which is free7 and managed to tie a horiKontal scroll bar to the motor speed controlled via the serial port. Kip attached. $t%s probably got more than it needs to duplicate the program on your system but $ couldn%t figure out exactly which bits were needed. The program is both simplified and made more difficult to understand 6sorry7 by
being multi"threaded. 'ne 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.7 #omSpeed.KipJ) AB
Step .: !xperiment4
$f things are basically working this provides an awful lot of room for experimentation. 3ix my bit patterns@ Does the bitrate matter much( Do you have to control the width of ConC and CoffC pulses or is simply controling their ratio sufficient( $f you only have to control the ratio you can consider multi"character sequences at higher bit rates to get more speed levels. 'utputing : followed by &)H would be about half on. This should work for dimming flashlight bulbs too.