Servo not responding to Flex Sensor

Please Help! I connected Servo Motor & Flex Sensor to Arduino Uno R3 but Servo is not moving as per the Flex Sensor. It is moving on in its own in steps & then after 180 degree rotation keep trying to rotate but gets locked. When it is trying to move, the USB Connection & disconnection sound from computer comes repeatedly. I tried supplying external 5V supply to Servo from Adaptor but it is also not helping. The Servo Number is SM-S3317S by SpringRC. It is a Medium Servo.

The code is:

/*
SparkFun Inventor's Kit
Example sketch 09

FLEX SENSOR

  Use the "flex sensor" to change the position of a servo
  
  In the previous sketch, we learned how to command a servo to
  mode to different positions. In this sketch, we'll introduce
  a new sensor, and use it to control the servo.
  
  A flex sensor is a plastic strip with a conductive coating.
  When the strip is straight, the coating will be a certain
  resistance. When the strip is bent, the particles in the coating
  get further apart, increasing the resistance. You can use this
  sensor to sense finger movement in gloves, door hinges, stuffed
  animals, etc. See http://www.sparkfun.com/tutorials/270 for
  more information.
  
Hardware connections:

  Flex sensor:

    The flex sensor is the plastic strip with black stripes.
    It senses bending away from the striped side.
    
    The flex sensor has two pins, and since it's a resistor,
    the pins are interchangable.
    
    Connect one of the pins to ANALOG IN pin 0 on the Arduino.
    Connect the same pin, through a 10K Ohm resistor (brown
    black orange) to GND.
    Connect the other pin to 5V.

  Servo:
  
    The servo has a cable attached to it with three wires.
    Because the cable ends in a socket, you can use jumper wires
    to connect between the Arduino and the servo. Just plug the
    jumper wires directly into the socket.
    
    Connect the RED wire (power) to 5 Volts (5V)
    Connect the WHITE wire (signal) to digital pin 9
    Connect the BLACK wire (ground) to ground (GND)
  
    Note that servos can use a lot of power, which can cause your
    Arduino to reset or behave erratically. If you're using large
    servos or many of them, it's best to provide them with their
    own separate 5V supply. See this Arduino Forum thread for info:
    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239464763

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Include the servo library to add servo-control functions:

#include <Servo.h> 

// Create a servo "object", called servo1. Each servo object
// controls one servo (you can have a maximum of 12):

Servo servo1;

// Define the analog input pin to measure flex sensor position:

const int flexpin = 0; 


void setup() 
{ 
  // Use the serial monitor window to help debug our sketch:
   
  Serial.begin(9600);
  
  // Enable control of a servo on pin 9:

  servo1.attach(9);
} 


void loop() 
{ 
  int flexposition;    // Input value from the analog pin.
  int servoposition;   // Output value to the servo.

  // Read the position of the flex sensor (0 to 1023):
  
  flexposition = analogRead(flexpin);

  // Because the voltage divider circuit only returns a portion
  // of the 0-1023 range of analogRead(), we'll map() that range
  // to the servo's range of 0 to 180 degrees. The flex sensors
  // we use are usually in the 600-900 range:
  
  servoposition = map(flexposition, 600, 900, 0, 180);
  servoposition = constrain(servoposition, 0, 180);

  // Now we'll command the servo to move to that position:

  servo1.write(servoposition);

  // Because every flex sensor has a slightly different resistance,
  // the 600-900 range may not exactly cover the flex sensor's
  // output. To help tune our program, we'll use the serial port to
  // print out our values to the serial monitor window:
  
  Serial.print("sensor: ");
  Serial.print(flexposition);
  Serial.print("  servo: ");
  Serial.println(servoposition);
  
  // Note that all of the above lines are "print" except for the
  // last line which is "println". This puts everything on the
  // same line, then sends a final carriage return to move to
  // the next line.

  // After you upload the sketch, turn on the serial monitor
  // (the magnifying-glass icon to the right of the icon bar).
  // You'll be able to see the sensor values. Bend the flex sensor
  // and note its minimum and maximum values. If you replace the
  // 600 and 900 in the map() function above, you'll exactly match
  // the flex sensor's range with the servo's range.
  
  delay(20);  // wait 20ms between servo updates
}

I Powered Arduino with 9V 1A Adaptor & gave power to Servo through it instead of powering Arduino by USB or powering Servo with a 5V Adaptor. And this time though the results are much better but still not upto satisfaction. This time Servo responds to Flex Sensor but vibrates a lot in any position. Any suggestions ?

Put a 0.1uF capacitor from the analogue input to ground.

When posting code use the # icon not the quote one next to it.

Hi Mike,
Have connected 0.1 microF Capacitor between Analog Input & GND. Things improved a bit but still Servo Vibrating at extreme positions.

Try printing out the value you send to the servo to see by how much it is changing.

If the capacitor does not help then you can try and take several measurements and use an averaged result.

Hi Mike,
I am very new to Arduino. Don’t know how to print the value I am sending to Servo. Can I see it in serial monitor? Can you elaborate. Also how to send the desired value to Servo if Capacitor doesn’t helps? Can you explain.

If you have a

Serial.begin(9600);

In the setup this turns on the serial port.

Then

Serial.println("Prints this message");

Serial.println(value); // prints the contents of a variable called value.

If you open the serial monitor you will see them appear.

Also how to send the desired value to Servo if Capacitor doesn't helps?

By averaging your reading from several analogReads

 value_to_use = (analogRead(pin) + analogRead(pin) + analogRead(pin) + analogRead(pin) ) / 4

This will take four readings and average them. There are many other ways to do this but this one is simple.

Hi Mike,
I am facing problem with Arduino board. The Power Regulator chip of Arduino Board is overheating when powered with 9V 1A Adaptor & Servo being fed through Arduino’s 5V & GND pins. I am running single Servo Without Flex Sensor. The Servo however is working perfect and moves as described (rotates 90 degree then 180 & finally 0). The code for single servo is given below. I am facing the same problem of how to power Servo when using it with Flex Sensor & while using it single.

/*
SparkFun Inventor's Kit
Example sketch 08

SINGLE SERVO

  Sweep a servo back and forth through its full range of motion.

  A "servo", short for servomotor, is a motor that includes 
  feedback circuitry that allows it to be commanded to move to
  specific positions. This one is very small, but larger servos
  are used extensively in robotics to control mechanical arms,
  hands, etc. You could use it to make a (tiny) robot arm,
  aircraft control surface, or anywhere something needs to be
  moved to specific positions.

Hardware connections:

  The servo has a cable attached to it with three wires.
  Because the cable ends in a socket, you can use jumper wires
  to connect between the Arduino and the servo. Just plug the
  jumper wires directly into the socket.
  
  Connect the RED wire (power) to 5 Volts (5V)
  Connect the WHITE wire (signal) to digital pin 9
  Connect the BLACK wire (ground) to ground (GND)

  Note that servos can use a lot of power, which can cause your
  Arduino to reset or behave erratically. If you're using large
  servos or many of them, it's best to provide them with their
  own separate 5V supply. See this Arduino Forum thread for info:
  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239464763

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// If we had to write a sketch to control a servo from scratch,
// it would be a lot of work. Fortunately, others have done the
// hard work for you. We're going to include a "library"
// that has the functions needed to drive servos.

// A library is an set of additional functions you can add to
// your sketch. Numerous libraries are available for many uses,
// see http://arduino.cc/en/Reference/Libraries for information
// on the standard libraries, and Google for others. When you're
// using a new part, chances are someone has written a library
// for it.

#include <Servo.h>  // servo library

// Once you "include" a library, you'll have access to those
// functions. You can find a list of the functions in the servo
// library at: http://arduino.cc/en/Reference/Servo
// Most libraries also have example sketches you can load from
// the "file/examples" menu.

// Now we'll create a servo "object", called myservo. You should
// create one of these for each servo you want to control. 
// You can control a maximum of twelve servos on the Uno 
// using this library. (Other servo libraries may let you
// control more). Note that this library disables PWM on
// pins 9 and 10!

Servo servo1;  // servo control object


void setup()
{
  // We'll now "attach" the servo1 object to digital pin 9.
  // If you want to control more than one servo, attach more
  // servo objects to the desired pins (must be digital).

  // Attach tells the Arduino to begin sending control signals
  // to the servo. Servos require a continuous stream of control
  // signals, even if you're not currently moving them.
  // While the servo is being controlled, it will hold its 
  // current position with some force. If you ever want to
  // release the servo (allowing it to be turned by hand),
  // you can call servo1.detach().

  servo1.attach(9);
}


void loop()
{
  int position;
  
  // To control a servo, you give it the angle you'd like it
  // to turn to. Servos cannot turn a full 360 degrees, but you
  // can tell it to move anywhere between 0 and 180 degrees.

  // Change position at full speed:

  servo1.write(90);    // Tell servo to go to 90 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(180);   // Tell servo to go to 180 degrees

  delay(1000);         // Pause to get it time to move

  servo1.write(0);     // Tell servo to go to 0 degrees

  delay(1000);         // Pause to get it time to move
  
  // Change position at a slower speed:

  // To slow down the servo's motion, we'll use a for() loop
  // to give it a bunch of intermediate positions, with 20ms
  // delays between them. You can change the step size to make 
  // the servo slow down or speed up. Note that the servo can't
  // move faster than its full speed, and you won't be able
  // to update it any faster than every 20ms.

  // Tell servo to go to 180 degrees, stepping by two degrees
 
  for(position = 0; position < 180; position += 2)
  {
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }

  // Tell servo to go to 0 degrees, stepping by one degree

  for(position = 180; position >= 0; position -= 1)
  {                                
    servo1.write(position);  // Move to next position
    delay(20);               // Short pause to allow it to move
  }
}

Hi Mike,

Then
Code:
Serial.println("Prints this message");

Serial.println(value); // prints the contents of a variable called value.

If you open the serial monitor you will see them appear.

Don't we need to declare the variable "Value" at beginning of program? Will this variable take the values sent to Servo automatically?

How to implement the Variable "Value_to_use" in the program? If possible please provide complete code alongwith the blocks under which they need to be implemented. I am very weak in coding (love hardware more) so please forgive me for silly questions, if any.

Don't we need to declare the variable "Value" at beginning of program?

That line was a generic way to print any variable, you substitute the name of the variable you want to use in place of "Value"

How to implement the Variable "Value_to_use"

Similarly the variable Value_to_use should be substituted by the name of the value you want to use.

The Power Regulator chip of Arduino Board is overheating when powered with 9V 1A Adaptor & Servo being fed through Arduino's 5V & GND pins.

In that case you need to supply a separate regulator or power supply for the servo.

& Servo being fed through Arduino’s 5V & GND pins.

Bad! Servos need external power supplies similar to the below.

Thanks Mike & Zoomkat,
Will try the implementations as per your recommendation & will let you know.

Thanks Mike & ZoomKat. Issue finally resolved without alteration of code. Thanks for pointing me in the right direction. What I was doing wrong was that I am not connecting the GND pin of 5V 1A Adaptor to GND pin of Arduino Uno. Now both "Single Servo" & "Servo with Flex Sensor" are working perfectly.

Regards,
Tanmay

hello everyone…
I am also working on the same project which wireless controlled robotic claw… I was able to move the motors in response to the flex sensors (without wireless technique), but when i started to do it wirelesly by using RF module I am not receiving any response from the flex sensors to the servo motors. My question is do I need to write another code on the receiving side so that the motors move…??? the code i used when iam doing without wirelessly is…

#include <Servo.h>

Servo myservo1;
Servo myservo2; // create servo object to control a servo
Servo myservo3;
Servo myservo4;

int potpin1 = A0; // analog pin used to connect flex sensor output
int potpin2 = A1;
int potpin3 = A2;
int potpin4 = A3;

int val1; // variable to read the value from the analog pin
int val2;
int val3;
int val4;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600); // begin serial commmunication with Mega at 9600 baud rate

myservo1.attach(P2_5);
myservo2.attach(P2_4);
myservo3.attach(P1_5);
myservo4.attach(P1_4); // attach servo to pin
}

void loop()
{
// put your main code here, to run repeatedly:
{

val1 = analogRead(potpin1); // read the value of the flex sensor (analog value between 0 and 1023)
Serial.write(val1);
val1 = map(val1, 755, 1000, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val1); // set the servo position according to the scaled value

}

{
val2 = analogRead(potpin2);
Serial.write(val2);
val2 = map(val2, 700, 1000, 0, 180);
myservo2.write(val2);

}

{
val3 = analogRead(potpin3);
Serial.write(val3);
val3 = map(val3, 800, 1000, 0, 180);
myservo3.write(val3);

}

{
val4 = analogRead(potpin4);
Serial.write(val4);
val4 = map(val4, 790, 1000, 0, 180);
myservo4.write(val4);

}

}

in case if I need to write a code on the receiver side also… what are the modifications do i need to make the the above code, so that i can match both the codes…???

Hi,
You have your flex sensor on pin-0, this pin is also used for the serial comms to the PC! move it and try again. Not read all the post, so someone else might have seen this already, but this would account for the jittering.

Regards

Mel.

Cactusface:
You have your flex sensor on pin-0, this pin is also used for the serial comms to the PC!

It's on analog 0

Hi,
Really must get my eyes fixed!!

Regards

Mel.