23 May 2011

Arduino UNO: Flex Sensor and LEDs

Using the same LED matrix as before, and swapping the Photo Resistor for a Flex (or Bend) sensor, and a slight modification to the code, I can now have a light show that can be controlled by "Bending".

I used my finger to bend the sensor, but I could have attached it to a plant, a tree or anything else that bends. The bending changes the resistance, and therefore the INPUT value at analog pin 0.

The parts required:
  • Arduino UNO
  • 10 x Red LEDs
  • 9 x 330 Ohm resistors for the LEDs
  • 1 x 10K Ohm resistors for the flex sensor.
  • 1 x Flex sensor
  • Wires and Breadboard to connect it all together
Most of the components needed for this project can be sourced from the Sparkfun Inventor's Kit

The video:


The Sketch:







































The Arduino Code: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Flex Sensor and LEDs created by ScottC on 23rd May 2011
updated on 16/05/2012.

-----------------------------------------------------*/

//Flex Sensor Pin (flexPin)
//the analog pin the Flex Sensor is connected to
int flexPin = 0;  
                  
void setup() {
  for (int i=4; i<14; i++){
    pinMode(i, OUTPUT); //sets the led pins 4 to 13 to output
  }
}

void loop(){
 //Ensure to turn off ALL LEDs before continuing 
 for (int i=4; i<14; i++){
    digitalWrite(i, LOW); 
  }
 
 /* Read the flex Level 
  Adjust the value 130 to 275 to span 4 to 13
  The values 130 and 275 may need to be widened to suit 
  the minimum and maximum flex levels being read by the 
  Analog pin */
 int flexReading = map(analogRead(flexPin), 130, 275, 4, 13); 
         
// Make sure the value does not go beyond 4 or 13
 int LEDnum = constrain(flexReading, 4, 13); 
 
/*Call the blink function: this will turn the LED on for 10 milliseconds, and keep it
  off for only 1 millisecond. You can change the blink rate by changing these values,
  however, I want a quick response time when the flex sensor bends, hence the small 
  values. LEDnum determines which LED gets turned on.*/
 blink(LEDnum, 10,1);
}

// The blink function - used to turn the LEDs on and off
void blink(int LEDPin, int onTime, int offTime){
  // Turn the LED on                                         
 digitalWrite(LEDPin, HIGH);  
 
  // Delay so that you can see the LED go On.
 delay(onTime);
 
  // Turn the LED Off                                         
  digitalWrite(LEDPin, LOW);  
 
 // Increase this Delay if you want to see an actual blinking effect.
  delay(offTime);
}


The flex sensor pins/legs are a bit fragile, so be careful when poking it into the breadboard.


For more projects - have a look at the ArduinoBasics Project Page

32 comments:

  1. Thanks for this great tutorial. I would be helpfull if you put a list with the materials, specially with the resistors values for the non-electronic guys like me. I asume that there is a 1K resistor for the flex sensor and 220 for the leds?

    ReplyDelete
  2. I totally understand. A mere oversight on my part. Thankyou for your advice.

    ReplyDelete
  3. how can i preview flex sensor to a monitor via arduino?? any suggestion??

    ReplyDelete
    Replies
    1. Serial.print("Flex Value = ");
      Serial.println(analogRead(flexPin));

      This will read the flex sensor value attached to the flexPin, and send it to the computer. You can observe the values using the serial monitor.

      You may also be interested in one of my other blog posts:
      http://arduinobasics.blogspot.com/2012/07/arduino-basics-simple-arduino-serial.html

      Delete
  4. Great tutorial Scott! I started taking physical computing at grad school, and this came up pretty useful. I tried to run this on the void loop

    Serial.print("Flex Value = ");
    Serial.println(analogRead(flexPin));

    to see the values on the monitor and make it more accurate, for some reason it does not seem to work? Any suggestions?

    ReplyDelete
    Replies
    1. Just a couple of questions:
      Firstly - does your code compile?
      Secondly - are you using the serial monitor to view the printed values?

      Delete
  5. i have a 350 ohm flex sensor but i am unable to determine its sensitivity... It would be of great help to me if you could tell how you got the extreme values used in the map function i.e in your case 130 and 275.. thanks in advance :)

    ReplyDelete
    Replies
    1. I first used a function like
      Serial.println(analogRead(flexPin));
      This allows you to see what values your flex sensor produces.
      In my case, I think it went beyond the values of 130 and 275. Then I tightened up the range and used the constrain function. But the real learning comes from trial and error, and playing around with different set limits.

      Delete
  6. hi

    Can anyone tell me is it necessary to use map function?Can't we just use serial monitor thing?

    Regards

    ReplyDelete
    Replies
    1. This project doesn't use serial communication.
      You don't have to use map, but it is convenient.

      Delete
  7. what is 130 and 275?? is it voltage or resistance value?? if it is number then how you convert that? when we flexed the sensor its resistance will increase and it act like voltage divider then what is 130 and 275 value??

    ReplyDelete
    Replies
    1. "The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the Arduino called an analog-to-digital converter or ADC." as per this site:
      http://arduino.cc/en/Tutorial/AnalogInput

      However, you will find that the flex sensor will not produce values that range from 0 to 1023, it will range from a value above 0 and below 1023. You can use the serial monitor to see the range of your flex sensor by inserting this line into your code:

      Serial.println(analogRead(flexPin));

      As per one of my previous comments, I got a reading that was below 130 and above 275 depending on whether I flexed the sensor or kept it straight. But these values were not consistent at the tail end of the readings in that they varied with each time I flexed the sensor. So in order to get some consistency, I chose a range within my extreme values.

      To understand why I did this, first change these values to 0 and 1023, and run the sketch. Start narrowing these values (eg. 50 and 950... and then
      100-900 etc etc etc). You will find that the sketch is most effective when you chose a range that is about 50-100 units within the extreme values of your sensor.

      I hope this makes sense.

      Regards
      Scott

      Delete
    2. Hi, i am trying to start up my new project about flex sensor glove for hand
      flexion assessment and recording data for one day(by using memory card
      shield),
      so for that i assemble one flex sensor with arduino uno with the proper
      resistance and program to read the result and mapping it, that's working
      properly.
      but i need some help please for adding a knob or regulator (variable
      resistor) connected to a buzzer or led those makes flag or alarm when a
      given degree (set by variable resistor) of flex sensor reached. this regulator is assigned to user to select at which angle of flexion of his hand buzzer should be executed.
      thanks for support and help.

      Delete
    3. What exactly do you need help with? It sounds like you have it pretty well thought out. You just need to put it together. So where are you going wrong?

      Delete
  8. very good tutorial about how to interface a force sensor. Also, I add this tutorial in my article about force sensors http://www.intorobotics.com/force-sensors-reviewed-and-programming-tutorials/

    ReplyDelete
  9. Thanks for this! I used a modified version of your code with two flex sensors attached to the fingers of a glove. It allows me to control 8 EL wire channels on my 10' tall EL tree.

    ReplyDelete
    Replies
    1. No probs Caley. I would be interested to see your project. Leave a youtube link if you want.

      Delete
  10. Excellent work. I'd have added an adjustable buffer (via potentiometer) to control the sensitivity of the flex sensor, without touching the code it's a pretty neat control device (for those looking for a bit more functionality).

    ReplyDelete
  11. Hey Scott,

    Where did you place the line below in the code?

    Serial.println(analogRead(flexPin));

    ReplyDelete
    Replies
    1. I didn't put this line in the code. Not exactly sure what you mean. Please clarify.

      Delete
  12. Great job! I have a question though, is this applicable to other Arduino microcontroller? You use Arduino UNO, I have Arduino Duemilanove. Thanks!

    ReplyDelete
    Replies
    1. Yes should work. No library required.

      Delete
  13. In order for the serial monitor to display the flex sensors digital output value, you must initialize the baud rate under void setup.
    void setup() {
    Serial.begin(9600);
    }
    void loop() {
    Serial.println(analogRead(flexPin));
    }

    ReplyDelete
  14. Scott,
    Great tutorial. I'm trying to modify this code to just fade one led in and out based on how much the sensor is flexed but can't seem to figure out how to do this. How would the code have to be modified?

    ReplyDelete
    Replies
    1. int fadeValue = map(analogRead(flexPin), 130, 275, 0, 255);

      Delete
    2. Thanks Scott. Got it working.

      Delete
  15. guys what if i want use more than one sensor in same project ?

    ReplyDelete
    Replies
    1. There is only one of me.

      I have not used more than one sensor... and so I have not tried doing this myself, but perhaps you could try setting it up like this:
      http://makerzone.mathworks.com/wp-content/uploads/2014/07/arduinoSetup1.png

      Delete
  16. we can bend the sensor in both direction.how to identify in which direction it bend? is there any two way bend sensor??

    ReplyDelete

Feel free to leave a comment :