ESP8266 Magnetic Sensor Project

This project demonstrates how to use the ESP8266 microcontroller with a Hall effect sensor to detect the presence of a magnetic field. The Hall effect sensor outputs a digital signal based on whether a magnet is present near it.

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

  • ESP8266 (NodeMCU)
  • Hall Effect Sensor Module
  • Magnet (for testing)
  • Jumper Wires

Wiring to the ESP8266 (NodeMCU)

Connect S to D5, the supply pin to the 3.3 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.

A bare A3144 specifies 4.5 V minimum, so at 3.3 V it may be unreliable. Either choose a 3.3 V-rated Hall sensor such as the AH3503, or power the A3144 from 5 V and drop its output with a divider.

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 pinESP8266 (NodeMCU) pinFunction
S / OUTD5LOW when a magnet is present
Middle / +3V3Supply (4.5–24 V on a bare A3144)
− / GNDGNDCommon ground

Example Code

Detecting magnet presence and counting passes, as in a speed sensor. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.

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

bool magnetPresent = false;
unsigned long passCount = 0;

void setup() {
  Serial.begin(115200);
  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 ESP8266 (NodeMCU)

The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards 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 ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.

There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.

D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.

Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.

ESP8266 (NodeMCU) characteristicValueWhy it matters here
Logic voltage3.3 VSensor outputs above this level need a divider or level shifter
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsa single analog channel, A0Determines how many analog sensors can share the board
PWM outputsany GPIO via software PWMNeeded for brightness, speed and tone control
I²C pinsD2 (SDA, GPIO4) and D1 (SCL, GPIO5) by defaultFixed by hardware — wiring copied from another board may not match
Interrupt pinsany GPIO except D0 (GPIO16)Required for counting fast or asynchronous events
Serialone hardware UART plus a transmit-only second portMonitor runs at 115200 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.
  • The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
  • Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.

Taking It Further on the ESP8266 (NodeMCU)

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 ESP8266 (NodeMCU) specifically:

The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.

Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.

For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.

Notes and Practical Limits

For speed measurement, attach the sensor to an interrupt-capable pin — on the ESP8266 (NodeMCU) that means any GPIO except D0 (GPIO16) — 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.