Interfacing Flex Sensor with Arduino

A flex sensor or bend sensor is a low-cost and easy-to-use sensor specifically designed to measure the amount of deflection or bending.

It became popular in the 90s due to its use in the Nintendo Power Glove as a gaming interface. Since then people have been using it as a goniometer to determine joint movement, a door sensor, a bumper switch for wall detection or a pressure sensor on robotic grippers.

Flex Sensor Overview

A flex sensor is basically a variable resistor that varies in resistance upon bending. Since the resistance is directly proportional to the amount of bending, it is often called a Flexible Potentiometer.

Flex sensors are generally available in two sizes: one is 2.2″ (5.588cm) long and another is 4.5″ (11.43cm) long.

types of flex sensors

Construction

A flex sensor consists of a phenolic resin substrate with conductive ink deposited. A segmented conductor is placed on top to form a flexible potentiometer in which resistance changes upon deflection.

flex sensor construction

Directions to Use

Flex sensors are designed to flex in only one direction – away from ink (as shown in the figure). Bending the sensor in another direction may damage it.

directions to use flex sensor

Also take care not to bend the sensor close to the base, because the bottom of the sensor (where the pins are crimped on) is very fragile and can break when bent over.

How Flex Sensor Works?

The conductive ink printed on the sensor acts as a resistor. When the sensor is straight, this resistance is about 25k.

flex sensor working

When the sensor is bent, conductive layer is stretched, resulting in reduced cross section (imagine stretching a rubber band). This reduced cross section results in an increased resistance. At 90° angle, this resistance is about 100KΩ.

When the sensor is straightened again, the resistance returns to its original value. By measuring the resistance, you can determine how much the sensor is bent.

Reading a Flex Sensor

The easiest way to read the flex sensor is to connect it with a fixed value resistor (usually 47kΩ) to create a voltage divider. To do this you connect one end of the sensor to Power and the other to a pull-down resistor. Then the point between the fixed value pull-down resistor and the flex sensor is connected to the ADC input of an Arduino.

This way you can create a variable voltage output, which can be read by a Arduino’s ADC input.

flex voltage divider

Note that the output voltage you measure is the voltage drop across the pull-down resistor, not across the flex sensor.

The output of the voltage divider configuration is described by the equation:

flex1

In the shown configuration, the output voltage decreases with increasing bend radius.

For example, with 5V supply and 47K pull-down resistor, when the sensor is flat (0°), the resistance is relatively low (around 25kΩ). This results in the following output voltage:

flex2

When flexed all the way (90°), the resistance rises to 100KΩ. This results in the following output voltage:

flex3

Wiring Flex Sensor to Arduino UNO

It is quite easy to connect a flex sensor to an arduino.

You need to connect a 47kΩ pull-down resistor in series with the flex sensor to create a voltage divider circuit. Then the point between the pull-down resistor and the FSR is connected to the A0 ADC input of an Arduino.

wiring flex sensor to arduino

Note that flex sensors are basically resistors. This means that you can connect them either way and they will work fine.

Arduino Code

Here is a simple sketch that reads the sensor data from the ADC pin of the Arduino and prints the output on the serial monitor. For most projects, this is pretty much all that’s needed.

const int flexPin = A0;			// Pin connected to voltage divider output

// Change these constants according to your project's design
const float VCC = 5;			// voltage at Ardunio 5V line
const float R_DIV = 47000.0;	// resistor used to create a voltage divider
const float flatResistance = 25000.0;	// resistance when flat
const float bendResistance = 100000.0;	// resistance at 90 deg

void setup() {
	Serial.begin(9600);
	pinMode(flexPin, INPUT);
}

void loop() {
	// Read the ADC, and calculate voltage and resistance from it
	int ADCflex = analogRead(flexPin);
	float Vflex = ADCflex * VCC / 1023.0;
	float Rflex = R_DIV * (VCC / Vflex - 1.0);
	Serial.println("Resistance: " + String(Rflex) + " ohms");

	// Use the calculated resistance to estimate the sensor's bend angle:
	float angle = map(Rflex, flatResistance, bendResistance, 0, 90.0);
	Serial.println("Bend: " + String(angle) + " degrees");
	Serial.println();

	delay(500);
}

If everything is fine, you should see a change in resistance and estimated angle calculation, as you bend the flex sensor.

flex sensor analog output

Code Explanation:

The sketch begins with the declaration of the Arduino pin to which FSR and 47K pulldown are connected.

const int flexPin = A0;	

Next, we define couple of constants: VCC is your system’s voltage, R_DIV is the resistor used to create a voltage divider, flatResistance and bendResistance are your flex sensor’s resistance when it’s flat and bent at 90°. Make sure that you set these constants accurately.

const float VCC = 5;
const float R_DIV = 47000.0;
const float flatResistance = 25000.0;
const float bendResistance = 100000.0;

In setup function of code we initialize the serial communication with the PC and set the flexPin as INPUT.

void setup() {
	Serial.begin(9600);
	pinMode(flexPin, INPUT);
}

In loop function, we first take the ADC reading.

int ADCflex = analogRead(flexPin);

When the Arduino converts this analog voltage into digital, it actually converts it to a 10-bit number of range 0 to 1023. So to calculate the actual output voltage, we use below formula:

float Vflex = ADCflex * VCC / 1023.0;

Next, we calculate the resistance of the flex sensor using the formula derived from the voltage divider formula and display it on the serial monitor.

float Rflex = R_DIV * (VCC / Vflex - 1.0);
Serial.println("Resistance: " + String(Rflex) + " ohms");

Finally, we use the calculated resistance to estimate the sensor’s bend angle. To do this we use the IDE’s built-in map() function.

The map() function maps and converts the resistance of the sensor to the sensor’s bend angle. So, when we call map(Rflex, flatResistance, bendResistance, 0, 90.0), value of flatResistance would get mapped to 0°, a value of bendResistance to 90° and values in-between to values in-between.

float angle = map(Rflex, flatResistance, bendResistance, 0, 90.0);
Serial.println("Bend: " + String(angle) + " degrees");
Serial.println();