Introduction: How to Control an Electromagnet With an Arduino
Electromagnets are fun to play with, but to do something more than just pick up paperclips, you need better control. This is a simple tutorial on how to operate one with the Arduino, but building of of this (and using a few more magnets), you can do a number of projects like rail guns, musical chimes, haptic feedback or communication (like I did here) or something involving magnetic ooze.
Need:
Wire (for the magnet)
Steel or iron core for the magnet
Motor bridge (TA7291P or others)
5v-12v AC adapter
Arduino
Step 1: Arduino and Electromagnets (the Hardware)
There are a number of tutorials on how to make an electromagnet. The simplest way is to get something iron or steel and wrap an insulated wire around it a lot. The strength is dependent on how much current goes through the wire and how many wraps you do. If you get magnet wire like I used, it is covered with a clear insulator. To make the ends of the wire conductive, you must either sand off or burn off (with a lighter) the insulation. If you plan to run the magnet for a long time, I recommend using a thicker or longer steel core to act as a hint sink as the magnets can heat up a lot. An alternative (if your project allows it) is to fire your magnets in pulses. That will give your magnets a chance to cool down some. The image shows the electromagnets I used for my haptic device.
The circuit is very simple. All you need is the same circuit for powering a DC motor. However if you don’t need to reverse the polarity of the magnet, you can hook up two magnets to every motor bridge. I recommend hooking this up to an AC adapter rather than batteries as it will go through your batteries quickly (and may overheat them). Don’t power the magnets with your Arduino or you’ll fry the board. To hook an AC adapter to a breadboard, just clip the connector plug off, strip the wires, and wrap or solder them to some solid core wire. If you don’t have a voltmeter, if one wire of the adapter has dashed lines, that’s most likely the positive.
Step 2: Arduino and Electromagnets (the Software P.1)
The Arduino code to run this is simple--it really only takes the sample blinker program that is in examples section in your Arduino compiler. Plug the wire leading to pin 3 into a ground slot instead and run this:
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(1000);
}
The above program will only control one magnet though. The two motor bridge inputs should be HIGH/LOW or LOW/HIGH to control one of the magnets. LOW/LOW is of course off. Here is a blinker program that alternates between the two magnets.
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delay(500);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
delay(500);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(500);
}
Step 3: Arduino and Electromagnets (the Software P.2)
If you want to use this circuit for a haptic interface, vibrations are easier to feel than constant current. Place a permanent magnet either against the skin by taping it there using flexible medical or sports tape, or attach the magnet to a device as shown in the image. Here the permanent magnets are attached to a silicone membrane (a laptop keyboard protector sheet). This device goes under the arch of the left foot. The Arduino will fire in pulses (10ms had the best response for me) to cause vibration. Code for that might look like:
void activate(int pin) //activates the magnet drivers
{
int c;
for(c = 0; c < 10; c++)
{
digitalWrite(pin, HIGH);
delay(10);
digitalWrite(pin, LOW);
delay(10);
}
}
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
//start with both pins at low
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
void loop() {
activate(2);
delay(500);
activate(3);
delay(500);
}
Have fun and don’t overheat your magnets.

Participated in the
123D Circuits Contest
9 Comments
Question 4 years ago
Hello,
I am putting together a project together for school. Now I am running an Ardiuno Uno in connection with a L298N. Outputs 1 and 2 are connected to a motor that runs via a joystick (working fine) and I want to be able to set up a electromagnet that is activated and deactivated with the button on the joystick. I could use a little help in connections
Question 5 years ago
Sir, can I use H B ridge to change the poles of the magnet
8 years ago on Introduction
Any H-bridge is fine. This chip is designed to run motors (or things with higher power draw). Any chip that does that would work. The pins to connect would most likely be different though.
Reply 8 years ago
what kind of arduino did u use ?
8 years ago on Introduction
Hi sir i wanna ask about the size of the wire you use, number of turns and how many Tesla will be produced with this design? thanks in advance sir :)
Reply 8 years ago on Introduction
How strong of a magnet you need depends on the distance/strength of the permanent magnet. That being said, I don't remember the wire's gauge, but I used about 3.5m of wire for each screw. Experiment and see what works well for you.
8 years ago on Introduction
Hi,
Well I'm trying to reverse the current. So the poles also change on the electromagnet. Could you show me how to do that? ^^ (new instructable:))
Reply 8 years ago on Introduction
It's an AC motor, so you can't do that (same reason why flipping the plug over in the outlet doesn't reverse current). Were it a DC motor, you could.
8 years ago on Introduction
Hi,
The chip your using can this be another chip = another type like H-bridge?
Or does this chip only work for electromagnets?