# Arduino Tutorial #18 How to control servo motor with your finger by using flex sensor

Hi guys in this tutorial we will learn how to control servo motor with your fingers by using Flex sensor. So let’s get started.

For this you will need

1. Arduino,
2. Servo motor,
3. Flex sensor,
4. Jumper wires,
6. 56K ohms resistor.

Flex sensor used in many application such as robotics, gaming device, biometrics and fitness products. Flex sensor changes its resistance when it bends.

In normal flat position of sensor its resistance is 30k ohms and when it is bended its resistance increases up to 90k ohms resistance. This means that it won’t give 0 to 5V range. We will see it later in this video.  This range of resistance is depend upon flex sensor which you bought. Do connection as shown in Diagram.

##### Sketch:

```#include <Servo.h>

Servo myservo;

int flexpin = A0;
int val;

void setup() {
myservo.attach(9);
}

void loop() {
val = map(val, 370, 530, 0, 180);
myservo.write(val);
delay(10);
}```

We know that it won’t give us analog input range 0 to 1023. So we will determine the range of analog input. Go to file -> Example -> Basics -> AnalogReadSerial. Upload the sketch and go to tools and serial monitor. Now we can see the values when it is flat it is giving value around 370. And when it is bended it shows 530 value. Note this value as we need this value to map our servo angles. This value may be different for you if you are using other than 56k ohms resistor. You can calculate this range by voltage divider formula.

Vout=R2/(R1+R2)*Vin

voltage input = 5V
R1 = 56K ohms
R2 = 30K ohms (when flat)
90k ohms (when bended)
voltage output = 1.74V to 3.08V

(for flat position)

(for bended position)

I have taken 370 to 530 according to finger bending

You can see its values are almost same as what we got from serial monitor, it is showing 630 value for bending but we took 530 value for bending because when I bend my finger with sensor it won’t bend that much so I have taken values according to my fingers bending. Now let’s come to the programming part. #include will include outside library in our sketch. Create servo object to control a servo motor. Analog pin A0 is connected to flex sensor. Define variable for reading flex sensor value.  We have attached our servo to pin number 9. Analogread will read flex sensor value between 0 to 1023 and assign it to val. Now we want to map this value to servo angle. We know that it won’t range between 0 to 1023. We have previously checked it and it ranges between 370 to 530. So we will map it, for 370 it is 0 degree angle and for 530 it is 180 degree angle. Myservo.write will write flex sensor value and make angle rotation accordingly.  10 millisecond delay is just for servo to get there. You can see when I bend my finger it rotate anticlockwise and when I straight the finger it rotate clockwise.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2weYm4R (Servo Motor)

http://amzn.to/2wfeuDx  (Flex sensor)