How to Control an Electromagnet With an Arduino
by ehill14 in Circuits > Arduino
94596 Views, 62 Favorites, 0 Comments
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
Need:
Wire (for the magnet)
Steel or iron core for the magnet
Motor bridge (TA7291P or others)
5v-12v AC adapter
Arduino
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.
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.
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);
}
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);
}
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.
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.