To interface a linear hall sensor and magnet with an ESP32, wire the sensor VCC to 3.3V, GND to GND, and the analog output to an ADC-capable pin like GPIO 34. The output is a ratiometric voltage that shifts from its 1.65V null point as magnetic flux increases. By mapping the ESP32's 12-bit ADC reading back to voltage and applying the sensor's specific sensitivity rating, you can convert raw digital values into physical Gauss or Tesla measurements.

The Physics: How a Hall Sensor and Magnet Interact

When a constant current flows through a thin semiconductor plate and a magnetic field from a magnet passes perpendicularly through it, the Lorentz force pushes the charge carriers (electrons or holes) to one edge of the plate. This charge accumulation creates a measurable transverse voltage difference across the material known as the Hall voltage. This voltage is directly proportional to the magnetic flux density intersecting the sensor die.

In practical embedded projects, a hall sensor and magnet pair acts as a non-contact transducer. The raw microvolt-level Hall voltage is too small for a microcontroller to read directly, so integrated circuits (ICs) embed the semiconductor element alongside operational amplifiers. Depending on the IC architecture, this amplified signal is presented either as a continuous analog voltage proportional to the field strength, or as a digital logic-level switch that toggles at a specific magnetic threshold.

Sensor Selection and Wiring Pinouts

Before wiring anything, you must select the correct IC for your application. The most common mistake beginners make is conflating linear (analog) outputs with switch (digital) outputs. A linear sensor outputs a continuous voltage used for measuring distance, current, or field strength. A digital switch outputs a simple HIGH/LOW logic state used for limit switches, RPM counting, or door alarms. They are not interchangeable.

Common Hall Effect IC Specifications
IC Model Output Type Supply Range Sensitivity / Threshold Best Use Case
Honeywell SS49E Linear Analog 2.7V - 6.5V 1.4 mV/G (at 5V) Joystick position, linear displacement
Allegro A1302 Linear Analog 4.5V - 6.0V 1.3 mV/G (at 5V) Current sensing, proximity
TI DRV5055 Linear Analog 2.5V - 5.5V 50 mV/mT (Absolute) Precision 3.3V MCU field mapping
Melexis US1881 Digital Switch 3.5V - 24V Latch: ±3.5 mT Motor commutation, RPM counting

For this guide, we will focus on the SS49E linear analog sensor paired with an ESP32, as it is the most widely available module for hobbyists. Because the ESP32's ADC pins are strictly limited to 3.3V, we must power the SS49E from the ESP32's 3.3V rail, not the 5V VIN pin.

SS49E to ESP32 Wiring Pinout
SS49E Pin Function ESP32 Pin Implementation Notes
1 (Left) VCC 3V3 Do not use 5V; it will overdrive the ESP32 ADC.
2 (Center) GND GND Ensure a common ground plane to prevent floating offsets.
3 (Right) OUT GPIO 34 GPIO 34 is input-only and ADC-capable. Add a 100nF bypass cap between OUT and GND.

Output Signal Math: Raw ADC to Gauss

The output of a linear hall sensor is a ratiometric voltage. This means the quiescent (zero-magnet) output voltage and the sensitivity scale proportionally with the supply voltage. When powered at 5V, the SS49E outputs 2.5V at 0 Gauss, with a sensitivity of 1.4 mV/G. When we drop the supply to 3.3V for the ESP32, we must recalculate these baseline values.

At 3.3V VCC, the null voltage becomes exactly half the supply: 1.65V. The sensitivity scales down by the same ratio (3.3 / 5.0), yielding 0.924 mV/G (or 0.000924 V/G). The ESP32's 12-bit ADC maps the 0-3.3V range to integer values between 0 and 4095.

⚠️ ESP32 ADC Non-Linearity Warning: The ESP32's built-in ADC is notoriously non-linear at the extreme ends of its range (readings below 100 and above 3900). Fortunately, a ratiometric hall sensor naturally rests at 1.65V (ADC ~2048) when no magnet is present, keeping your measurements in the most linear middle-third of the ADC curve. Never attempt to measure extremely strong magnetic fields that push the ESP32 ADC past 3900 without using an external ADC like the ADS1115.

Here is the exact mathematical sequence to convert the raw ADC reading into Gauss:

  1. Convert Raw ADC to Voltage: Voltage = ADC_Raw * (3.3 / 4095.0)
  2. Calculate Delta from Null: Delta_V = Voltage - 1.65
  3. Convert to Gauss: Gauss = Delta_V / 0.000924

Below is the complete, copy-pasteable Arduino-framework C++ code implementing this math with basic oversampling to reduce noise. For deeper ESP32 ADC API documentation, refer to the official Espressif ADC Oneshot Driver documentation.


const int HALL_PIN = 34;
const int OVERSAMPLE = 16;
const float V_REF = 3.3;
const int ADC_MAX = 4095;
const float NULL_VOLTAGE = 1.65;
const float SENSITIVITY_3V3 = 0.000924; // V/Gauss at 3.3V supply

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  // Set attenuation to 11dB to allow full 0-3.3V range reading
  analogSetPinAttenuation(HALL_PIN, ADC_11db); 
}

void loop() {
  long adcSum = 0;
  for (int i = 0; i < OVERSAMPLE; i++) {
    adcSum += analogRead(HALL_PIN);
    delayMicroseconds(100);
  }
  
  float avgRaw = adcSum / (float)OVERSAMPLE;
  float voltage = avgRaw * (V_REF / ADC_MAX);
  float deltaV = voltage - NULL_VOLTAGE;
  
  // Polarity indicates North vs South magnetic pole
  float gauss = deltaV / SENSITIVITY_3V3; 
  
  Serial.print("Raw: "); Serial.print(avgRaw, 1);
  Serial.print(" | V: "); Serial.print(voltage, 3);
  Serial.print(" | Gauss: "); Serial.println(gauss, 1);
  
  delay(100);
}

Calibration, Scaling, and Interference Mitigation

While the theoretical math provides a solid baseline, real-world bench conditions require calibration and careful management of interference sources. The Hall effect is highly sensitive to environmental variables that can skew your physical unit calculations.

Calibration Procedure

To calibrate your specific hall sensor and magnet setup, you need to establish the true null offset and verify the sensitivity. Power the circuit and keep all magnets at least 12 inches away from the sensor. Read the average ADC value over 1000 samples; this is your true NULL_OFFSET. Do not assume it is exactly 2048. Manufacturing tolerances in the sensor's internal op-amp can shift the quiescent voltage by ±20mV. Update your code's NULL_VOLTAGE variable to reflect this measured baseline.

Next, introduce a magnet with a known field strength (or use a calibrated reference like an N52 neodymium disc at a fixed 10mm air gap) and record the output. If your calculated Gauss value deviates by more than 5% from the expected value, apply a linear scaling factor in your code to correct the sensitivity constant.

Common Interference Sources

When troubleshooting erratic readings, check these three primary interference vectors:

  • Thermal Drift: Hall sensors exhibit temperature coefficients. The SS49E has a sensitivity drift of roughly -0.06% per °C. If your project operates in an unconditioned garage or outdoors, a 30°C temperature swing will alter your sensitivity by nearly 2%. For high-precision outdoor applications, upgrade to a temperature-compensated IC like the TI DRV5055.
  • Electromagnetic Interference (EMI): Running your sensor wires parallel to AC mains cables or near switching power supplies will induce AC ripple on the analog output line. Always twist the signal wire with the ground wire, and solder a 100nF ceramic bypass capacitor directly across the VCC and GND pins at the sensor body, not at the microcontroller end.
  • Magnet Orientation and Geometry: A hall sensor and magnet pair must be aligned correctly. The SS49E measures the magnetic field perpendicular to its flat face. If you slide a magnet past the side edge of the sensor, the vector geometry will yield a non-linear cosine response, completely invalidating the raw-to-unit math. Always ensure the magnet's pole faces the sensor's branded flat side directly.

For a comprehensive breakdown of how different semiconductor materials affect thermal drift and baseline sensitivity, the All About Circuits semiconductor textbook chapter on Hall Effect Sensors provides excellent foundational physics context. By respecting the ratiometric nature of the analog output and keeping your signals out of the ESP32's non-linear ADC extremes, you can achieve highly reliable, non-contact magnetic measurements on the bench.