Using a Hall effect sensor with Arduino

A Hall effect sensor is a solid state magnetic switch and can be used to switch a circuit in the presence of a magnetic field. A low cost Keyes KY-003, unit is used here as there seems to be a number of these Keyes sensor boards at low cost on eBay. This board has a single digital output that goes LOW when a magnetic field is detected. Note, each side of the sensor reacts to a different magnetic pole, so if nothing seems to happen use the other pole of the magnet.


The following sketch turns on the onboard LED when a magnet is brought near to the sensor.


Hall Effect magnetic switch, a magnet is used to turn on the onboard Led

Hall Effect board is from Keyes. KY-003

Pin 1 connect to Arduino Gnd
Pin 2 connect to Arduino 5 volts
Pin 3 marked S connect to Arduino pin 2


# define hallPin 2
# define ledPin 13

void setup() {
pinMode(hallPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);

void loop() {
if (! digitalRead(hallPin)) {
Serial.println("Magnetic field detected");
digitalWrite(ledPin, LOW); // turn on LED
digitalWrite(ledPin, HIGH);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: