I made an electromagnet by wrapping some wire around a nail. It attracted other nails and metal objects when powered with a AA battery. I soldered the wires through a transistor to the 5V pin on the Arduino Nano. I have 2 wires soldered to the GND pin. 1 is for an LED. There is also a manual switch between the transistor and the magnet. When I plug the Arduino in to my laptop, it doesn't get detected until I flip the switch off. As soon as I put it back on, the computer disconnects, even though power still flows to the LED.
To confirm, on a generic transistor, the left of the round side is the input and the middle is the output, right?
I have seen a "Flyback Diode" being mentioned and also read that it was for working with high voltages/currents. I don't think that's my issue, but at this point, I really don't know anything. So what's my issue?
Your electromagnet has too low a resistance, you are shorting out your 5V supply (and perhaps
overloading the transistor)
To confirm, on a generic transistor, the left of the round side is the input and the middle is the output, right?
There is no generic pinout for BJTs, you have to check the datasheet for the particular device. BJTs
have a base, emitter and collector - use these names and people will understand you.
Yeah... Too much current (too-low resistance)… This is kind-of a difficult problem...
The "safest" thing would probably be a battery* to drive electromagnet and a relay** to switch it. Regular batteries (maybe a 6V lantern battery) and relays can be "abused" to some extent by drawing too much current, but of course regular batteries are not rechargeable. (A LiPo battery might overheat.)
In case you don't know [u]Ohm's Law[/u] (Current = Voltage/Resistance). i.e. "Resistance" is the resistance to current flow.
Ohm's Law is a law of nature.*** It's always true. If the power supply (or battery) can't supply the calculated current, the voltage will drop and sometimes "bad things" can happen! A fuse can blow, or the power supply might die, etc. If you were to connect a car battery (which can supply a few-hundred amps) the wire in (or the wires connected to) the electromagnet would burn up!
The resistance of your electromagnet is probably too low to measure with an everyday multimeter, but [u]Here is a chart[/u] showing Ohms per 1000 feet and Ohms per 1000 meters. Since the resistance of wire is very low, 99% of the time we "assume wire resistance is zero for engineering/design purposes."
The current would be limited by the batteries internal resistance, and the output voltage will drop. But, it will happily supply current at a reduced voltage.
** You can't drive a relay directly with the Arduino, but you could use your transistor or you can get relay boards with a built-in driver circuit.
HelpLincoln:
I have seen a "Flyback Diode" being mentioned and also read that it was for working with high voltages/currents. I don't think that's my issue,
If it is not your issue now it will be your issue when you clear up the others. Without the diode you will stand a good chance of resetting your Arduino each time the magnet is switched off or frying your Arduino completely.
Strictly speaking Ohm's law is not always true, its a property of certain conductors and intrisic
semiconductors.
Ohm's law states that the resistance of such a material is independent of current, ie that
voltage across a resistor is in linear proportion to current through it. (Or put another way,
R is a constant at constant temperature)
This clearly doesn't hold for a diode, a battery, a transistor, an insulator, a vacuum, etc etc,
its a property of certain materials that exhibit resistance like metals, carbon, electrolytes and
uniformly doped semiconductors.
People often think that the equation V = IR is Ohm's law, and it isn't, its just the definition of
resistance. This is a very common misconception, but its worth remembering when you encounter
materials that dont obey Ohm's law, like voltage-dependent resistors.
People often loosely refer to the equation V=IR as Ohm's law, and we usually know what they
mean, but don't be confused into thinking its a law of nature.
Compare with distance = velocity x time. That's the definition of velocity, so its always true,
but its not a law of nature because you can't independently test it with an experiment,
its a logically tautology. Laws of nature have to be discovered by interactiing with nature!