Arduino code for controlling 270 degree Servo motor

Hello everyone,

We are working on a project that is Exoskeleton hand using EMG sensors, so the Exoskeleton hand has only one servo which is connected to a shaft and that shaft transfer the torque to the gear onto the shaft for each finger in order to fully close and open the fingers simultaneously with the rotation of the servo once the values read from the EMG sensor values goes above or below threshold.

Now the problem we are facing is that we want to precisely control the rotation of the servo, that the initial position of the servo motor needs to be 135 degrees, and it does a full rotation in clockwise direction to close the hand and then goes back to the initial position.

The code which we have developed is that we made modifications to the SWEEP test code to fit our application but the issue is that servo motor moves way to back and rotates more than the specified angles and it doesn't rotate completely or you can say apply enough force to close the hand.

Here is the code that we are using any help will be appreciated

#include <Servo.h>

Servo myservo;  // create servo object to control a servo


int pos = 135;    // variable to store the servo position
int sensorPin = A0;             // define the sensor pin 'Analog Pin 3'
#define sensorThreshold 220     // define the threshold of the value from the sensor

void setup() {
  Serial.begin(115200);           // starting serial communication to define threshold by reading values from sensor
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}

void loop() {
int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);  
if(sensorValue< sensorThreshold){
  for (pos = 135; pos <= 180; pos += 1) { // goes from 135 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10); 
    }
               // waits 15ms for the servo to reach the position
  }
  else if(sensorValue >sensorThreshold)
    for (pos =180; pos>= 135; pos -= 1) { // goes from 180 degrees to 135 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  } 

If I were you, for this kind of project i would rather select a servo with feedback. An internet search would give many sites where you can source these or if your daring hack the servo ur are currently using to add the feedback like shown here! :wink:

the servo code you shared would set the servo to approximately the intended position as there is not way for it to store/recall its current position. you can always fine tune the position values but that can very easily go off the mark after some time.

hope that helps....

how can a servo do a full rotation?
as you suggested, i thought many servos can go beyond 180, to 270, which means the write values from 0-180 need to be scaled to do so.

for testing, you might be better off setting the servo (write()) to values read in thru the serial monitor

there are also servos with higher torque (goBilda)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.