Introduction: How to Control an Electromagnet With an Arduino

Picture of 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)

Picture of 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)

Picture of 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.

Comments

ehill14 (author)2015-01-28

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.

siddhu609 (author)ehill142015-11-17

what kind of arduino did u use ?

Kira_gelo (author)2015-08-19

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 :)

ehill14 (author)Kira_gelo2015-08-20

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.

beyondal (author)2015-02-25

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:))

ehill14 (author)beyondal2015-02-25

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.

beyondal (author)2015-01-26

Hi,

The chip your using can this be another chip = another type like H-bridge?
Or does this chip only work for electromagnets?

About This Instructable

44,136views

48favorites

License:

Bio: Pudding is my co-pilot
More by ehill14:How to Make Children’s Vacuum Cleaner Ball CannonHow to make a button more sensitive (for robot sensors)How to Baby-Proof a Fish Tank
Add instructable to: