Controlling an Electromagnet

In this project, you'll learn how to connect and control an electromagnet.

An electromagnet is a type of magnet in which the magnetic field is produced by an electric current. The magnetic field disappears when the current is turned off. The electromagnet is simply a large coil of wire wrapped around a piece of iron.

Parts needed: Arduino      LED      Transistor      Diode      22K ohm resistor Breadboard     

The symbol for the electromagnet is shown on the right. There are two connection points. To turn on an electromagnet, we simply connect the two ends of the electromagnet wires to a power source, one end to 5V and the other end to GND.

To control when to turn on or off an electromagnet, we need to connect one end of the wire to a digital I/O pin on the Arduino board just like with a LED. However, the electromagnet requires much more power (current) than a LED and the Arduino's digital output pin cannot supply this much current, so we cannot directly connect the wire to the digital output pin, therefore, a transistor is needed to boost up the power.

      Electromagnet connections

You may want to first read about the diode

and the transistor before you continue.

Diode Diode symbol

1 Making the connections
  • Follow the schematic diagram on the right to connect up the electromagnet.
  • Be careful with the orientation of the diode and the transistor.
  • Connect a diode between the two ends of the electromagnet, with the negative side of the diode also connected to 5V.
  • Connect the C (collector) leg of the transistor to the positive side of the diode and also the electromagnet.
  • Connect the E (emitter) leg of the transistor to GND.
  • Connect the B (base) leg of the transistor through a 22K Ω resistor, and then to pin 2 on the Arduino.
Electromagnet connections
2 Code.
  • Send a HIGH signal to pin 2 to turn on the electromagnet.
        digitalWrite(2, HIGH);
  • Send a LOW signal to pin 2 to turn off the electromagnet.
        digitalWrite(2, LOW);