The Physics: How a Hall Sensor Works
When an electrical current flows through a semiconductor material, the charge carriers (electrons or holes) move in a straight line. If you introduce a magnetic field perpendicular to that current, the Lorentz force deflects the charge carriers to one side of the material. This accumulation of charge on one edge creates a measurable transverse voltage difference across the semiconductor, known as the Hall voltage.
This Hall voltage is directly proportional to the strength of the perpendicular magnetic flux density (the B-field). By amplifying this microvolt-level signal with an internal operational amplifier, a linear hall sensor outputs a continuous, ratiometric analog voltage that scales predictably with the magnetic field strength, allowing microcontrollers to measure exact magnetic flux density rather than just detecting presence.
Analog vs. Digital: Know Your Output Type
A common mistake in embedded projects is conflating linear (analog) and switch (digital) hall sensors. While both rely on the same physical principle, their internal signal conditioning is entirely different.
- Linear Hall Sensors (e.g., Allegro A1302, Honeywell SS49E): Output a continuous analog voltage. The quiescent (zero-magnetic-field) output is typically half the supply voltage (VCC/2). A south magnetic pole increases the voltage; a north pole decreases it. Use these for measuring stroke, position, or current.
- Digital Hall Switches (e.g., A3144, US1881): Output a binary logic level (HIGH/LOW). They contain an internal Schmitt trigger and an open-drain or push-pull transistor. They snap ON at a specific magnetic threshold (B_op) and snap OFF at a lower release threshold (B_rp) to prevent chatter. Use these for RPM counting, limit switches, or latch detection.
This guide focuses strictly on interfacing the linear analog hall sensor to extract continuous physical measurements.
Wiring the A1302 Linear Hall Sensor to ESP32
The Allegro A1302 is a standard 3-pin linear hall sensor. While its datasheet specifies a nominal 5V supply, it operates perfectly down to 3.3V, which is critical for direct ESP32 interfacing without frying the GPIO pins.
| A1302 Pin | Function | ESP32 Connection | Notes & Supply Range |
|---|---|---|---|
| 1 (VCC) | Power Supply | 3V3 | Operates 3.3V to 5V. Use 3.3V to match ESP32 ADC max input. |
| 2 (OUT) | Analog Output | GPIO 34 (ADC1_CH6) | Outputs VCC/2 at zero Gauss. Max output is VCC. |
| 3 (GND) | Ground | GND | Common ground required for accurate ADC referencing. |
Always use ADC1 pins (GPIO 32-39) for analog sensors on the ESP32. ADC2 pins are shared with the WiFi radio; if you initialize WiFi or Bluetooth, ADC2 readings will fail or return garbage data. GPIO 34, 35, 36, and 39 are input-only and ideal for sensors.
Wiring Steps
- Connect the A1302 Pin 1 (VCC) to the ESP32 3V3 pin. Do not use the 5V (VIN) pin unless you have a voltage divider on the output.
- Connect A1302 Pin 3 (GND) to the ESP32 GND.
- Connect A1302 Pin 2 (OUT) to ESP32 GPIO 34.
- Place a 100nF (0.1µF) ceramic decoupling capacitor directly across the VCC and GND pins of the sensor on your breadboard to filter high-frequency switching noise.
The Math: Converting Raw ADC to Gauss
To convert the sensor's analog output into a physical unit (Gauss or Tesla), we must account for the ESP32's ADC characteristics and the sensor's ratiometric sensitivity.
1. Bypassing the ESP32's Non-Linear ADC
The ESP32's native 12-bit ADC (0-4095) is notoriously non-linear, particularly at the extreme low and high voltage ends. Instead of using analogRead() and doing manual voltage mapping, use the analogReadMilliVolts() function available in ESP32 Arduino Core v2.x and later. This function uses the chip's internal eFuse calibration data to return a highly accurate millivolt reading.
2. Ratiometric Sensitivity Scaling
The A1302 datasheet lists a nominal sensitivity of 1.3 mV/G when powered at 5V. Because the sensor is ratiometric, its sensitivity scales linearly with the supply voltage. When powered at 3.3V, the sensitivity becomes:
Sensitivity_3.3V = 1.3 mV/G * (3.3V / 5.0V) = 0.858 mV/G
3. The Conversion Formula
At 3.3V, the quiescent (zero-field) output voltage is exactly half the supply: 3300 mV / 2 = 1650 mV. The formula to calculate the magnetic field in Gauss is:
B (Gauss) = (V_out_mV - 1650) / 0.858
Complete ESP32 Arduino Code
// ESP32 Linear Hall Sensor (A1302) Interfacing
// Requires ESP32 Arduino Core v2.0.0 or higher
const int HALL_PIN = 34; // ADC1 pin
const float VCC_MV = 3300.0; // 3.3V supply in millivolts
const float QUIESCENT_MV = VCC_MV / 2.0; // 1650 mV
const float SENSITIVITY_MV_G = 1.3 * (VCC_MV / 5000.0); // 0.858 mV/G
void setup() {
Serial.begin(115200);
analogReadResolution(12); // Ensure 12-bit resolution
pinMode(HALL_PIN, INPUT);
Serial.println("Calibrating zero-field offset...");
}
void loop() {
// Read voltage in millivolts using eFuse calibrated ADC
int v_out_mv = analogReadMilliVolts(HALL_PIN);
// Calculate Gauss
float gauss = (v_out_mv - QUIESCENT_MV) / SENSITIVITY_MV_G;
// Convert to Tesla (1 Tesla = 10,000 Gauss)
float tesla = gauss / 10000.0;
Serial.printf("Voltage: %d mV | Field: %.2f Gauss | %.4f Tesla\n",
v_out_mv, gauss, tesla);
delay(100); // 10 Hz sample rate
}
Interference, Drift, and Real-World Gotchas
Linear hall sensors are highly susceptible to environmental noise. If your readings are unstable, check these common interference sources:
- Electromagnetic Interference (EMI): Hall sensors will pick up alternating magnetic fields from nearby AC wiring, stepper motors, or switching power supplies. Keep the sensor and its wiring away from inductive loads. Twisted-pair wiring for the sensor leads can help reject common-mode magnetic noise.
- Thermal Drift: The quiescent output voltage and sensitivity drift with temperature. The A1302 has a quiescent voltage temperature coefficient of roughly -1 to +1 mV/°C. In precision applications, you must implement software temperature compensation using a co-located thermistor, or switch to a specialized zero-drift hall IC like the Allegro ALS31300.
- Mechanical Stress: Piezoresistive effects in the semiconductor die can cause offset shifts if the sensor's plastic package is bent or subjected to mechanical stress from tight PCB mounting. Avoid bending the leads sharply at the package base.
Frequently Asked Questions
Why is my hall sensor reading fluctuating wildly when no magnet is present?
Wild fluctuations are almost always caused by EMI or an unstable power supply. The ESP32's 3.3V LDO regulator can sometimes carry high-frequency switching noise from the onboard WiFi/Bluetooth radio. First, ensure you have a 100nF ceramic capacitor placed as close to the sensor's VCC and GND pins as physically possible. Second, implement a software low-pass filter (like an exponential moving average) in your code to smooth out high-frequency ADC jitter.
Can a linear hall sensor measure AC current?
Yes, but indirectly. By placing the linear hall sensor in the air gap of a ferromagnetic core (like a split-core current transformer) that encircles an AC wire, the sensor will output an AC voltage proportional to the alternating magnetic field generated by the current. To get a usable reading on a DC-biased microcontroller ADC, you must sample the waveform at a high rate (e.g., 1 kHz or higher) and calculate the Root Mean Square (RMS) value in software. For dedicated AC/DC current sensing, consider an integrated hall current sensor like the ACS712 or ACS724 instead.
What is the difference between a hall sensor and a reed switch?
A reed switch is a purely mechanical, magnetically actuated relay consisting of two ferromagnetic metal reeds sealed in a glass tube. It draws zero power when idle and can switch high voltages directly, but it suffers from mechanical bounce, limited lifecycle (typically 10^6 to 10^7 operations), and slow response times. A hall sensor is a solid-state semiconductor device. It requires continuous power, outputs a low-voltage logic or analog signal, but offers infinite lifecycle, zero contact bounce, and the ability to measure continuous magnetic field strength rather than just binary presence.






