Hacking USB input
April 17, 2010 10:31 PM   Subscribe

Is there an easy way to hack a USB port to give meaningful input to a program when a brief electrical connection is made? I'd like to create a device to start and stop a timer on a laptop.

The idea is to create a computer program to run skateboard races. We normally use tapestrips to trigger the start/ finish signals in a hobby box timer. I'd like to hire a programmer to write a program that would move the process onto a laptop. This would simplify the running and timing of our races, but don't know how to make the interface with the tapeswitches.
I had thought of sacrificing a USB mouse, and soldering the wires connected to the tapestrips across the "mouse click" buttons. Would this be likely to work? Would the connection be too brief to be registered? Would the length of wire (several hundred feet) effect the signal. Could it be done another, simpler, way?

I can think of nowhere else that such a question might even be considered!
THANX MEFITES!
posted by infomaniac to Technology (23 answers total) 1 user marked this as a favorite
 
$35 for 8 digital inputs and 8 digital outputs? (I haven't used it so this is not a recommendation.)
posted by Chocolate Pickle at 11:06 PM on April 17, 2010 [2 favorites]


Once you've got the hardware sorted out, the software is very easy. Just bind a function to the MouseDown event in your programming language of choice.
posted by randomstriker at 11:09 PM on April 17, 2010


You definitely need additional hardware. Unlike older standards (serial and parallel), the USB standard requires a USB-client controller at the device end.

You could try the mouse approach and see if it works. It'd be really trivial to write the software--like, I could whip up a timer stopped by the mouse in about 20 minutes. But, you may be right that the "click" wouldn't be long enough to register. You could, perhaps, get around this by bypassing the debounce circuitry. But, it could be built into the controller, rendering this approach impossible. The wire length should be fine if it's at least a few volts running through the switch.

For maximum reliability, though, I'd just buy the doohickey that Chocolate Pickle links. The programming will be slightly more complex, given that the event probably won't present as readily as the mouse. But it should also be pretty straight forward.
posted by Netzapper at 12:01 AM on April 18, 2010


Too bad parallel ports are extinct, since that's the sort of thing they're great for.

The mouse idea is pretty cool; or maybe you could also rig something up to a mic or line-in?

From what I understand, if you have to (which you very well may not,) the 'classic' robust way to handle taking a short-duration signal and making it a longer duration one is a 555 (or 565) monostable circuit. As to whether you need that, I guess you'd have to try it.
posted by blenderfish at 12:55 AM on April 18, 2010


The mouse idea seems pretty clever, it's worth a shot IMHO.

But otherwise I think your best bet is to use some programmable doohickey that can talk to a USB port, because as Netzapper and blenderfish say, the USB port really doesn't want to talk to anything that isn't an honest-to-god USB device. There are a bazillion available. I'd suggest checking out the USB Bit Whacker and the Arduino, both of which are aimed at people who aren't afraid of a soldering iron but whose primary goal this week isn't to mess with electronics. The nice thing about something like the Arduino is you can also move some of the smarts onto the device, so that you don't have to rely on the laptop making accurate .01-second timing measurements.
posted by hattifattener at 2:21 AM on April 18, 2010


The mouse or any HID device is your best bet. Mice and keyboards are the cheapest HID devices. Game controllers/joysticks are next cheapest.
posted by plinth at 3:38 AM on April 18, 2010


I do a lot of arcade control building for MAME, and the easiest and cheapest way to hack inputs is with USB gamepads. You can buy new ones on Amazon for <$10 and you get at least 10 inputs.
posted by wongcorgi at 7:28 AM on April 18, 2010


Yeah, I'd use something like an Arduino for this. You could even have all the timing logic on the Arduino and just have it spit out the times over the serial port when it's done (Arduinos have a USB -> Serial interface built in). For timing inputs you'd just have to wire up a few switches, probably with a debouncer.
posted by neckro23 at 7:52 AM on April 18, 2010 [1 favorite]


Several hundred feet? Even if you built a USB device you would be out of the USB spec. You only get around 16-20 feet with USB. You'll need something like a CAT5 USB extender.
posted by damn dirty ape at 10:38 AM on April 18, 2010


You can't put a USB device hundreds of feet away from the computer, but you can run a wire that long from the far side of your USB device. It might add a microsecond or two to your measured result, but you won't be measuring that accurately anyway.
posted by Chocolate Pickle at 11:34 AM on April 18, 2010


It might add a microsecond or two to your measured result, but you won't be measuring that accurately anyway.

Yeah.. the latency isn't so much of a concern as that the inherent inductance and capacitance of the length of wire might make the signal hard to detect if it is of too short a duration. (In which case you could put a 555 monostable [or other circuit] on the far end, near the device.)
posted by blenderfish at 4:52 PM on April 18, 2010


Response by poster: Wow, thanks guys.
I like the Bit Whacker solution, but think the wiring is a bit daunting for a newbie. As I hope to make this program a free download, with simple "make" instructions for the input interface, I'd like to make the wiring as simple as possible. There isn't any way in the USB protocols to just look for a voltage spike in pins 2 or 3 (...5 volts from pin 1, into pin2 to start the timer, into 3 to stop)? The Arduino circuits look simple and and may be more reliable since the input would be standardized, but are they recognizable outside that development language? If so, they may be the ticket. If not, I think I'm gonna try cracking open a USB mouse and try adding a 555 circuit if the "clicks" are missed. Can an external mouse be distinguished the trackpad clicks? Can a game controller? ~Thanks.
posted by infomaniac at 6:11 PM on April 18, 2010


There isn't any way in the USB protocols to just look for a voltage spike in pins 2 or 3 (...5 volts from pin 1, into pin2 to start the timer, into 3 to stop)?

Not even kind of a little bit.

USB is actually packet switched. The client device makes up a packet of information that says "I am device X, sub-device Y, sending a block of data pursuant to protocol ID Z, its length is A, its checksum is B, here is the block of data." From the host to the device, there is a similar packet that's made up and dispatched.

USB bears more in common with ethernet and IP than it does with the old-fashioned the RS-232 serial protocol.
posted by Netzapper at 6:49 PM on April 18, 2010


I think that the problem with using a mouse for this is that while you're doing so you can't use any other kind of pointing device on that same computer -- and that could be a major headache.

Also, I think you'll find that hacking a mouse isn't going to be as easy as you think.
posted by Chocolate Pickle at 7:08 PM on April 18, 2010


There isn't any way in the USB protocols to just look for a voltage spike in pins 2 or 3 (...5 volts from pin 1, into pin2 to start the timer, into 3 to stop)?

Trying to screw around like that is a really good way to fry the mobo in your computer. And Netzapper is right: there is absolutely no way to detect it with software. USB isn't that kind of interface.
posted by Chocolate Pickle at 7:11 PM on April 18, 2010


Actually, upon further thought, even RS-232 doesn't work on a pin-voltage basis. It requires synchronization and handshake.

Now, if you can find a laptop with a parallel port on it, then you're in business. There are all sorts of bit-banging circuits for a parallel port that consist of basically a resistor and some wire.

All things considered, I'd go with either the USB game pad suggested above, or one of the purpose-built USB bit-bangers.
posted by Netzapper at 7:26 PM on April 18, 2010


Response by poster: *Also, I think you'll find that hacking a mouse isn't going to be as easy as you think.
*you can't use any other kind of pointing device on that same computer -- and that could be a major headache.

Would cracking open a game controller or a keyboard work better? --Same plan, buttons on the game controller, little used keys on a keyboard. I'd love to find a RadioShack part or common Ebay detritus that I could some how "plug into." I'd like this to be makable by someone with minimal skills.
posted by infomaniac at 7:34 PM on April 18, 2010


The Arduino uses the FTDI chipset to do the serial->USB conversion. With the FTDI drivers installed, it just looks like a regular (old-style, obsolete) serial port in Windows (or OSX).

"Several hundred feet" might be a problem. IANAEE, only a dumb hobbyist, but it probably depends on the total resistance of your wire (there + back) to the switch. Or, possibly, it'll just work fine. I'd just give it a try and see if it works. Be sure to use a pullup resistor on your input pins.
posted by neckro23 at 8:46 PM on April 18, 2010


even RS-232 doesn't work on a pin-voltage basis

Some of it does: DTR or DSR depending on the gender. And some UARTs could be set up to generate an interrupt on changes in those lines. But modern computers don't have RS-232 ports any more, so the question is moot.

Would cracking open a game controller or a keyboard work better?

A keyboard would have the same problem: while it was plugged in, the real keyboard would be disabled.

I really think you're better off using a purpose-built digital I/O device. I really think you should abandon this idea of trying to hack a consumer device. Believe me, in the long run it'll be cheaper and easier.
posted by Chocolate Pickle at 9:21 PM on April 18, 2010


Have you looked at Phidgets yet? They're self-contained, limited function, USB compatible widgets that are very easy and easy to program for. They're not super cheap, but if all you want to do is a single function like a button then you're set, no reason to bother hacking a mouse or programing a microcontroller.

It should probably be noted that this isn't the most accurate way to time stuff and that the analog to digital conversion, USB protocol navigation, and a multitasking operating system means that the timing can easily be off by hundredths of a second.
posted by Ookseer at 10:06 PM on April 18, 2010


What about using the mic input of your sound card? Put one strip on the left channel, one on the right channel.

Then again, that might just act as a huge antenna.
posted by caaaaaam at 4:15 AM on April 19, 2010


Response by poster: I really think you're better off using a purpose-built digital I/O device. I really think you should abandon this idea of trying to hack a consumer device. Believe me, in the long run it'll be cheaper and easier.

Yeah, you may be right. Still looking for something an amature could make though....

THANKS FOR THE HELP!
posted by infomaniac at 6:10 PM on April 19, 2010


Another non-consumer device option are these cheap dev tools from Silicon Labs. You can get one of those toolstick base adapters ($18) to program, one USB daughter card ($10) and done. Once programmed, you wouldn't need the base adapter anymore, and the daughter card can be powered by USB, so no external power is needed either. The free software tools come with USB examples that any embedded programmer should be able to hack a little to suit your needs.
posted by thewildgreen at 1:26 PM on April 21, 2010


« Older How to travel with a baby? (To France, without a...   |   Seeking motorsports apparel distributors Newer »
This thread is closed to new comments.