When a current-carrying semiconductor is placed in a magnetic field, the Lorentz force deflects the charge carriers toward one edge of the material. This charge accumulation creates a transverse voltage differential known as the Hall effect. In a raw semiconductor, this voltage is in the microvolt range and highly temperature-dependent, making it useless for direct microcontroller interfacing.
A practical linear hall element sensor (such as the Texas Instruments DRV5055 or Honeywell SS49E) solves this by integrating the raw Hall element with a low-noise operational amplifier and temperature-compensation circuitry on a single silicon die. The result is a ratiometric analog output voltage that scales linearly with the perpendicular magnetic flux density (measured in Gauss or milliTesla), allowing you to measure continuous magnetic field strength rather than just detecting a simple on/off threshold.
Wiring and Pinout for 3.3V Microcontrollers
While older 5V parts like the SS49E are common in hobby kits, modern embedded design heavily favors 3.3V logic. The TI DRV5055 is a 3.3V-native linear hall element sensor that pairs perfectly with the ESP32's ADC without needing a logic-level shifter or voltage divider. The output is strictly an analog voltage; it does not output a digital PWM or I2C signal.
| Pin Number | Function | ESP32 Connection | Notes & Supply Range |
|---|---|---|---|
| 1 | VCC | 3V3 | Supply range: 2.5V to 5.5V. Use 3.3V for direct ESP32 ADC compatibility. |
| 2 | VOUT | GPIO 34 (ADC1_CH6) | Analog output. Range: 0.2V to VCC - 0.2V. Ratiometric to VCC. |
| 3 | GND | GND | Must share common ground with the microcontroller. |
- Power the Sensor: Connect Pin 1 to the ESP32 3V3 pin and Pin 3 to GND.
- Decouple: Solder a 100nF capacitor across Pin 1 and Pin 3.
- Route the Signal: Connect Pin 2 (VOUT) to an ADC1 pin on the ESP32 (e.g., GPIO 34). Avoid ADC2 pins (GPIO 25, 26, 27) as they conflict with the WiFi radio.
- Verify Quiescent Voltage: With no magnet present, measure VOUT with a multimeter. It should read exactly VCC/2 (approx 1.65V).
Output Signal Math: Converting Raw ADC to MilliTesla
The most common mistake makers make with analog magnetic sensors is treating the ADC reading as an absolute unit. The output of a linear hall element sensor is ratiometric, meaning the zero-field offset (quiescent voltage) and the sensitivity scale proportionally with the supply voltage. If your 3.3V rail sags to 3.2V, both your baseline and your sensitivity shift.
For the DRV5055A2 variant powered at exactly 3.3V:
- Quiescent Voltage (V_Q): 1.65V (Output at 0 mT)
- Sensitivity (S): 33 mV/mT (or 3.3 mV/Gauss)
- ESP32 ADC Resolution: 12-bit (0 to 4095)
- ADC Reference Voltage: 3.3V (nominal)
The math to convert a raw 12-bit ADC reading into milliTesla (mT) is:
V_out = ADC_raw * (3.3 / 4095.0)
B_mT = (V_out - 1.65) / 0.033
Here is the complete, copy-pasteable ESP32 Arduino code implementing this math with a basic moving average filter to smooth out ADC jitter:
// Linear Hall Element Sensor (DRV5055A2) Interfacing for ESP32
const int hallPin = 34; // ADC1 pin
const float VCC = 3.3; // Supply voltage
const int ADC_MAX = 4095; // 12-bit resolution
const float V_QUIESCENT = 1.65; // Zero-field voltage (VCC/2)
const float SENSITIVITY = 0.033; // 33 mV/mT converted to V/mT
float readMagneticField() {
long sum = 0;
// Oversample 16 times to reduce noise and effectively increase resolution
for(int i = 0; i < 16; i++) {
sum += analogRead(hallPin);
delayMicroseconds(50);
}
float avgADC = sum / 16.0;
// Convert ADC to Voltage
float vOut = avgADC * (VCC / ADC_MAX);
// Convert Voltage to milliTesla (mT)
float field_mT = (vOut - V_QUIESCENT) / SENSITIVITY;
return field_mT;
}
void setup() {
Serial.begin(115200);
analogReadResolution(12); // Ensure 12-bit mode
Serial.println("Linear Hall Element Sensor Initialized.");
}
void loop() {
float mT = readMagneticField();
// 1 mT = 10 Gauss
float gauss = mT * 10.0;
Serial.print("Field: ");
Serial.print(mT, 2);
Serial.print(" mT | ");
Serial.print(gauss, 1);
Serial.println(" Gauss");
delay(100);
}
Calibration and Magnetic Interference Sources
Datasheets provide nominal values, but real-world physics demands calibration. The quiescent voltage (V_Q) of a hall element sensor has a manufacturing tolerance of roughly ±10%. Furthermore, the ESP32's internal ADC is notoriously non-linear and suffers from offset errors. To calibrate, power up the circuit with absolutely no ferromagnetic materials or magnets within a 1-meter radius. Read the raw ADC value, convert it to voltage, and hardcode that exact value as your V_QUIESCENT constant instead of assuming 1.65V. For high-precision applications, implement a two-point calibration using a known reference magnet.
According to Texas Instruments' sensor design guidelines, magnetic interference is the primary cause of erratic readings in embedded systems. Common interference sources include:
- Ferromagnetic Metals: Steel standoffs, iron-core inductors, or even the metal casing of a nearby battery will bend the magnetic flux lines, creating localized 'dead zones' or amplifying the field unpredictably.
- AC Current Carrying Wires: Mains wiring or high-current stepper motor phases generate a 50Hz/60Hz alternating magnetic field. This induces a low-frequency ripple in your analog output. Use a digital low-pass filter in software or twist your sensor wires to reject common-mode magnetic noise.
- Temperature Drift: While modern ICs have internal temperature compensation, extreme thermal gradients (e.g., placing the sensor near a TO-220 voltage regulator) will cause the zero-field offset to drift by roughly 0.05% per °C.
Frequently Asked Questions
What is the difference between a linear hall element sensor and a digital hall switch?
A linear hall element sensor outputs a continuous analog voltage proportional to the magnetic field strength, allowing you to measure distance, angle, or exact flux density. A digital hall switch (like the A3144) contains an internal Schmitt trigger and open-drain transistor; it outputs a strict HIGH or LOW logic signal only when the field crosses a specific threshold (e.g., 30 Gauss). Use linear sensors for joysticks and current sensing; use digital switches for RPM counting and limit switches.
Can a hall element sensor measure AC current accurately?
Yes, and this is a primary industrial use case (e.g., in current sensing applications). By placing the sensor in the air gap of a toroidal ferrite core wrapped around an AC-carrying wire, the sensor reads the induced magnetic field. However, to measure AC accurately, your microcontroller's sampling rate must be at least 10x the AC frequency (e.g., 1kHz sampling for 50Hz mains) to capture the sine wave peaks, and you must calculate the True RMS value in software.
Why is my hall element sensor analog reading drifting over time?
If your reading drifts slowly over minutes, the culprit is almost always thermal drift. The internal amplifier and the Hall element itself have temperature coefficients. If you mounted the sensor on a PCB near a heat-generating component (like a linear regulator or motor driver), the ambient temperature rise shifts the quiescent voltage. Ensure adequate thermal isolation, or implement a software baseline-tracking algorithm that slowly re-centers the zero-point when the magnet is known to be absent.
How close does the magnet need to be to a linear hall element sensor?
The magnetic field strength of a standard dipole magnet drops off according to the inverse-cube law ($1/r^3$). This means if you double the distance between the magnet and the sensor face, the field strength drops to one-eighth (12.5%) of its original value. For a typical 10mm neodymium magnet, the usable linear range for a DRV5055 is roughly 5mm to 30mm. Closer than 5mm, the sensor will saturate (clip at the maximum voltage); further than 40mm, the signal will drown out in the ADC noise floor.






