[tutorial] How to use a Flex Sensor

Introduction

flex sensor

In this "How to" tutorial you will learn how to use a flex sensor with Arduino uno.

A flex sensor uses carbon on a strip of plastic to act like a variable resistor. The resistance changes by flexing the component.

The sensor bends in one direction, the more it bends, the higher the resistance gets.

In this example we will use a flex sensor to fade an led.

So, let's get started!

What you will need

parts

For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • Flex Sensor
  • Led
  • 10KOhm & 220 Ohm resistors

The Circuit

fritzing

The connections are pretty easy, see the image above with the breadboard circuit schematic.

The Code

Here's the code, embedded using codebender!

A flex sensor has a range from about ~10K to ~35K, that means it won't give us a full 0-5 volt range (or 0-1023 analog value). Try to use the serial monitor below to find out what analog value you will take while you bending the sensor. It supposed to be between 700 to 900.

How it works:

Read analog value from flex sensor
-> value=analogRead(flexPin);

Map analog values 700-900 to pwm values 0-255
-> value = map(value, 700, 900, 0, 255);

Send pwm value to led
-> analogWrite(ledPin, value);

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino with this sketch. And that's it, you've programmed your Arduino board! Press connect button to start serial communication with your Arduino board.

You can make your own modifications to the code by clicking the "Edit" button. For example, you can change "700-900" values in map function.

Well done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use a flex sensor with Arduino.