Using the Flex Sensor

Theo Chemel

What is the flex sensor?

From SparkFun:

One side of the sensor is printed with a polymer ink that has conductive particles embedded in it. When the sensor is straight, the particles give the ink a resistance of about 30k Ohms. When the sensor is bent away from the ink, the conductive particles move further apart, increasing this resistance (to about 50k-70K Ohms when the sensor is bent to 90°, as in the diagram below). When the sensor straightens out again, the resistance returns to the original value. By measuring the resistance, you can determine how much the sensor is being bent.


The flex sensor should only be bent in the direction with the ink facing outwards.


Connecting the Flex Sensor to the Arduino

Turn the potentiometer all the way to the left to start. You may need to adjust it and the values in the code to obtain an accurate reading. And, as always, DOUBLE CHECK YOUR WIRING!


Programming with the Flex Sensor

Upload the following code to your Arduino and open the Serial Monitor (Tools > Serial Monitor in the menu bar). If everything is working properly, you should see a stream of values printed out. Adjust the resistance parameters at the top of the file until you obtain accurate bend readings. It will take some trial and error!


/******************************************************************************
Flex_Sensor_Example.ino
Example sketch for SparkFun's flex sensors
  (https://www.sparkfun.com/products/10264)
Jim Lindblom @ SparkFun Electronics
April 28, 2016


Create a voltage divider circuit combining a flex sensor with a 47k resistor.
- The resistor should connect from A0 to GND.
- The flex sensor should connect from A0 to 3.3V
As the resistance of the flex sensor increases (meaning it's being bent), the
voltage at A0 should decrease.


Development environment specifics:
Arduino 1.6.7
******************************************************************************/
const int FLEX_PIN = A0; // Pin connected to voltage divider output


// Measure the voltage at 5V and the actual resistance of your
// 47k resistor, and enter them below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 200000.0;

// Upload the code, then try to adjust these values to more
// accurately calculate bend degree.
const float STRAIGHT_RESISTANCE = 589960.0; // resistance when straight
const float BEND_RESISTANCE = 1600000.0; // resistance at 90 deg


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


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


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


  delay(500);
}