Skip to main content

How to use an Electromagnet

What is an Electromagnet?

The Grove Electromagnet is a type of magnetic actuator that becomes magnetized when an electric current passes through it. When powered on, it can attract small ferromagnetic objects such as nails, screws, or paper clips. When the current stops, it immediately loses its magnetism. This makes it useful in applications like magnetic locks, robotic grippers, and simple magnetic control experiments.

Wiring

  1. Red VCC to 5V (Power)
  2. Black GND to GND
  3. Yellow SIG to PIN 2

Getting started

The following code demonstrates how to control the Grove Electromagnet by toggling it on and off using a digital pin.

When the signal pin is set to HIGH, the electromagnet turns on and attracts metallic objects. When set to LOW, it turns off and releases them.

const int electromagnetPin = 2;  // Electromagnet connected to digital pin 2

void setup() {
  pinMode(electromagnetPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600);
}

void loop() {
  // Turn the electromagnet ON
  digitalWrite(electromagnetPin, HIGH);
  Serial.println("Electromagnet ON");
  delay(2000);

  // Turn the electromagnet OFF
  digitalWrite(electromagnetPin, LOW);
  Serial.println("Electromagnet OFF");
  delay(2000);
}