The most common linear position sensor types for microcontroller projects are resistive slide potentiometers, linear Hall effect sensors, and LVDTs. They output either ratiometric analog voltage, absolute analog voltage, or digital signals depending on the transducer physics. Choosing the right one depends on your required travel distance, resolution, and environmental conditions. This guide breaks down the exact specifications, wiring pinouts, and raw-to-unit math needed to interface these sensors with an ESP32 or Arduino, including the calibration code to convert noisy ADC readings into precise millimeter measurements.
Core Linear Position Sensor Types and Specifications
Linear position sensors convert physical displacement along a single axis into an electrical signal. Resistive types use a wiper sliding across a carbon or cermet track to change resistance, while magnetic types (Hall effect) measure the flux density gradient of a moving magnet. Inductive types like LVDTs (Linear Variable Differential Transformers) rely on the mutual inductance change between a primary coil and two secondary coils as a ferromagnetic core moves through them, a principle detailed extensively in All About Circuits.
Capacitive and magnetostrictive sensors operate on time-of-flight or dielectric shift principles, offering sub-micron resolution but requiring complex signal conditioning. For hobbyist and light-industrial microcontroller projects, resistive and Hall effect sensors dominate due to their direct analog voltage outputs and low cost, whereas LVDTs and magnetostrictive sensors require dedicated AC excitation or specialized interface ICs.
| Sensor Type | Example Part Number | Travel Range | Resolution | Output Type | Typical Price |
|---|---|---|---|---|---|
| Resistive Slide | Bourns PTB0143-2010DPB | 100 mm | ~0.5% (Analog) | Ratiometric Analog (V) | $3.50 |
| Linear Hall Effect | Allegro A1324 | 10-30 mm (magnet dep.) | 12-bit (ADC dep.) | Absolute Analog (V) | $1.20 |
| LVDT (Inductive) | TE Connectivity DC-EC | 2 mm to 500 mm | Infinite (Analog) | Differential AC / DC (V) | $145.00 |
| Magnetostrictive | Balluff BTL7 | 50 mm to 7600 mm | 0.01 mm | Digital (SSI/IO-Link) | $450.00 |
| Capacitive | Micro-Epsilon capaNCDT | 0.5 mm to 25 mm | <0.1 µm | Analog (V) / Digital | $800.00+ |
Wiring, Pinouts, and Signal Conditioning
It is critical not to conflate analog and digital outputs when wiring these sensors. A slide potentiometer outputs a ratiometric analog voltage that scales with your supply voltage (VCC). If VCC sags, your reading shifts. A linear Hall effect sensor outputs an absolute analog voltage centered at VCC/2, where the deviation from that midpoint indicates magnetic flux density. Digital sensors (like I2C capacitive sensors) output discrete binary packets and require pull-up resistors on their data lines.
| Sensor Type | VCC Supply Range | GND | Signal Pin | ESP32 / Arduino Pin Mapping |
|---|---|---|---|---|
| Slide Potentiometer | 3.3V to 5.0V | Common Ground | Wiper (Analog V) | ESP32: GPIO 34 (ADC1_CH6) Arduino: A0 |
| Linear Hall Effect | 3.0V to 5.5V | Common Ground | OUT (Analog V) | ESP32: GPIO 35 (ADC1_CH7) Arduino: A1 |
| LVDT (w/ AD698 Driver) | 12V to 24V (Driver) | Driver Ground | Driver Vout (0-5V) | ESP32: GPIO 32 (ADC1_CH4) Arduino: A2 |
For the linear Hall effect sensor, the physical air gap between the sensor die and the moving magnet is your mechanical linkage. As Allegro MicroSystems notes in their design guides, the magnetic field gradient is non-linear at the extremes of the magnet's travel, so you must mechanically restrict the travel range to the linear center zone of the flux field.
Raw-to-Unit Math and Calibration Code
Converting raw ADC counts into physical units (millimeters or inches) requires understanding your sensor's transfer function.
Resistive Sensor Math:
Because the output is ratiometric, the physical position is a direct ratio of the voltage out versus the reference voltage, multiplied by the maximum mechanical travel.
Position (mm) = (V_out / V_ref) * Travel_max
Linear Hall Effect Math:
Hall sensors output a quiescent voltage (usually VCC/2) when no magnet is present. The sensitivity is given in mV/mT (millivolts per millitesla).
Position (mm) = ((V_out - V_quiescent) / Sensitivity) * Flux_Gradient_Factor
In practice, calculating the exact magnetic flux gradient factor is nearly impossible without a gaussmeter. Instead, we use a two-point linear calibration. You move the actuator to the physical 0mm mark, record the raw ADC value, move it to the physical 100mm mark, and record the second raw ADC value. The microcontroller then uses the map() function or a linear equation to interpolate the space between.
Below is a complete, copy-pasteable ESP32 Arduino sketch that reads a slide potentiometer, applies an Exponential Moving Average (EMA) filter to kill contact bounce noise, and maps the raw 12-bit ADC reading to millimeters.
// ESP32 Linear Position Sensor Calibration Code
const int sensorPin = 34; // GPIO 34 (ADC1_CH6)
// Calibration points (Measure these with a multimeter/serial monitor)
const int rawZeroMM = 120; // Raw ADC reading at 0mm physical stop
const int rawMaxMM = 3950; // Raw ADC reading at 100mm physical stop
const float travelMax = 100.0; // Max travel in mm
// EMA Filter state
float filteredADC = 0;
const float alpha = 0.15; // Lower = smoother but slower response
void setup() {
Serial.begin(115200);
analogReadResolution(12); // Set ESP32 ADC to 12-bit (0-4095)
analogSetAttenuation(ADC_11db); // Full range ~0-3.1V
// Prime the filter
filteredADC = analogRead(sensorPin);
}
void loop() {
int rawReading = analogRead(sensorPin);
// Apply Exponential Moving Average to smooth carbon track noise
filteredADC = (alpha * rawReading) + ((1.0 - alpha) * filteredADC);
// Constrain to calibrated bounds to prevent negative/over-travel math errors
float constrainedADC = constrain(filteredADC, rawZeroMM, rawMaxMM);
// Map raw ADC to physical millimeters
float positionMM = map(constrainedADC, rawZeroMM, rawMaxMM, 0, travelMax * 100);
positionMM = positionMM / 100.0; // map() doesn't support floats natively
Serial.print("Raw: ");
Serial.print(rawReading);
Serial.print(" | Filtered: ");
Serial.print(filteredADC, 1);
Serial.print(" | Position: ");
Serial.print(positionMM, 2);
Serial.println(" mm");
delay(20); // 50Hz sample rate
}
Interference, Noise, and Troubleshooting
Linear position sensors are highly susceptible to environmental and electrical interference. When your readings jump erratically or drift over time, trace the issue using this ranked decision path:
- Power Supply Ripple (Analog Sensors): Because slide pots are ratiometric, any noise on your 3.3V or 5V rail directly injects into your signal. Fix: Add a 100nF ceramic capacitor and a 10µF tantalum capacitor in parallel across the sensor's VCC and GND pins, placed as close to the sensor body as possible.
- EMI from Stepper Motors/VFDs (Hall & LVDT): Magnetic sensors will pick up stray flux from nearby unshielded stepper motors or high-current AC wiring. Fix: Use twisted-pair shielded cable for the signal line, grounding the shield at the microcontroller end only to prevent ground loops. For Hall sensors, switch to a ratiometric Hall IC rather than an absolute one if your VCC is clean, as common-mode noise rejection is better.
- Wiper Contact Bounce / Carbon Dust (Resistive): Over time, mechanical slide pots accumulate carbon dust inside the housing, causing dead spots and micro-arcing that looks like massive spikes on an oscilloscope. Fix: If cleaning with contact cleaner fails, replace the pot with a cermet-track variant (which handles higher wiper currents without burning) or switch to a non-contact linear Hall effect setup.
- Ground Loops (LVDT / Industrial Sensors): If you are using an LVDT driver powered by a separate 24V industrial supply and reading it with an Arduino powered by a USB laptop supply, the differing ground potentials will cause a massive DC offset or 50/60Hz hum. Fix: Implement an analog opto-isolator or a dedicated differential amplifier (like the TI INA128) to bridge the two ground domains safely.
By selecting the correct transducer physics for your environment and applying proper analog signal conditioning at the hardware level, your software calibration code can focus on precision rather than fighting noise.






