Hi all!

I’m wondering the easiest way to use the arduino to control a simple electromagnet.

Thanks in advance!

A relay is basically an electromagnet, so search the forum for info on hooking up a relay and use that as a starting point.

The simplest way would probably be to use an I/O line to control a MOSFET or transistor that switches power to your electromagnet (as an I/O line by itself will probably not be able to source the current needed to drive an electromagnet.

  • Ben

Thanks for the replies guys.

I was thinking a transistor would be the easiest way to set it up.

Should I be concerned about current/heat the electromagnet will produce. I was thinking of using a 9v or a 1.5 battery which should provide enough power. I would also use an inline resistor on the digital out of the arduino before the base (1k). I’m new to microelectronics so forgive my probable newbness.

Do I need a transistor to handle up to 1amp of current? more? or will the smaller ones for 500ma etc… work? Should it have a heat sync? I don’t need the magnet to be powered for very long - like a relay, it will just need to be enough for the magnet to pull what I’m attracting to it.

Again, thanks for your help guys.


FYI there was a huge post about this about 6 months ago in the forum. if you search it with Google I think you will find some terrific information.


thanks for the feedback Daniel2,

I’ve been surfing around and found a few posts that seem applicable. The solenoid one in particular. Sure is a lot to learn, I think I’m just starting to wrap my head around this language of electronics. In some ways it is similar to programming, which I’m more familiar with.


I’m doing a project with solenoids right now and the easiest way I found to control them is by using an uln2003a transistor array. Those are gated so you get an on/off output instead of a linear output as from a regular transistor.
I advise you to test the current flowing through your solenoid first though, because the uln2003a supports max 500mA per channel. each circuit has 7 channels, so by using 2-3 channels in parallel you can get a safe 1-1.5 Amp margin.

However, you must beware of current spikes going through solenoids as they usually have high inductance. Some people might suggest to use an opto-isolator in order to protect your micro-controller but I haven’t had any problems so far.

thanks for the helpful reply,
any problem with heat with the arrays in parallel with them somewhat maxed out? or maybe you aren’t leaving them on?

Most of the stuff I’ve seen have the catch diode in parallel with the solenoid to deal with the flyback? from the solenoid. but maybe thats built into the chip. I’ve also seen capacitors in my newb search that even out voltage spikes. These were in particular being used for sensors. Could you use a capacitor to even out your voltage?

indeed an elegant solution for controlling multiple high current loads :slight_smile:


hmm, I dont know about using a capacitor, but a diode should be good I guess. I guess you can always try to check the voltage and the currents at the logical gate outputs before sticking it to the arduino.
I havent had any problems with heat sinking because my solenoid draws only 150mA in stable mode, and doesnt need to be on for very long… however, the IC heated somewhat quickly when I passed 2 Amps of current for a test, but I have never experimented with heat sinks yet :-/.

hello, i`m also experimenting with a 12v solenoid and as Handi said there is a risk of inductive current coming from the solenoid that could damage the atmega but i did not succeed in using the opto-isolator (i tried the pc817) because it did not delivered the necessary current for the solenoid to work. so i used the uln28003a darlington array instead (which is basically like the uln2003a mentioned some posts above) and my atmega is still alive.

here´s the circuit and solenoid images