Interfacing Arduino With Flex Sensor and Servo Motor
by Python EveryDay in Circuits > Robots
1301 Views, 7 Favorites, 0 Comments
Interfacing Arduino With Flex Sensor and Servo Motor
About Flex Sensor
Flex sensor is a flexible resistor, it has two pins. Whenever you bend it, its resistance changes. Its resistance is minimum when it is relaxed or straight. And resistance is found maximum when bended.
It follows resistivity
ρ = RA/l
where ρ = resistivity of a material
R = Resistance
A = Area of the material
l = Length of the material
Supplies
- Arduino UNO
- SparkFun Flex Sensor
- Resistor 163K Ω
- Jumper wires (generic)
Connect the Flex Sensor to a Multimeter
Measurement of Sensor Resistance
Connect the flex sensor to a multimeter, and set the multimeter on resistance.
Resistance When Straight
Note down its resistance when normal i.e. Straight ( bend angle = 0°)
Resistance When Bended Fully
Note down its resistance when fully bended ( bend angle = 180°)
Connecting Flex Sensor With Arduino
As the flex sensor is a variable resistor, we cannot connect it directly to arduino. We need a voltage divider circuit. The first resistor will be fixed one. The value of the resistor must be equal to the max resistance value of flex sensor. The fixed value resistor and flex sensor must be connected in series. The fixed value resistor must be connected to 5V whereas the flex sensor must be connected to GND. Their junction must be connected to pin A0 of the board.
Connecting Servo Motor
Servo motor has three pins Power, Ground and Signal power and ground needs to be connected to 5V and GND pins of board respectively. Signal can be connected to any of the digital pin.
Record the analog input value by bending the sensor from minimum to maximum. The servo motor need angle in degrees from 0° to 180°, but the sensor data can be any value from 0 to 1024. Use to the below code to convert the flex sensor value to proportional servo motor angle.
int const min_flex_val= 159 // analog input value for straight sensor
int const max_flex_val= 511 // analog input value for totally bended sensor float flex_value = analogRead(A0); float y= ((flex_value - min_flex_val)/(max_flex_val-min_flex_val)); int angle=(y*180); //angle value will be in the range 0 to 180
By doing above your motor should rotate proportionally to the bending of the sensor.
This can be used to control the hand of a robot, or mimic human hand gestures.
Full Code
#include <Servo.h><br><br><servo.h>Servo m;<br><br></servo.h>void setup() { pinMode(13, OUTPUT); pinMode(A0,INPUT); m.attach(7); Serial.begin(9600); } <br> void loop(){ float x=analogRead(A0); float y= ((x-159)/352); int angle=(y*180); Serial.println(angle); m.write(angle); }