Electronic – the simplest and cheapest way to interface with USB

microcontrollerusb

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?

Best Answer

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');
  }
}