Skip to main content

How to use a Electromagnet

What is a Hall Effect Sensor?Electromagnet?

The hallGrove effect sensorElectromagnet is a type of magnetic sensoractuator whichthat becomes magnetized when an electric current passes through it. When powered on, it can beattract usedsmall forferromagnetic detectingobjects such as nails, screws, or paper clips. When the strengthcurrent stops, it immediately loses its magnetism. This makes it useful in applications like magnetic locks, robotic grippers, and direction of asimple magnetic fieldcontrol produced from a permanent magnet or an electromagnet with its output varying in proportion to the strength of the magnetic field being detected.experiments.

image-1706547936079.png

Wiring

  1. leftRed VCC to 5V (Power)
  2. middleBlack GND to GND
  3. rightYellow SIG to PIN2PIN via 10K resistor halleffectcircuit.png2

Getting started

The following code usesdemonstrates digitalRead()how to getcontrol the Grove Electromagnet by toggling it on and off using a integerdigital (1/0)pin.

representing

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

const int hallSensorPinelectromagnetPin = 2;  // Hall Effect sensorElectromagnet connected to digital pin 2

int hallSensorState;          // Variable to store the state of the sensor

void setup() {
  Serial.begin(9600);pinMode(electromagnetPin, // Start serial communication at 9600 baud
  pinMode(hallSensorPin, INPUT)OUTPUT); // Set the Hall Effect sensor pin as anOUTPUT
  INPUTSerial.begin(9600);
}

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

  // ReadTurn the stateelectromagnet ofOFF
  thedigitalWrite(electromagnetPin, sensorLOW);
  Serial.println(hallSensorState)"Electromagnet OFF");
  delay(100)2000);
}