Electromagnet: how to integrate this element with your Arduino board


There are some electronic projects or to use with your Arduino, in which you will need to work with controlled magnetism. I mean, in a normal permanent magnet, there will always be a force of attraction, but with a electromagnet you can control that magnetic field to generate it just when you need it. That way, you can attract ferromagnetic materials for a multitude of applications.

For example, imagine that you want to open or close a small trap door automatically when something happens, or move some metal object, etc. In that case, the best thing you can use is an electromagnet, avoiding having to create other complete mechanisms that do the same function.

What’s an electromagnet?

módulo Electroimán

An electro-magnet is an electronic device that allows you to generate a magnetic field at will. That is, a device that becomes a magnet only when you need it, and not always like permanent magnets. That way, you can attract ferromagnetic objects just at the right time when you want.

Electromagnets are widely used in the industry. For example, you have surely seen on TV those machines that are in some points where metal is recycled and that have an electromagnet that the operator activates from the cabin to take the chassis of a car from the scrap yard, or to attract other metal pieces. Then, when the crane that holds this electromagnet has positioned itself where it wants to leave these metal objects, it simply deactivates the magnetic field of the electromagnet and everything will fall.

The way to activate it is by supplying this element with a continuous current. While this current is acting on the electromagnet, the magnetic field is maintained and the metal remains attached to it. When this current ceases, it will disappear and the metal elements will come off. So you can control it quickly.

Well, you can also use this for your own benefit and in a very cheap way. You can buy the electromagnet ready-made or create it yourself, as it is not complicated at all, unlike other electronic components.
electric motors (the rotor turns thanks to magnetic fields that are generated), generators, speakers, relays, magnetic locks, and a long etc.

Even if you are more or less clear about how to operate an electromagnet, you should understand well how it works to ‘attract’ or ‘repel’ objects (if you change polarization). With this type of device, you will not need to use permanent magnets to attract ferromagnetic materials such as iron, cobalt, nickel, and other alloys.

How does it work?

For the electromagnet to work, we must go back to the studies of the Danish Hans Christian Orsted of 1820. He discovered that electrical currents can generate magnetic fields. Later, Britain’s William Sturgeron would make the first electromagnet using that discovery, and that goes back to 1824. And it wasn’t until 1930 that Joshep Henry would perfect it to create the electromagnet we know today.

Keep in mind the type of metal or alloy you are going to use for your project, since not everyone is attracted to these magnets.

Physically it will be constituted by a rolled coil and within it a ferromagnetic core, such as mild iron, steel and other alloys. The coils are usually made of copper or aluminium, and have an insulating cover like a varnish to prevent them from making contact, since they will be placed very close to each other or directly in contact to compact them even more. Something similar to what happens with transformer coils, which also have this varnish.

The function of the coils is to generate this magnetic field, and the core will increase this effect and concentrate it to reduce scattering losses. Inside the core material, its domains will be aligned or oriented in one direction thanks to the intensity generated by the coil, that is, it resembles what happens inside permanent magnets, which also have those domains aligned in a specific direction according to their pole.

You can control the force of attraction by increasing the current you are passing through the electromagnet. Having said that, I have to say that this is not the only factor that affects the attraction force of the electromagnet, to increase its power you can increase one or all of the following factors:

  • Number of solenoid coils.
  • Core material.
  • Current intensity.

When the current stops, the domains tend to re-orient themselves in a random way, and therefore lose their magnetism. So, when you remove the applied current, the electromagnet stops attracting. However, a residual magnetic field can remain, which is called remanent magnetism. If you want to remove it, you can apply a cohesive field in the opposite direction or raise the temperature of the material above the Curie temperature.

Get an electromagnet


As I said, you can create it yourself if you like DIY or if you are looking for a type of electromagnet with features that are not satisfied with those you can buy. Another option, if you are more lazy, is that you buy the electromagnet in any store like in Amazon.

Keep in mind something, if you’re going to buy the electromagnet. And that is that you’re going to find different prices and several types that have different characteristics. Among them, what varies most is the amount of weight they can support or attract. For example, the 25N of 2.5Kg, the 50N of 5Kg, the 100N of 10Kg, the 800N of 80Kg, 1000N of 100Kg, etc. There are bigger ones for industrial applications, but it is not frequent for domestic applications… Don’t think that the price is so high between one and the other, as they range from 3 to 20 euros.

If you decide to create it yourself, you can have a cheap electromagnet simply by winding wire to generate a coil and inside you must introduce a ferrous core. For example, the simplest and easiest electromagnet that children usually make to learn in laboratories is to use a battery that they connect to a coiled wire (it must be covered with insulating varnish or plastic insulation so that they don’t make contact in the coils) and inside which they introduce a tip as a core. When the two ends are connected to each of the poles of the battery, a magnetic field will be generated in the coil which attracts metals .

Of course, you can perfect the electromagnet with a larger coil or using a different metal core if you want to achieve higher power dimensions and magnetic fields.

Integration with Arduino

esquema electroiman con Arduino

The integration with Arduino is not complicated at all. Whether it’s a purchased electromagnet or one you create yourself, you can directly use the Arduino outputs and the power supply to activate or deactivate the electromagnet at will using the code in your sketch. But if you want to do it in a better way, you should use some element to control the electromagnet in a more appropriate way, especially if it is a more powerful electromagnet. In this case, you can use for example a transistor MOSFET as a control element, or an NPN TIP120 (the one I used to test), and even a relay. So, you can use one of the digital pins to control the transistor and this one to the electromagnet…

You must put a fly back or antiparallel diode like the one in the picture, between the two connectors of the electromagnet. You must also include a 2K ohms resistance as you see in the diagram. The rest of the connections are very simple as you can see. Of course, in this case, the blue and red wires correspond to the external power supply that will be applied to the solenoid.

Remember that there are electromagnets of rated voltage of 6V, 12V, 24V, etc., so you must know well the voltage to be applied to the solenoid in order not to spoil it. You can see the details in the Amazon description or looking for the datasheet of the component you are using. Remember to also respect its pinout, which are two pins, one for ground or GND and the other Vin to apply the control current.

The one I have used to test this example of scheme that I have created in Fritzing is of 6V, so in the lines that I have put towards the right in the scheme it will be applied +0/6V in the red and -0/6V in the blue. Remember that depending on the intensity you will get more or less attraction force.
the code, you can do something simple like the following (remember that you can modify the code so that it activates and deactivates intermittently after a while, like this one, that it does it according to another sensor that you have in your circuit, or that some event occurs…):

const int pin = 3;
//Remember to use the correct pin that you used in the electrical scheme of your project
void setup() {
  pinMode(pin, OUTPUT); //define pin as output
void loop(){
  digitalWrite(pin, HIGH); // set the pin to HIGH to activate the electromagnet
  delay(10000); // wait a second
  digitalWrite(pin, LOW); // set the pin to LOW to deactivate the electromagnet
  delay(10000); // wait a second

Leave a Comment