Arduino Uno Magnetic Sensor (Hall Effect)

This project demonstrates how to use a Hall effect sensor with an Arduino Uno to detect the presence of a magnetic field. The sensor outputs a signal when exposed to a magnetic field, which is then read by the Arduino Uno and displayed on the Serial Monitor.

How It Works

A magnetic sensor module such as the KY-003 is built around a Hall-effect switch, typically an A3144 or similar. The Hall effect itself is straightforward: pass a current through a thin semiconductor and bring a magnetic field perpendicular to it, and a small voltage appears across the other axis, proportional to field strength.

The A3144 is a digital part. It contains the Hall element, an amplifier and a Schmitt trigger, so its output is simply HIGH or LOW with built-in hysteresis — the field must weaken appreciably before it releases, which stops the output chattering as a magnet drifts near the threshold.

Crucially, these switches are polarity sensitive. A unipolar device responds only to the south pole facing its marked face. If a magnet produces nothing, flipping it over usually fixes the problem — that is not a fault.

Because detection is magnetic rather than optical or mechanical, it works through plastic, wood, glass and water, and there are no contacts to wear out.

Components Needed

  • Arduino Uno
  • Hall Effect Sensor Module
  • Jumper Wires
  • Arduino Uno

Wiring to the Arduino Uno

Connect S to D2, the supply pin to the 5 V rail and GND to ground. The output is open-collector on many carriers, which means it can pull LOW but cannot drive HIGH — declare the pin as INPUT_PULLUP so it idles high.

The A3144 is specified from 4.5 V upward, so a 5 V supply suits it well.

Mount the sensor so the magnet passes within 5–10 mm of its marked face. Detection range falls off steeply with distance — roughly with the cube of it — so a few extra millimetres can mean no detection at all.

Module pinArduino Uno pinFunction
S / OUTD2LOW when a magnet is present
Middle / +5VSupply (4.5–24 V on a bare A3144)
− / GNDGNDCommon ground

Build and Upload

Open the Arduino IDE and create a new sketch.

Copy and paste the provided Arduino code into the sketch.

Upload the code to the Arduino Uno.

Open the serial monitor with a baud rate of 9600.

Observe the messages indicating the presence or absence of a magnetic field.

Example Code

Detecting magnet presence and counting passes, as in a speed sensor. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.

Detecting magnet presence and counting passes, as in a speed sensor
const int HALL_PIN = 2;

bool magnetPresent = false;
unsigned long passCount = 0;

void setup() {
  Serial.begin(9600);
  pinMode(HALL_PIN, INPUT_PULLUP);   // open-collector output needs a pull-up
}

void loop() {
  bool present = (digitalRead(HALL_PIN) == LOW);   // LOW = magnet detected

  if (present && !magnetPresent) {   // rising edge of a pass
    passCount++;
    Serial.print("Magnet detected — pass #");
    Serial.println(passCount);
  }
  magnetPresent = present;
  delay(10);
}

Applications

A hall effect magnetic sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Door and window contacts in alarm systems, with the magnet on the moving leaf
  • Wheel and shaft speed measurement, counting magnet passes per revolution
  • Position limits on linear actuators and sliding mechanisms
  • Lid and enclosure interlocks that need no exposed contacts
  • Water and gas meter pulse pickup, where a magnet is embedded in the dial

Working with the Arduino Uno

The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.

With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.

The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.

Arduino Uno characteristicValueWhy it matters here
Logic voltage5 VMatches most hobby modules directly
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsA0–A5 (six channels)Determines how many analog sensors can share the board
PWM outputsD3, D5, D6, D9, D10 and D11Needed for brightness, speed and tone control
I²C pinsA4 (SDA) and A5 (SCL)Fixed by hardware — wiring copied from another board may not match
Interrupt pinsD2 and D3 onlyRequired for counting fast or asynchronous events
Seriala single hardware UART shared with USBMonitor runs at 9600 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • No detection no matter how close the magnet — turn the magnet over; unipolar sensors respond to one pole only.
  • The output floats or reads randomly — the open-collector output needs INPUT_PULLUP or an external 10 kΩ resistor.
  • Detection is unreliable at the edge of range — move the magnet closer or fit a stronger neodymium magnet.
  • The count increments several times per pass — unusual with a Schmitt-trigger part, but a slow-moving magnet at the exact threshold can do it; reduce the gap.
  • Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
  • An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).

Taking It Further on the Arduino Uno

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:

The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.

Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.

Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.

Notes and Practical Limits

For speed measurement, attach the sensor to an interrupt-capable pin — on the Arduino Uno that means D2 and D3 only — and count edges in an ISR rather than polling. Polling misses passes once the shaft spins quickly.

If you need to know how strong the field is rather than merely whether a magnet is present, use a linear Hall sensor such as the AH49E instead. Digital switches deliberately throw that information away.