To interface standard 100mm linear position sensors like the Bourns PTB60 slide potentiometer or the Allegro A1324 Hall-effect IC with an ESP32, wire the sensor VCC to 3.3V, GND to GND, and the analog wiper/output to an ADC1 pin like GPIO 34. The output is a continuous analog voltage (typically 0.5V to 3.0V), which the ESP32’s 12-bit ADC reads as a raw integer (0-4095) that you must scale to millimeters using a two-point calibration.

The Sensing Principle: Resistive vs. Magnetic Linear Position Sensors

Resistive linear position sensors (like the Bourns PTB60 or TT Electronics P160 slide potentiometers) work as mechanical voltage dividers. A physical wiper slides along a resistive carbon or cermet track, outputting a variable analog voltage strictly proportional to physical displacement. They offer absolute position tracking without needing a homing sequence, making them ideal for low-cost DIY actuator feedback, audio faders, and pedal mechanisms, though they are subject to mechanical wear over hundreds of thousands of cycles.

Magnetic linear position sensors (like the Allegro A1324 or TI DRV5055) rely on the Hall effect to track movement. As a magnet moves linearly past the sensor IC, the changing magnetic flux density alters the output voltage. These provide non-contact, frictionless operation with infinite mechanical life, completely immune to the carbon track wear and dust ingress that plague resistive pots in dirty shop environments. Crucially, both types output a continuous analog voltage, not a digital pulse train or a 4-20mA current loop, meaning they rely entirely on the microcontroller's analog-to-digital converter (ADC) for resolution.

Wiring and Pinout: Supply Ranges and ESP32 Connections

When wiring analog sensors to an ESP32, you must use the ADC1 pins (GPIO 32, 33, 34, 35, 36, 39). The ADC2 pins share hardware with the WiFi radio and will return garbage data if WiFi is active. Below is the reference table for common DIY linear position sensors.

Sensor Model Type Supply Range (VCC) Output Type ESP32 Pin Connection
Bourns PTB60 Resistive Slide Pot 3.3V to 5.0V Analog Voltage (0V - VCC) Wiper to GPIO 34
Allegro A1324 Linear Hall-Effect 4.5V to 5.5V Analog Ratiometric (0.5V - 4.5V) Requires voltage divider to GPIO 35
TI DRV5055 Linear Hall-Effect 2.5V to 5.5V Analog Ratiometric (0.2V - 1.8V) Direct to GPIO 36 (Set VCC to 3.3V)

Standard Wiring Procedure for a 3.3V Slide Potentiometer:

  1. De-energize the breadboard. Connect the ESP32 3V3 pin to the left terminal of the slide potentiometer.
  2. Connect the ESP32 GND pin to the right terminal of the slide potentiometer.
  3. Connect the center wiper terminal to GPIO 34 on the ESP32.
  4. Solder or breadboard a 100nF ceramic capacitor directly between GPIO 34 and GND. This forms a low-pass filter to strip high-frequency EMI before the ADC samples the pin.
  5. Power on and verify the voltage at the wiper with a multimeter. It should sweep smoothly from ~0.05V to ~3.25V as you move the slider.

The Math: Converting Raw ADC Reads to Millimeters

The ESP32 features a 12-bit SAR ADC, meaning it maps the 0V–3.3V input range to integer values between 0 and 4095. To convert this raw reading into a physical unit (millimeters), you must first calculate the voltage, then map that voltage to your sensor's physical travel distance.

Assuming a 100mm slide potentiometer powered by a stable 3.3V reference, the math looks like this:

// 1. Read the raw 12-bit ADC value
int rawAdc = analogRead(34); // Range: 0 to 4095

// 2. Convert raw ADC to Voltage
float vRef = 3.3; // Measure your actual 3V3 pin with a DMM for precision
float voltage = (rawAdc / 4095.0) * vRef;

// 3. Map Voltage to Physical Position (Millimeters)
float vMin = 0.1;  // Voltage at physical 0mm (accounts for dead zones)
float vMax = 3.2;  // Voltage at physical 100mm
float travelLength = 100.0; // Total travel in mm

float position_mm = ((voltage - vMin) / (vMax - vMin)) * travelLength;

// Clamp the value to prevent negative numbers or over-travel errors
if (position_mm < 0) position_mm = 0;
if (position_mm > 100) position_mm = 100;
Bench Tip: Never assume your ESP32's 3V3 pin is exactly 3.300V. Cheap voltage regulators on clone DevKits often output 3.24V or 3.38V. Because ratiometric sensors scale with VCC, any fluctuation in your supply voltage will directly corrupt your millimeter reading unless you measure the actual VCC and update the vRef variable in your code, or use the ESP32's internal eFuse calibration values via analogReadMilliVolts().

Calibration, Scaling, and Beating EMI Interference

Calibration and Scaling: Off-the-shelf linear position sensors rarely output exactly 0.0V at zero travel and exactly 3.3V at maximum travel. Mechanical end-stops and wiper resistance create "dead zones." You must perform a two-point calibration. Physically lock the sensor at 0mm, record the raw ADC average, then lock it at 100mm and record the second average. Use these two empirical data points as your vMin and vMax in the math above. For high-precision requirements (sub-millimeter), the native ESP32 ADC is insufficient due to severe non-linearity at the voltage rails. In those cases, bypass the internal ADC and wire the sensor to an external 16-bit I2C ADC like the TI ADS1115.

Common Interference Sources: Analog voltage signals are high-impedance and act as antennas. The most common interference sources in a maker space are stepper motor drivers (like the A4988 or TMC2209), variable frequency drives (VFDs), and switching power supplies. These inject high-frequency noise into your sensor wiring, causing the ESP32 ADC reading to jump erratically by ±50 points.

The Fix: Always use twisted-pair wire for the analog signal and ground. Keep sensor wires physically separated from stepper motor phase wires. The 100nF capacitor mentioned in the wiring steps creates a hardware low-pass filter. Combined with a software moving-average filter (averaging 16 to 32 consecutive ADC reads), you can eliminate 99% of EMI-induced jitter without introducing unacceptable latency to your control loop.

FAQ: Linear Position Sensor Troubleshooting

Why is my linear position sensor reading jumping around on the ESP32?

This is almost always caused by electromagnetic interference (EMI) or a floating ground. Stepper motors and relays generate massive voltage spikes that couple into high-impedance analog traces. First, verify that the sensor ground shares a common, star-grounded node with the ESP32 GND. Second, ensure you have a 100nF ceramic capacitor placed as close to the ESP32 GPIO pin as possible. Finally, implement a software exponential moving average (EMA) filter in your code to smooth out remaining high-frequency noise spikes.

Can I wire multiple analog linear position sensors to the same ESP32 ADC channel?

Not directly. You cannot simply wire two analog outputs together, as they will short-circuit and fight each other, potentially damaging the sensor wipers or the ESP32 GPIO. If you are out of ADC pins, you must use an analog multiplexer IC like the CD74HC4067 (16-channel) or CD4051 (8-channel). The multiplexer acts as a digital switch, allowing the ESP32 to select which sensor's voltage is routed to the single ADC pin at any given millisecond.

What is the difference between linear position sensors and rotary encoders for tracking distance?

Linear position sensors (pots and Hall ICs) provide absolute position. When the ESP32 boots up, it immediately knows exactly where the actuator is physically located. Rotary encoders (incremental) provide relative movement; they output digital pulses as a shaft turns, but they have no memory of their position when power is lost. If you use a rotary encoder on a linear actuator, you must drive the actuator to a physical limit switch to "home" it every time the system reboots. Choose linear position sensors for applications where homing is mechanically impossible or unsafe.

How do I protect a 5V linear position sensor when connecting to a 3.3V ESP32?

If your sensor requires a 5V supply (like the Allegro A1324) and outputs up to 4.5V, feeding that directly into an ESP32 GPIO will fry the microcontroller's internal clamping diodes. You must use a resistive voltage divider to scale the output down. Wire a 10kΩ resistor in series with the sensor output, and a 20kΩ resistor from the ESP32 GPIO pin to ground. This divides the voltage by roughly 0.66, safely mapping a 4.5V maximum signal down to ~2.97V, which is well within the ESP32's safe ADC range. Remember to update your math formulas to account for this voltage division ratio.