I need to control an electromagnet via USB with an android phone, to make a small educational project.
I know how to write the software, but how can I control an electromagnet via USB?
Via an USB Relay? Any suggestion?
This may be overkill for what you want, but Microchip do an Accessory development starter kit for Android.
With that you can create anything you like to interface with your Android phone. You can even use Arduino shields with it!
To this you would need to add something to actually drive the electromagnet. You might like to check out the many tutorials online about interfacing motors and solenoids to the Arduino, as the same applies to this board.
Most commonly either a transistor, or transistor pair (known as a Darlington Pair) is used, depending on power requirements. These are also available in an IC form, such as the popular ULN2003 chip with 7 Darlington Pairs in it.
I have created an Arduino shield that contains an 8-channel Darlington IC, the ULN2803, which you can etch yourself using Toner Transfer. The details on it are here.
Shameless self promotion there :)
You would need to use a USB capable microcontroller or something like an FT245 USB to parallel in order to connect and receive commands from the host properly.
Then simply trigger a pin to turn on the base of a suitable transistor/relay and activate the electromagnet.
EDIT - note the FT245 solution will only work if the Android can be used as a host. From a quick google I found this page which says version 3.1 and higher support host mode. YOu would have to check for you particular device on the specifics. There are quite a few threads out there about doing things like connecting a keyboard to androids.
Otherwise a uC (e.g. PIC24/32) could be used as a host.
IOIO is smaller, cheaper and easier to work with than adk's. It will also work with any Android version (as opposed to 2.3.4+) and supports both USB or Bluetooth connection to Android.