Friday, December 9, 2011

Circuit 13 - Flex Sensor

Purpose: To control a mini servo motor using a flex sensor potentiometer

Equipment: 
  • Arduino x 1 
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC - 13 Breadboard Sheet x 1
  • Flex Sensor x 1
  • Mini Servo x 1
  • 10k ohm resistor x 1
  • 3 pin header x 1
  • Wire x 8

References:

Program Details: 
In this circuit, another new piece of hardware is introduced, the flex sensor. The flex sensor uses carbon on a strip of plastic to sense resistance based on the bending of the strip. There is a higher level of resistance as the sensor is bent more; the range is from approximately 10k - 35k ohms. The arduino will sense the resistance provided by the flex sensor and translate it to a numerical value which will be used to control the position of the servo.

The programming sector of this exercise is quite simple. First the entire servo library is imported which allows us to use pre-made methods right away without the tedious coding. Then an object of type Servo is created along with a few variables. Then a connection between the serial monitor and arduino is created, allowing the user to see the resistance value. The resistance value is read from the flex sensor and is scaled as a numerical value. The numerical value is finally sent to the servo motor and the servo motor spins accordingly. 

Time to complete: 10 mins to assemble
                                5 mins to code

Results: The circuit and program both ran perfectly the first time although it was hard to place the servo onto the breadboard/pin header. 

Photo: 

Tips: 
  • Make sure the servo wires match up with ground, power and signal properly (black with ground, red with power, and white with signal)
  • Make sure the circuit involved with the flex sensor is proper (connect to ANALOG PIN 0, add resistor)

Further Work: I will calibrate the sensor's range to make it turn better.

Program Modifications:
The program is very similar to the one in the link below:

Program:
// imports the entire Servo library
#include <Servo.h> 

// an object "myservo" created of type Servo
Servo myservo;  

int potpin = 0;  // analog pin used to connect the potentiometer
int val;             // reads value from the potentiometer

void setup() 
  Serial.begin(9600);  // creates a connection between the serial monitor and arduino
  myservo.attach(9);  //  the servo object is controlled through pin 9 

void loop() 
  val = analogRead(potpin);              // the value of the potentiometer is read and stored 
  Serial.println(val);                           // the value of the potentiometer is displayed in the serial monitor
  val = map(val, 50, 300, 0, 179);     // the scale is changed to be compatible with the servo 
  myservo.write(val);                        // sets the servo position according to the scaled value 
  delay(15);                           

No comments:

Post a Comment