17
\$\begingroup\$

If I wanted to make a simple device that communicates with my computer, say maybe a switch that could mute my computer when turned on and off and plug it in via USB, what would be the cheapest and easiest way to accomplish this?

\$\endgroup\$
  • 4
    \$\begingroup\$ look at my usb single chip scope here, it is difficult to do with fewer parts 1 ATTiny45 and a couple of zener, 2 chanels HID \$\endgroup\$ – user4047 Apr 29 '11 at 23:23
  • \$\begingroup\$ Very cool project! \$\endgroup\$ – Code Painters Apr 29 '11 at 23:24
10
\$\begingroup\$

Easiest? Grab an Arduino and write a couple of lines of Python. Arduino's are incredibly easy to program, don't require any additional hardware to work with, and are quite popular. Python has a very straightforward serial library and is a breeze to write in.

Example Code

Python: Run this script as a service. I'm using Ubuntu, so this script will pop up a notification telling you when a button has been pressed on the Arduino.

#! /usr/bin/python

import serial
import pynotify

ser = serial.Serial('/dev/ttyUSB0', 9600)
while True:
  x = ser.read()
  if x == 'b':
    # Show notification
    n = pynotify.Notification("Arduino", "The button was pressed.")
    n.show()

Arduino:

void setup(){
  // Assuming button is active low and on pin 4
  pinMode(4, INPUT);
  Serial.begin(9600);
}

void loop(){
  if(digitalRead(4) == LOW){
    Serial.print('b');
  }
}

 

\$\endgroup\$
  • \$\begingroup\$ can you supply more detail on using pyserial to interface with Arduino? Code sample or links appreciated. \$\endgroup\$ – LeanerRocky Apr 30 '11 at 5:22
  • 1
    \$\begingroup\$ Sure thing. Check out the example. \$\endgroup\$ – Nick Pascucci Apr 30 '11 at 16:42
  • \$\begingroup\$ I really appreciate the example. You are legend!! \$\endgroup\$ – LeanerRocky May 1 '11 at 9:39
  • \$\begingroup\$ Not going to be cheapest though. \$\endgroup\$ – quickly_now Feb 3 '16 at 7:29
  • \$\begingroup\$ that is not "interface with usb" but ftdi serial only. \$\endgroup\$ – nonchip Aug 31 '19 at 14:13
8
\$\begingroup\$

Build a USB Human Interface Device Class device. That is the same class used by keyboards, mice, joysticks, game pads, and the like. Your PC already has full device driver support for HID devices, so software access is easy. Lots of pointers and even sample code can be found at Jan Axelson's HID page. Browse around her site for lots of good information related to USB device development.

Many of the small microprocessor families can do HIDs. I've even seen it done at USB Low Speed with an ATtiny 8-pin AVR entirely in software.

Pick your preferred chip, then search its "usual resources" for HID samples.

Another approach is to make a serial communications device. Several single chip solution from FTDI exist. The FT232R is a popular choice. Drivers are required, but Windows Certified drivers are known to the Found New Hardware wizard so installation isn't hard. Once installed, you have a device that looks like a COM port. Alternate drivers are available that will let you access its GPIO pins and use it in more advanced modes.

Update, 2015: This had a link to http://www.lvr.com/hidpage.htm, which seems to have link rotted and the new location discovered thanks to El Marce. Jan's Lakeview Research is still a great source for information about practical use of USB.

\$\endgroup\$
  • \$\begingroup\$ The Jan Axelson's link is broken. I found this (janaxelson.com/hidpage.htm) by googling: "jan axelson hid page". Would this be so kind to verify is the right one? Thanks! \$\endgroup\$ – El Marce Aug 29 '15 at 7:47
  • \$\begingroup\$ Thanks. That is her site now. I'm guessing that Lakeview Research either lost or released their old domain name and it got snatched up by someone new. \$\endgroup\$ – RBerteig Aug 29 '15 at 23:00
  • 1
    \$\begingroup\$ If you choose to go the FTDI route, make sure you get a cable with a genuine FTDI part. FTDI (a Chinese company) got sick and tired of having their chips counterfeited, so they taught their drivers to recognize their own chips. This has caused a LOT of trouble for people who bought two-way radios with programming cables that were built with the counterfeit chips, because the manufacturer of the radio bought the counterfeit chips (hopefully by mistake!). \$\endgroup\$ – John R. Strohm Jan 6 '16 at 4:36
  • 1
    \$\begingroup\$ FTDI is an English company (well, Scottish actually) based in Glasgow. \$\endgroup\$ – quickly_now Feb 3 '16 at 7:29
  • \$\begingroup\$ @RBerteig link work? \$\endgroup\$ – Fast Snail Feb 21 '16 at 6:19
6
\$\begingroup\$

Here is a very detailed blog post http://msdn.microsoft.com/en-us/devlabs/dd491992 with software to use an under $5 PIC18F4550 to interface via USB as a standard HID device. The software provided can be used to interface to any HID device, so if you want an alternate chip the software will still work.

An added plus is that the blog post shows you how to do bi-directional communication so not only can you interface a switch you can turn an LED on and off.

\$\endgroup\$
3
\$\begingroup\$

Here is a simple project of mine using a PIC18F2455, based on Brad Minch's USB software. PCBs are available from Olimex.

\$\endgroup\$
  • \$\begingroup\$ Leon, I'm researching how to interface a PIC with a PC using libusb (libusb.info). The project you linked here may be the closest thing I've found but the links are broken. Any chance you still have the source for this project and can share it? \$\endgroup\$ – chuck1 Jan 22 at 23:05
2
\$\begingroup\$

If you don't mind Cortex-M3 based microcontroller, take a look at NXP's LPC1343. It's only $6.30 at Digikey (1 piece). There's one very cool feature - firmware for storage and HID devices in chip's ROM!

Compared to a solution with FTDI chip + microcontroller, there's one chip less :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.