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\$
2
  • 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
    Commented Apr 29, 2011 at 23:23
  • \$\begingroup\$ Very cool project! \$\endgroup\$ Commented Apr 29, 2011 at 23:24

5 Answers 5

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\$
5
  • \$\begingroup\$ can you supply more detail on using pyserial to interface with Arduino? Code sample or links appreciated. \$\endgroup\$ Commented Apr 30, 2011 at 5:22
  • 1
    \$\begingroup\$ Sure thing. Check out the example. \$\endgroup\$ Commented Apr 30, 2011 at 16:42
  • \$\begingroup\$ I really appreciate the example. You are legend!! \$\endgroup\$ Commented May 1, 2011 at 9:39
  • \$\begingroup\$ Not going to be cheapest though. \$\endgroup\$ Commented Feb 3, 2016 at 7:29
  • \$\begingroup\$ that is not "interface with usb" but ftdi serial only. \$\endgroup\$
    – nonchip
    Commented Aug 31, 2019 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\$
5
  • \$\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
    Commented Aug 29, 2015 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
    Commented Aug 29, 2015 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\$ Commented Jan 6, 2016 at 4:36
  • 1
    \$\begingroup\$ FTDI is an English company (well, Scottish actually) based in Glasgow. \$\endgroup\$ Commented Feb 3, 2016 at 7:29
  • \$\begingroup\$ @RBerteig link work? \$\endgroup\$
    – Fast Snail
    Commented Feb 21, 2016 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\$
1
  • \$\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
    Commented Jan 22, 2020 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 and acknowledge you have read our privacy policy.

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