Go Down

Topic: Connecting Arduino to Servos and Rotary Switch (Read 7608 times) previous topic - next topic

snowflax

I am working on this robot: instructables.com/id/Very-simple-clean-up-robot/ for a school project. But, the writer didn't specify how to connect the servos and rotary switches to the arduino and I'm afraid he's no longer active so I can't ask him. Can anyone tell me how to connect the servos and rotary switch? Also, because there's two types of servos (normal and continuous rotation), do I need to connect it differently?

Your help is much appreciated. Thank you

PaulS

Quote
I am working on this robot: instructables.com/id/Very-simple-clean-up-robot/ for a school project.

Your link doesn't work.

The art of getting good answers lies in asking good questions.

JimboZA

#2
Sep 15, 2014, 03:59 pm Last Edit: Sep 15, 2014, 04:01 pm by JimboZA Reason: 1
It is vague, I must say. It's not even clear to me (although I confess I didn't read every word) what the rotary switch does. I see there are buttonpin and buttonpin1 in that code, so perhaps that's two connections to the switch? Each position of the rotary switch is just a switch I guess, so hook it up as lots of switches. Switches are best when they have a pullup or pulldown resistor, and using the internal pullup is easiest, fewer components. Then as you can can see in the notes to the first pic below, the switch's pin goes low when pressed (or in your case, when rotated to).

I would recommend hooking up the servos like the second attached pic. Key is to provide their own power, not feed from Arduino 5V. Forum wisdom in the absence of any specific data, is to allow 1A for each motor that can run at the same time.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

johnwasser

The "rotery" switch isn't a rotary switch.  It's a microswitch like those used as limit switches in mechanical equipment.  It looks like there is one on each of the front corners to detect wall collisions.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

snowflax


It is vague, I must say. It's not even clear to me (although I confess I didn't read every word) what the rotary switch does. I see there are buttonpin and buttonpin1 in that code, so perhaps that's two connections to the switch? Each position of the rotary switch is just a switch I guess, so hook it up as lots of switches. Switches are best when they have a pullup or pulldown resistor, and using the internal pullup is easiest, fewer components. Then as you can can see in the notes to the first pic below, the switch's pin goes low when pressed (or in your case, when rotated to).

I would recommend hooking up the servos like the second attached pic. Key is to provide their own power, not feed from Arduino 5V. Forum wisdom in the absence of any specific data, is to allow 1A for each motor that can run at the same time.


Thanks a lot! I'm still a newbie in robotics field, can you tell me what the green dot (in the second picture) is? and I don't need to connect the two kinds of servo differently?


The "rotery" switch isn't a rotary switch.  It's a microswitch like those used as limit switches in mechanical equipment.  It looks like there is one on each of the front corners to detect wall collisions.


So, If I use rotary switch, it won't detect wall collisions as I need it to?

JimboZA

Green dots are wires that connect; no dot means just crossed over in the drawing and not connected to each other.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

snowflax

I'm in  an emergency and I need my robot to work soon, so can anyone tell me how to modify my servo (a TowerPro SG90 Micro Servo) so that it will work like I need it? I need the servo to know its position and angle so I can't get rid of the potentiometer.

zoomkat


I'm in  an emergency and I need my robot to work soon, so can anyone tell me how to modify my servo (a TowerPro SG90 Micro Servo) so that it will work like I need it? I need the servo to know its position and angle so I can't get rid of the potentiometer.


Bummer! Just watch the servo and tell the servo where it is. Or put the servo in the position you want it in and then disconnect it so it won't move to another position.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

snowflax

Actually it can rotate  :smiley-sweat:
If I change the code to

leftservo.write(0);
backleftservo.write(0);
rightservo.write(0);
backrightservo.write(0);

the left servos rotate counter-clockwise and the right clockwise, and I need all to rotate clockwise. So what should I put in the left servos' degree?

JimboZA


So what should I put in the left servos' degree?



180?

And if you want one side to go to (say) 55 you could synch the other side's with 180-55.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

snowflax

No, I need the servo to rotate continuously (I already modified my servo). I tried putting 180, 360, 90, none of them works

zoomkat

Quote
No, I need the servo to rotate continuously (I already modified my servo). I tried putting 180, 360, 90, none of them works


http://www.instructables.com/id/Very-simple-clean-up-robot/%20for%20a%20school%20project

If you modified your servo for the usual continuous rotation, then it in itself cannot be commanded to a specific  position. You might be able to use an encoder attached to the servo to determine when the servo should be commanded to stop rotating. If you modified the servos to be wheel drives for the bot, then you should have sent them a 1500us command and tweaked the pot until the servo motor stopped rotating, then glued the pot. Below is some servo test code you might use to determine the current servo command value at which the servo stops rotating. As usual this instructable is a lttle light on some important details, but is still a good project.

Code: [Select]

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) {
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos);
    }
  }
  readString=""; //empty for next input
}



Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

snowflax


Quote
No, I need the servo to rotate continuously (I already modified my servo). I tried putting 180, 360, 90, none of them works


http://www.instructables.com/id/Very-simple-clean-up-robot/%20for%20a%20school%20project

If you modified your servo for the usual continuous rotation, then it in itself cannot be commanded to a specific  position. You might be able to use an encoder attached to the servo to determine when the servo should be commanded to stop rotating. If you modified the servos to be wheel drives for the bot, then you should have sent them a 1500us command and tweaked the pot until the servo motor stopped rotating, then glued the pot. Below is some servo test code you might use to determine the current servo command value at which the servo stops rotating. As usual this instructable is a lttle light on some important details, but is still a good project.

Code: [Select]

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) {
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos);
    }
  }
  readString=""; //empty for next input
}




I only modified my servo by cutting the mechanical stopper in the gear, without touching the potentiometer at all. I thought by only doing that it won't rotate but if I write the code like this:

servo.write(0);

it rotates, but the left side ccw and the right side cw. and I need all to rotate cw

zoomkat

Quote
it rotates, but the left side ccw and the right side cw. and I need all to rotate cw


The below uses myservo2.write(180-n); to get the opposite side servo to rotate in the same direction.

Code: [Select]

//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together

String readString;
#include <Servo.h>
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);
  myservo2.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
    int n = readString.toInt();  //convert readString into a number
    myservo1.write(n);
    myservo2.write(180-n); //turns opposite side servo in desired direction
    readString="";
  }
}


Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

snowflax

from this code

Code: [Select]
#include <Servo.h>

Servo leftservo;
Servo rightservo;
Servo pickupservo;
Servo backrightservo;
Servo backleftservo;
int leftservoPin = 2;
int rightservoPin = 3;
int backleftservoPin = 4;
int backrightservoPin = 5;
int pickupservoPin = 6;
int buttonPin = 1;
int buttonPin1 = 7;

int buttonState = 0;
int buttonState1 = 0;

void forward()
{
  leftservo.write(0);
  backleftservo.write(0);
  rightservo.write(0);
  backrightservo.write(0);
}

void moveforward()
{
  leftservo.write(225);
  backleftservo.write(225);
  rightservo.write(45);
  backrightservo.write(45);


void left()
{
  leftservo.write(720);
  backleftservo.write(720);
  rightservo.write(0);
  backrightservo.write(0);
}

void right()
{
  leftservo.write(0);
  backleftservo.write(0);
  rightservo.write(720);
  backrightservo.write(720);
}

void pickup()
{
  pickupservo.write(100);
  delay(5000);
  pickupservo.write(0);
}

void setup()
{
  pickupservo.attach(pickupservoPin);
  leftservo.attach(leftservoPin);
  rightservo.attach(rightservoPin);
  backleftservo.attach(backleftservoPin);
  backrightservo.attach(backrightservoPin);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
}


how can I make it when buttonPin state is low it turns left (as in void left), when buttonPin1 state is low it turns right (as in void right), and when both is low it picks up (as in void pickup)

Your help is much appreciated, I'm about to present this robot soon, so I need it to work as I described above.

Go Up