Controlling simple relay switch via USB

60,435

Solution 1

Unfortunately, in most cases you cannot control the power supply to the USB port. The power supply is usually hardwired through, and not switchable in software. You can send a reset to a USB device, but that won't work in your case.

There are a number of projects on instructables that do similar to what you describe, but unfortunately they seem to either be quite complicated or require expensive parts.

EDIT: There is actually a product currently in the news which would do want you want, but it doesn't appear to be shipping yet: http://www.pwrusb.com/

EDIT (again): Apparently you can do this with some usb hubs. This post sugggests the Linksys USB2HUB4 is one that works.

EDIT (and again): Apparently there are a number of similar questions, but there don't seem to be any more useful answers:

Solution 2

Most of the USB to Serial or USB to RS232 $10 converters support hardware handshaking. Use one of those as a single channel digital io.

Connect your transistor that will drive the relay to DTR on the converter board and command DTR ON/OFF with the converters driver.

A 2N7002 is a good transistor to use (FET actually) since it will work from 3V and doesnt need any resistors anywhere.

Solution 3

You need a USB-GPIO microcontroller:

Adafruit FT232H (about $15) enter image description here

Arduino Nano ATmega328 (about $7) 2

See this answer

Solution 4

Here is a complete solution.

It uses an Arduino board, with a USB connection, (and Uno for example) to control the relay and combines this with pfod (Protocol for Operation Discovery) www.pfod.com.au which will let you control the board/relay from either the Arduino IDE SerialMonitor, or a terminal window (TeraTerm) or a Java program. The message protocol encloses commands in { }, think simplified html for micros, and provides numerous screens, menus, sliders, text and numeric inputs, etc. A detailed protocol spec is available

See Garage Door Remote for a detailed example, with full Arduino code and an example of controlling the relay from TeraTerm as well as the Arduino IDE SerialMonitor.

There is also an Android app, pfodApp, which will do general purpose control via bluetooth, or wifi/internet with 128 bit security. The pfodApp does all the Android stuff, you only need to code some simple strings in your Arduino code to get any menu system you want. See www.pfod.com.au for numerous examples.

Share:
60,435
Matt
Author by

Matt

SOreadytohelp

Updated on July 09, 2022

Comments

  • Matt
    Matt almost 2 years

    I'm looking to control a mains powered light from a simple relay switch connected via USB to the computer.

    The relay switch isn't even a USB device, it's just a simple switch that requires the USB voltage to turn it on. When the voltage drops below a threshold, the switch will turn the light off.

    My problem is that I can't control the power output of a USB port. I'm happy to do it using any language on Windows or Linux (but preferably Java because I'm used to it).