The Physics: What Are Hall Effect Sensors Actually Measuring?
When a current-carrying conductor is placed in a magnetic field, the Lorentz force pushes the charge carriers to one side of the material, creating a measurable transverse voltage. This is the Hall effect. The magnitude of this voltage is directly proportional to the strength of the magnetic field passing perpendicularly through the conductor, allowing us to translate invisible magnetic flux into a readable electrical signal.
In practical silicon Hall effect sensors (like the Texas Instruments DRV5055 or Allegro A1302), a constant bias current is driven through a thin semiconductor die. When a magnetic field penetrates the die, the resulting microvolt-level Hall voltage is amplified by internal op-amps and conditioned. The IC then outputs this data as either a proportional analog voltage or a digital logic pulse, depending on the specific sensor architecture.
Analog vs. Digital: Choosing the Right Output Type
A common mistake on the bench is conflating analog linear sensors with digital switches. They serve entirely different purposes and wire up differently.
Analog (Linear) Sensors: These output a continuous voltage proportional to the magnetic flux density. At zero magnetic field, the output sits at a quiescent voltage (usually VCC/2). As a north pole approaches, the voltage rises; as a south pole approaches, it falls. You use these for measuring distance, joystick position, or current sensing. Examples: DRV5055, SS49E, A1302.
Digital (Switch) Sensors: These contain an internal Schmitt trigger. The output pin (usually open-drain) snaps LOW when the magnetic field exceeds a specific operate point (Bop) and releases HIGH when it drops below a release point (Brp). The gap between Bop and Brp is hysteresis, which prevents mechanical chatter. You use these for limit switches, RPM counting, or door-closed detection. Examples: A3144, US5881, DRV5013.
Wiring and Pinout Reference for ESP32
Below is the wiring reference for the Texas Instruments DRV5055A4, a 3.3V-native analog Hall sensor that is safe to connect directly to an ESP32 without level shifting.
| Sensor Pin | Function | Supply Range | ESP32 Connection | Wiring Notes |
|---|---|---|---|---|
| 1 (VCC) | Power Supply | 2.5V to 5.5V | 3V3 Pin | Use a 100nF ceramic bypass capacitor between VCC and GND as close to the sensor as possible to filter high-frequency noise. |
| 2 (GND) | Ground | N/A | GND Pin | Keep the ground return path short to avoid ground loop offsets. |
| 3 (OUT) | Analog Output | 0V to VCC | GPIO 34 (ADC1_CH6) | GPIO 34 is input-only and has no internal pull-up/pull-down resistors, making it ideal for raw analog reads. |
The Math: Converting Raw ADC Readings to Millitesla
Reading the sensor is only half the job; converting the ESP32's raw ADC integer into a physical unit (millitesla or Gauss) requires understanding both the sensor's sensitivity and the ESP32's ADC quirks.
The ESP32's 12-bit ADC theoretically outputs 0 to 4095. However, with the standard 11dB attenuation required to read up to ~3.3V, the ADC is notoriously non-linear at the top end, practically maxing out around 3.1V (roughly 3900 raw). We must account for this in our scaling math.
DRV5055A4 Specifications (at VCC = 3.3V):
- Quiescent Voltage (Vq): 1.65V (Zero magnetic field)
- Sensitivity (S): 132 mV/mT (or 0.132 V/mT)
Step-by-Step Conversion Math:
- Calculate Actual Voltage:
V_out = (Raw_ADC / 4095.0) * 3.1 - Find Voltage Delta:
Delta_V = V_out - 1.65 - Calculate Magnetic Field (mT):
B_mT = Delta_V / 0.132
// ESP32 Arduino C++ Implementation
const int adcPin = 34;
const float vcc_max = 3.1; // Practical max for ESP32 11dB attenuation
const float vq = 1.65; // Quiescent voltage at 3.3V VCC
const float sensitivity = 0.132; // 132 mV/mT converted to V/mT
void setup() {
Serial.begin(115200);
analogReadResolution(12);
analogSetPinAttenuation(adcPin, ADC_11db);
}
void loop() {
int raw = analogRead(adcPin);
float vOut = (raw / 4095.0) * vcc_max;
float deltaV = vOut - vq;
float bField_mT = deltaV / sensitivity;
Serial.printf("Raw: %d | Voltage: %.2f V | Field: %.2f mT\n", raw, vOut, bField_mT);
delay(100);
}
Real-World Interference and Calibration
Hall sensors are incredibly susceptible to environmental interference. Because they measure magnetic flux, any changing current in the vicinity will induce a reading. Stepper motors are the worst offenders on the bench; the phase currents switching in the stator windings generate massive localized magnetic fields that will cause your sensor readings to oscillate wildly. Keep linear Hall sensors at least 2 inches away from stepper motor bodies and unshielded power inductors.
Calibration is mandatory. The internal op-amps in Hall ICs have slight offset errors. The quiescent voltage (Vq) is rarely exactly VCC/2. To calibrate, power the circuit, keep all magnets away, and average 500 raw ADC reads. Calculate the actual Vq from that average and replace the hardcoded 1.65 in the code above with your measured baseline. Furthermore, Hall sensors exhibit temperature drift (typically -0.05% to -0.1% per °C). If your application operates in an unheated garage or near a hot 3D printer bed, you must read an onboard thermistor and apply a temperature compensation curve to maintain accuracy.
Frequently Asked Questions
What are Hall effect sensors used for in 3D printers and CNC machines?
In motion control, digital Hall sensors (like the A3144) are used as non-contact endstops. Unlike mechanical microswitches, they have no physical contacts to oxidize or bounce, providing repeatable homing accuracy down to 0.01mm. Analog Hall sensors are used in devices like the BLTouch or magnetic levitation bed leveling probes, where the precise distance to the print bed alters the magnetic flux through the sensor, allowing the firmware to map bed topography.
Can a Hall effect sensor measure high AC current without direct contact?
Yes, this is the basis of clamp meters and industrial current transducers. By running the AC current-carrying wire through a ferromagnetic toroidal core (a flux concentrator) with a small air gap, the magnetic field generated by the current (Ampere's Law) is focused into the gap. A linear Hall sensor placed in that gap measures the flux density, which is directly proportional to the AC current. For DIY AC current sensing, however, dedicated current transformer (CT) modules or the ACS712 IC are generally easier to interface than a bare Hall element.
Why are my Hall sensor ADC readings jumping erratically on the ESP32?
Erratic jumps are almost always caused by electromagnetic interference (EMI) or poor grounding. First, ensure you have a 100nF bypass capacitor soldered directly across the VCC and GND pins of the sensor. Second, check your wiring: long, unshielded jumper wires act as antennas for the 50/60Hz magnetic fields radiating from nearby mains wiring. Twist the signal and ground wires together to reject common-mode noise. Finally, if the jumps only happen when a relay or motor switches, you need to physically relocate the sensor away from the inductive load or add a ferrite bead to the sensor's power line.






