ESP8266 Linear Magnetic Hall Sensors Project

This project utilizes an ESP8266 microcontroller to measure the magnetic field strength using a Linear Hall Effect Sensor. The sensor value is then displayed on the Serial Monitor for monitoring and analysis.

How It Works

A linear Hall sensor such as the AH49E — found on the KY-024 carrier — differs fundamentally from the digital switch used in the KY-003. Instead of a Schmitt trigger producing HIGH or LOW, it outputs a continuous voltage proportional to magnetic flux density, so it measures the field rather than merely detecting it.

With no field present the output rests at roughly half the supply voltage. A north pole drives it above that midpoint, a south pole below it. This ratiometric, bipolar behaviour means a single sensor reports both the strength and the polarity of the field.

The ESP8266 (NodeMCU) samples with a 10-bit ADC, so analogRead() returns 0–1023 across 3.3 V at the NodeMCU A0 header (the bare chip reads 0–1 V). With the quiescent point near 512 counts, the usable swing in each direction is about 512 counts, though in practice a small magnet moves it far less.

The KY-024 also carries an LM393 comparator and a potentiometer, so it offers a digital output alongside the analog one — convenient when you want both a proportional reading and a threshold trip from the same part.

Components Needed

  • ESP8266 (NodeMCU)
  • Linear Hall Effect Sensor
  • Resistors (if needed for sensor circuit)
  • Jumper Wires

Wiring to the ESP8266 (NodeMCU)

Connect AO to A0, optionally DO to D5, VCC to the 3.3 V rail and GND to ground. Because the output is ratiometric — it scales with supply voltage — power the sensor from the same rail that feeds the ADC reference, or the midpoint will not sit where you expect.

At 3.3 V the quiescent point sits near 1.65 V and the full swing is smaller than at 5 V, so resolution per millitesla is lower. The ESP32's 12-bit ADC recovers much of that.

Always record the no-magnet baseline at startup rather than assuming exactly mid-scale. Device-to-device variation of a hundred counts or more is normal, and subtracting a measured baseline removes it.

Module pinESP8266 (NodeMCU) pinFunction
A0 / AOA0Analog field strength, centred at mid-supply
D0 / DOD5Comparator trip, threshold set by the pot
VCC / +3V3Supply
GND / −GNDCommon ground

Example Code

Measuring field strength and polarity against a baseline captured at boot. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.

Measuring field strength and polarity against a baseline captured at boot
const int HALL_PIN = A0;
const int DEADBAND = 15;   // ignore small drift

int baseline = 0;

void setup() {
  Serial.begin(115200);
  delay(100);

  long total = 0;                 // capture quiescent level, no magnet nearby
  for (int i = 0; i < 64; i++) { total += analogRead(HALL_PIN); delay(2); }
  baseline = total / 64;

  Serial.print("Baseline: ");
  Serial.println(baseline);
}

void loop() {
  int raw   = analogRead(HALL_PIN);
  int delta = raw - baseline;     // signed: polarity as well as strength

  if (abs(delta) > DEADBAND) {
    Serial.print(delta > 0 ? "NORTH" : "SOUTH");
    Serial.print(" pole, strength ");
    Serial.println(abs(delta));
  }
  delay(100);
}

Applications

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

  • Contactless position sensing, where a magnet on a moving part reports travel
  • Current measurement, by sensing the field around a conductor
  • Joystick and control-knob position without wiping contacts to wear out
  • Distinguishing magnet polarity in sorting and orientation checks
  • Proximity sensing with proportional output, so approach speed can be judged

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:

  • The reading sits at mid-scale and barely moves — the magnet is too weak or too far; linear Hall parts have a much shorter useful range than digital switches.
  • The baseline drifts with temperature — AH49E output has a temperature coefficient. Re-capture the baseline periodically for long-running builds.
  • Values change when the supply changes — this is expected from a ratiometric part. Use a stable regulated rail.
  • Readings are noisy — average several samples, and keep the signal lead short and away from motor wiring.
  • 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

Treat the output as signed data relative to the baseline, never as an absolute number. The sketch above does this, and it is what makes polarity detection possible.

If your application only needs "magnet present or not", the digital KY-003 is cheaper, simpler and far more tolerant of distance. Reach for a linear sensor only when the amount matters.