The Physics: How a HAL Sensor Works

At the silicon level, a HAL sensor relies on the Lorentz force. When a bias current flows through a thin semiconductor element and a magnetic field is applied perpendicular to that current, the charge carriers (electrons or holes) are deflected to one side of the material. This accumulation of charge creates a measurable transverse voltage across the element, known as the Hall voltage. The strength of this voltage is directly proportional to the magnetic flux density passing through the semiconductor.

In raw form, this Hall voltage is incredibly small—often just microvolts. Modern integrated circuits like the Texas Instruments DRV5053 package the Hall element alongside a low-noise operational amplifier, a voltage regulator, and temperature-compensation circuitry. This internal signal conditioning boosts the microvolt signal into a clean, usable analog voltage that scales linearly with the magnetic field, allowing microcontrollers to read it directly via an Analog-to-Digital Converter (ADC).

Analog vs. Digital Outputs: Don't Conflate Them

The most common mistake hobbyists make is buying a digital Hall switch when they need a linear measurement, or vice versa. You must know exactly what the output actually is before wiring it to your microcontroller.

  • Analog (Linear) HAL Sensors: Output a continuous voltage proportional to the magnetic field strength. At zero magnetic field, the output sits at a quiescent offset (usually Vcc/2). As a magnetic pole approaches, the voltage rises or falls. Use these for measuring proximity, fluid level (via floating magnets), or joystick position.
  • Digital (Switch) HAL Sensors: Output a binary digital signal (HIGH or LOW). They contain an internal Schmitt trigger and an open-drain or push-pull transistor. They snap LOW when the magnetic field crosses a specific threshold (e.g., 30 Gauss) and snap HIGH when it drops below a release threshold. Use these for RPM counting, limit switches, or door alarms.
Bench Tip: If your sensor has three pins (VCC, GND, OUT) but the datasheet mentions an "open-collector" output and a "Bop/Brp" threshold, you have a digital switch. You will need a pull-up resistor on the OUT pin, and it will not give you a proportional voltage.

Wiring the DRV5053 Analog HAL Sensor to ESP32

For this guide, we are using the TI DRV5053 (specifically the DRV5053RA variant), a ratiometric analog sensor that operates natively at 3.3V, making it perfect for the ESP32 without needing logic level shifters.

Sensor Pin Function ESP32 Connection Notes & Supply Range
VCC (Pin 1) Power Supply 3V3 Supply range is 2.5V to 5.5V. Use 3.3V for direct ESP32 ADC compatibility.
OUT (Pin 3) Analog Output GPIO34 (ADC1_CH6) Outputs 0.2V to 1.8V (at 3.3V Vcc). Do not use ADC2 pins if using WiFi.
GND (Pin 2) Ground GND Keep ground return path short to avoid ground-loop noise.

Wiring Steps:

  1. Connect the sensor VCC to the ESP32 3V3 pin. Do not use the 5V VIN pin; exceeding 3.3V on the output will damage the ESP32's ADC.
  2. Connect the sensor GND to the ESP32 GND.
  3. Connect the sensor OUT pin directly to ESP32 GPIO34.
  4. Hardware Filtering (Crucial): Solder a 1kΩ series resistor on the OUT line, and place a 100nF ceramic capacitor from the ADC side of the resistor to GND. This creates a ~1.6kHz low-pass RC filter to kill high-frequency switching noise.

Signal Math: Converting Raw Readings to MilliTesla

The ESP32's raw 12-bit ADC (`analogRead()`) is notoriously non-linear and suffers from attenuation errors. Never use raw 0-4095 values for precision sensor math. Instead, use the ESP-IDF calibrated function `analogReadMilliVolts()`, which applies factory-stored eFuse calibration data to return an accurate millivolt reading.

The Scaling Math:
The DRV5053RA has a sensitivity of 100 mV/mT (milliTesla). At zero magnetic field, the output is exactly Vcc/2. If Vcc is 3.3V (3300 mV), the zero-field offset is 1650 mV.

The formula to convert the measured voltage to magnetic flux density (B) is:

B (mT) = (V_measured_mV - V_zero_mV) / Sensitivity

Worked Example:
You place a neodymium magnet near the sensor. The ESP32 reads analogReadMilliVolts(34) and returns 2150 mV.
B = (2150 - 1650) / 100
B = 500 / 100 = 5.0 mT (or 50 Gauss).

Calibration & Interference Gotchas:
1. Zero-Offset Drift: Before taking measurements, read the sensor with no magnets present and store that value as your dynamic V_zero in software. This accounts for minor Vcc fluctuations.
2. AC Mains Interference: If your sensor is near AC wiring, the 50Hz/60Hz electromagnetic field will induce a ripple in your readings. If your RC hardware filter isn't enough, implement a software moving-average filter (e.g., average 20 samples taken over exactly one AC cycle: 20ms for 50Hz or 16.6ms for 60Hz) to mathematically cancel the interference.
3. Package Stress: Hall elements are sensitive to mechanical stress. If you bend the PCB or over-tighten a mounting screw near the sensor, the piezoresistive effect in the silicon will shift your zero-offset. Mount the sensor on a rigid, stress-free area of the board.

Frequently Asked Questions

Why is my hal sensor ADC reading jumping around on the ESP32?

The ESP32's ADC has an inherent noise floor of roughly ±30mV when using raw reads, which translates to ±0.3 mT of jitter on a 100mV/mT sensor. To fix this, first ensure you are using analogReadMilliVolts() instead of analogRead(). Second, add the 1kΩ/100nF hardware RC filter mentioned in the wiring steps. Finally, average 16 to 32 consecutive samples in your code before calculating the final milliTesla value.

Can I use a digital hal sensor for measuring magnetic field strength?

No. A digital HAL sensor (like the A3144) only outputs a binary HIGH or LOW based on an internal threshold. It cannot tell you if the magnetic field is 10 Gauss or 100 Gauss, only that it has crossed the trip point (usually around 30-50 Gauss). If your project requires measuring the distance to a magnet or the exact field strength (like a DIY gaussmeter or fluid level sensor), you must use a linear analog sensor.

How close does the magnet need to be to the hal sensor?

Magnetic flux density follows an inverse-cube law relative to distance from a dipole magnet. A standard 10mm x 3mm N52 neodymium disc magnet will output roughly 2.5 mT at 10mm away, but drops to under 0.5 mT at 25mm away. For reliable analog readings with the DRV5053 (which maxes out around ±21 mT at 3.3V), keep the magnet within 5mm to 15mm of the sensor face. Always orient the magnet so its poles face the flat top of the sensor package, as Hall elements are primarily sensitive to perpendicular (Z-axis) magnetic fields.