A flex sensor is a flexible, variable resistor (potentiometer) that increases its electrical resistance as it is bent. Unlike digital switches that output a simple HIGH or LOW, a flex sensor provides a continuous analog voltage when paired with a fixed resistor in a voltage divider circuit. By measuring this voltage with a microcontroller's Analog-to-Digital Converter (ADC), you can calculate the exact bend angle in degrees. In this guide, we will break down the physics, the exact wiring for an ESP32, and the raw-to-unit math required to get reliable degree readings on your workbench.
The Sensing Principle: How Carbon Polymer Tracks Resistance
At its core, a standard flex sensor (like the popular 2.2-inch models from Spectra Symbol or Adafruit) consists of a carbon-impregnated polymer substrate sandwiched between two conductive traces. When the sensor is perfectly flat, the carbon particles are densely packed, providing a low-resistance path for electrons—typically around 10,000 ohms (10kΩ).
As you bend the sensor, the polymer substrate stretches on the outside of the curve. This physical stretching increases the microscopic distance between the carbon particles, forcing electrons to jump across wider gaps. This disruption in the conductive matrix increases the electrical resistance. At a 90-degree bend, the resistance typically climbs to between 30kΩ and 40kΩ. Because this change is largely proportional to the bend angle, we can use basic Ohm's Law to map resistance directly to physical deflection.
Hardware Specs and ESP32 Wiring Table
You cannot wire a flex sensor directly to a microcontroller pin and expect a reading. Because it is a variable resistor, it requires a voltage divider circuit to convert the changing resistance into a changing voltage that the ESP32's ADC can read. We place a fixed pull-down resistor between the analog pin and ground, and the flex sensor between the 3.3V supply and the analog pin.
The internal 12-bit SAR ADC on the ESP32-WROOM-32 is notoriously non-linear above 2.6V. If your voltage divider outputs 3.0V, your readings will compress and become inaccurate. To keep the signal in the ESP32's linear sweet spot (0.1V to 2.5V), we use a 22kΩ fixed resistor instead of the standard 10kΩ often suggested in beginner tutorials.
| Sensor Pin / Node | ESP32 DevKit V1 Pin | Supply / Component Range | Notes |
|---|---|---|---|
| VCC (Flex Sensor Top) | 3V3 | 3.3V DC (Max 5V) | Do not use 5V if sharing rails with sensitive 3.3V logic. |
| Signal (Junction Node) | GPIO 34 (or 32-39) | Analog Input (0 - 3.3V) | Input-only pins. Do not use GPIO 25-27 (DAC conflict). |
| Fixed Resistor | Between Signal & GND | 22kΩ (1/4W, 1% tolerance) | Keeps max voltage under 2.5V to avoid ESP32 ADC roll-off. |
| GND (Flex Sensor Bottom) | GND | 0V Reference | Ensure a solid breadboard connection; loose grounds cause noise. |
The Math: Converting Raw ADC Readings to Bend Angles
Getting a raw ADC value (0-4095) is useless for physical applications. We need to convert that raw integer into a voltage, then into a resistance, and finally into a bend angle in degrees. Here is the exact mathematical pipeline.
Step 1: Raw ADC to Voltage
The ESP32's 12-bit ADC maps 0V to 0, and 3.3V to 4095.
V_out = (ADC_raw / 4095.0) * 3.3
Step 2: Voltage to Flex Resistance
Using the voltage divider formula where the flex sensor is the top resistor ($R_{flex}$) and the fixed resistor is the bottom ($R_{fixed} = 22,000\Omega$):
R_flex = R_fixed * ((3.3 / V_out) - 1)
Step 3: Resistance to Degrees (Linear Interpolation)
According to the Adafruit 2.2-inch Flex Sensor datasheet, the baseline flat resistance ($R_{flat}$) is ~10,000Ω (0°), and the 90-degree bent resistance ($R_{bent}$) is ~35,000Ω. We calculate the slope (degrees per ohm) and apply it.
Slope = 90.0 / (35000 - 10000) = 0.0036 degrees/ΩAngle = (R_flex - 10000) * 0.0036
R_flat and R_bent constants in your code.
Calibration, Interference, and Real-World Gotchas
When you wire this up on a breadboard, your serial monitor will likely show the angle jumping between 12° and 18° even when the sensor is sitting perfectly still. This is not a broken sensor; it is environmental interference and material physics.
1. Capacitive Coupling and Mains Hum:
The junction between the flex sensor and the 22kΩ resistor is a high-impedance node. High-impedance analog traces act like antennas, picking up 50Hz/60Hz electromagnetic interference (EMI) from nearby AC mains wiring, switching power supplies, and even fluorescent lights. The Fix: Solder a 100nF (0.1µF) ceramic capacitor directly between the ADC signal pin and GND. This creates a low-pass hardware filter that shorts high-frequency noise to ground before it hits the ESP32's ADC sample-and-hold circuit.
2. Mechanical Hysteresis and Creep:
Flex sensors suffer from polymer creep. If you bend the sensor to 90° and hold it there for 60 seconds, the resistance will slowly drift upward. When you release it, it will not immediately return to 10kΩ; it will take several seconds to relax back to baseline. The Fix: Never use flex sensors for applications requiring absolute, repeatable positional tracking over long hold times. They are best suited for dynamic, transient gesture recognition (like sign-language translation gloves) rather than static joint-angle measurement. For static joints, use a rotary encoder or an IMU (like the BNO055).
3. Temperature Drift:
The carbon polymer matrix is temperature-sensitive. A 10°C increase in ambient temperature can drop the baseline resistance by 5% to 8%. If your wearable project moves from an air-conditioned lab to an outdoor summer environment, your zero-degree baseline will shift. Implement a software 'zeroing' button in your UI that recalibrates the R_flat variable upon startup.
Frequently Asked Questions
What is a flex sensor used for in wearable robotics?
In wearable robotics and animatronics, flex sensors are primarily used for gross gesture recognition rather than precision joint tracking. They excel in applications like smart gloves for virtual reality (VR) finger tracking, physical therapy rehabilitation monitors that detect hand-closure percentages, and puppeteering rigs where the absolute angle matters less than the relative change in deflection. Because they are thin, lightweight, and require no mechanical linkages, they integrate easily into textile garments without altering the drape or weight of the fabric.
What is the difference between a flex sensor and a strain gauge?
While both measure physical deformation, their operating principles and use cases are entirely different. A flex sensor relies on a thick carbon-polymer layer that changes resistance based on macroscopic bending (measuring angles from 0° to 90°). A strain gauge uses a microscopic metallic foil zigzag pattern bonded to a rigid surface; it measures micro-strains (fractional millimeter stretching) caused by tension, compression, or torsion. Use flex sensors for hinges, fingers, and flaps. Use strain gauges for load cells, structural health monitoring, and torque measurement.
Why is my flex sensor output voltage drifting over time?
Voltage drift in a flex sensor circuit is almost always caused by material hysteresis (polymer creep) or thermal drift. The carbon particles inside the sensor take time to settle back into their original physical arrangement after being stretched. Additionally, if the sensor is mounted near a heat source (like a microcontroller's voltage regulator or a human wrist), the temperature coefficient of the carbon track will shift the baseline resistance. To mitigate this, always implement a software low-pass filter (like an exponential moving average) in your microcontroller code to smooth out micro-drifts, and provide a physical 'tare' or 'calibrate' button to reset the baseline resistance value dynamically.






