Flex Sensor not working

Hello,

I have now purchased two SparkFun Flex sensors (2.2") and tried to get them to work. I get a value, but bending the sensor does not really do anything. I have posted a diagram of how I have it hooked up, and here’s my code (from here):

    // Flex sensor test program
// Mike Grusin, SFE, 2011
// This program is free, use it however you wish!

// HARDWARE:
// Make the following connections between the Arduino and the flex sensor
// Note that the flex sensor pins are interchangeable

// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V

// INSTRUCTIONS:
// Upload this sketch to your Arduino, then activate the Serial Monitor
// (set the Serial Monitor to 9600 baud)

void setup()
{
  // initialize serial communications
  Serial.begin(9600); 
}

void loop()
{
  int sensor, degrees;
  
  // read the voltage from the voltage divider (sensor plus resistor)
  sensor = analogRead(5);
  
  // convert the voltage reading to inches
  // the first two numbers are the sensor values for straight (768) and bent (853)
  // the second two numbers are the degree readings we'll map that to (0 to 90 degrees)
  degrees = map(sensor, 768, 853, 0, 90);
  // note that the above numbers are ideal, your sensor's values will vary
  // to improve the accuracy, run the program, note your sensor's analog values
  // when it's straight and bent, and insert those values into the above function.
  
  // print out the result
  Serial.print("analog input: ");
  Serial.print(sensor,DEC);
  Serial.print("   degrees: ");
  Serial.println(degrees,DEC);
  
  // pause before taking the next reading
  delay(100);                     
}

The serial monitor shows this, repeating (despite my bending the sensor at the “zebra stripe” part):

analog input: 1023 degrees: 270
analog input: 1023 degrees: 270
analog input: 1023 degrees: 270

Thanks for any help or ideas! If you would like any more info, just let me know!

Connect the analog input to the junction of the resistor and the flex sensor. Move the input wire 4 holes to the right.

Well, you have connected one of the flex senson outputs to VCC, thats why it shows the 1023 value all the time (it's constantly reading 5V on ADC port). Connect the ADC wire as presented in here it should make it work.

congrim: Well, you have connected one of the flex senson outputs to VCC, thats why it shows the 1023 value all the time (it's constantly reading 5V on ADC port). Connect the ADC wire as presented in here it should make it work.

Thanks for the help - I did that and now it works, but barely. Here's the serial output:

analog input: 1015 degrees: 261 analog input: 1015 degrees: 261 analog input: 1016 degrees: 262 analog input: 1018 degrees: 264 analog input: 1020 degrees: 266 analog input: 1019 degrees: 265 analog input: 1020 degrees: 266 analog input: 1020 degrees: 266 analog input: 1019 degrees: 265 analog input: 1019 degrees: 265 analog input: 1016 degrees: 262 analog input: 1016 degrees: 262 analog input: 1014 degrees: 260 analog input: 1014 degrees: 260

What resitor did you use for that? Can you check the resistance of the flex sensor (maximum and minimum - not touched and bended)?

The used resistor should be more or less similiar value to bended sensor resistance. F.ex. my press sensor gives around 15-16k when pressed, so I used 20k resistor.

edit: One more thing - to get 0 on ADC you would need an infinite resistance resistor (in practice really big one, couple megaohms I think), and to get 1023 you'd need 0 resistance resistor (in practice no resistor). When you change the resistor, check the minimum and maximum values that serial monitor is showing you - you should use them as min and max in counting the degree of bend.

congrim: What resitor did you use for that? Can you check the resistance of the flex sensor (maximum and minimum - not touched and bended)?

The used resistor should be more or less similiar value to bended sensor resistance. F.ex. my press sensor gives around 15-16k when pressed, so I used 20k resistor.

edit: One more thing - to get 0 on ADC you would need an infinite resistance resistor (in practice really big one, couple megaohms I think), and to get 1023 you'd need 0 resistance resistor (in practice no resistor). When you change the resistor, check the minimum and maximum values that serial monitor is showing you - you should use them as min and max in counting the degree of bend.

The first I tried was 220 Ohm. I tried a 10k Ohm resistor, because that's what Sparkfun (the actual manufacturer of the sensor) says, but still no real variation in Serial monitor. I've tried some others too, but no real variation in the output...any ideas why it's not working for me? Is it just the resistor? The code's fine, yeah?

The code looks fine. Only thing I don't know why is not there is pinMode(A0,INPUT); in void setup function. However the programme seems to work...

The datasheet tells us, that the resistance is changing from 45k (bended) to 125k (still). Try using 47k resistor.

If you have a multimeter, try checking if the bend sensor really has the values written in the datasheet.

You can also check if ADC is working fine. First check with GND and VCC (Arduino should read 0 and 1023) - just plug the wire from A5 to Arduino's GND and VCC. Later change the flex sensor with potentiometer (one side pin to GND, second side to VCC, middle one to A5) - you should be able to manually change the values of sensor variable values.