Uno R3, 1 DPDT Switch, 2 servos, 12v car battery

Two servos are going to be rotating an object from horizontal to vertical based on high value from switch poles. High value on one pole raises to vertical (switch up position) and high value on other pole lowers to horizontal (switch down position).

I will show the code later as I am not at home to do so right now. But it works as expected, with the only issue being the stuttering of the servos when the switch is in the center/off position. I’m currently in testing environment with both servos running off of the Arduino and drawing power from usb.

This will eventually be located in a car and running off the 12v battery.

I know I can run the Arduino from the 12v in the VIN. But after days of googling, I still have not found a simple, straight forward answer on how to wire the servos externally with 12v power source.

Also, I need to know how long my wiring can be. The Arduino will be in the console and the servos will be in the back deck. Rough estimate would be 10’ of wire from The Arduino to servos. Will that be a problem?

Quick version for those who don’t like details:

  1. Servos stuttering with DPDT switch in center/off position (connected to the Arduino)
  2. How to connect servos externally to 12v car battery
  3. What are the length limits for wires from the Arduino to servos

Any help is greatly appreciated and code will be coming shortly

1) Without your circuit diagram, my best guess is that your outputs are floating. A pull up or pull down resistor may solve it. 2) What voltage are your servos? They're usually 5 V, but some are different. 3) As long as you're using wire thicker than a human hair, 10 ft. should be OK.

Thanks Henry

DPDT

5v Gnd Dig7 Dig2 Gnd 5v

Not sure what else to give or how to produce a decent looking wiring diagram. I don't know what pull up or pull down resistors are, but I'll look that up.

The servos are 4.8-6.0v.

The wire is 22AWG.

I'm beginning to think I'll need a switching regulator in the mix to power the servos from the 12v battery. Or how about a motor shield?

Thanks again, Henry

with the only issue being the stuttering of the servos when the switch is in the center/off position.

My guess is that you are switching the servo power on the servo ground wire with the switch. If so, you may damage the servo internals. also, are you powering the servos from the arduino? Servos need external power.

zoomkat:
My guess is that you are switching the servo power on the servo ground wire with the switch. If so, you may damage the servo internals. also, are you powering the servos from the arduino? Servos need external power.

Thanks for replying, zoomkat. I stated twice that the servos are being powered by the arduino. And the entire premise of my topic is how to run them with external power. I know I’m new around here, and new to arduino in general, but I have to say that it’s frustrating when people like you, who could probably do what I’m wanting to do in their sleep, don’t bother reading through an initial post before commenting.

With that said, and if you are still willing to assist, I would be happy to answer any question you have about my setup in order to help get to a solution.

I’m sweeping the servos with no load while connected to the arduino. The servos are getting power and ground from the breadboard with signal going directly to digital pins 8 and 9. The switch is also getting power and ground from the breadboard and pole signals going directly to digital pins 7 and 2. Hope that clears up things a little.

 #include <Servo.h>
 Servo Rservo;
 Servo Lservo;

const int switchDown = 7;  //pin for down position pole on switch
const int switchUP = 2;    //pin for up position pole on switch
const int upPos = 175;     //highest degree of sweep (vertical stop)
const int downPos = 80;    //lowest degree of sweep (horizontal stop)

int upVal = 0;        //variable for value of switch "up" pole (HIGH or LOW)
int downVal = 0;      //variable for value of switch "down" pole (HIGH or LOW)
int pos = downPos;    //degree position of servo

void setup(){
  pinMode(switchDown, INPUT);  //set switchDown pin (7) to INPUT
  pinMode(switchUP, INPUT);    //set switchUP pin (2) to INPUT
  Rservo.attach(9);            //set right servo to pin 9
  Rservo.write(pos);           //sweep right servo to start position
  Lservo.attach(8);            //set left servo to pin 8
  Lservo.write(pos);           //sweep left servo to start position
}

void loop(){
  downVal = digitalRead(switchDown);  //set downVal to switchDown value
  upVal = digitalRead(switchUP);      //set upVal to switchUP value
  
  if(upVal==HIGH){        //if switch in up position, loop to sweep servos
                          //from current position to vertical stop
                          
    for(pos = Rservo.read(); pos < upPos; pos += 1)  //increment upwards through loop
   {                                  
     Rservo.write(pos);      //servos are facing each other (mirrored) so
     Lservo.write(180-pos);  //as one travels from 80 (downPos) to 175 (upPos),
     delay(15);              //the other travels from the other side,     
   }                         //100 (180 - downPos) to 5 (180 - upPos)
   
   
  }else if(downVal==HIGH){   //if switch in down position, loop to sweep servos
                             //from current position to horizontal stop
                             
     for(pos = Rservo.read(); pos > downPos; pos -= 1)  //increment down through loop
   {                                  
     Rservo.write(pos);  
     Lservo.write(180-pos);     
     delay(15);                       
   } 
  }
}

And just in case these help...

I guess in the spirit of testing, I should reconfigure and wire the servos externally with 4 AA batteries to get 6V of power. My current config and code work as expected, save for the stuttering while the switch is in the off position.

So here is the diagram of what I think I should do to power the arduino with a 9V battery and the servos with 4 AA batteries. Am I close? I didn’t want to fry anything, so I thought I’d check with you all first.

And once this is accomplished, my goal is to implement this in a car. I need to power everything through the 12V car battery.

I think I see why your servos are 'stuttering'. You're writing to them every time the program loops, even though they're in the correct position. What you need to do is detect when the switch CHANGES position instead of detecting the position it's in. Look at the state change example in the examples page of this site.

HI, is that switch a three position switch, with centre off? That is its a DPTT, double pole triple throw. If so when you are in centre off, Dig 5 and dig 7 are no connected to anything, they are floating. Place 10K resistor each from gnd to Dig 5 and Dig 7. And yes, power the servos on a different supply to the arduino.

Tom..... :)

When your switch is in the central position Dig7 and Dig2 are floating. Due to the high impedance of the arduino inputs, they may take some time to settle (this is why they are stuttering.

I'd suggest you put a 4.7k resistor between GND and Dig2 (and the same with Dig7) to resolve the stuttering issue.

With the resistor in place you could (if you want to simplify the wiring) change the switch configuation to a single pole thus.

Dig2 5v Dig7

On the subject of power supply, a quick search on ebay for 12v to 5v will probably overwhelm you with choice. Just pick something that suits your budget and power requirements.

BTW 10 feet will be no problem.

Hi, this is the kind of dc-dc convertor you can find on ebay. You will need to adjust it to 5V output with your multimeter. You can then run the Arduino from the 5V supply too, by connecting it via the 5V connector and ground, ie. Bypassing the Arduino's barrel connector and built-in regulator.

http://www.ebay.co.uk/itm/1Pcs-XL6009-DC-DC-Adjustable-Step-up-boost-Power-Converter-Module-Replace-LM2577-/271532706970?pt=UK_BOI_Electrical_Test_Measurement_Equipment_ET&hash=item3f389c4c9a

You could get two UBECs like below and set one for 5v for the arduino (or just use the barrel connector), and the other for 6v for the servos. From the picture the switch seems to be two position. The floating of the non connected side of the switch can cause problems.

http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=ubec&_sop=15

Thanks everyone for the input. I have re-tooled and re-coded. I am now running with 9V battery powering the arduino and a 6V battery pack powering the servos. The switch is now a DPST rocker switch. The code works as expected.

I’m now back to the initial premise for my topic here, guidance as to how to change my current wiring scheme to have 12V from a car battery power everything. This isn’t something I can tinker with easily and try to figure it out. I’m going to need some guidance before trying to implement in the car.

#include <Servo.h>
 Servo Rservo;
 Servo Lservo;

const int buttonPin = 2;   //pin for rocker switch
const int upPos = 150;     //highest degree of sweep (vertical stop)
const int downPos = 60;    //lowest degree of sweep (horizontal stop)

int pos = upPos;           //degree position of servo, initially set to up (vertical) position
int buttonState = 0;

void setup(){
  pinMode(buttonPin, INPUT);      //set buttonPin pin (2) to INPUT
  digitalWrite(buttonPin, HIGH);  //set value of switch to HIGH
  Rservo.attach(9);               //set right servo to pin 9
  Rservo.write(pos);              //sweep right servo to start position (up/vertical)
  Lservo.attach(8);               //set left servo to pin 8
  Lservo.write(pos);              //sweep left servo to start position (up/vertical)
}

void loop(){
  buttonState = digitalRead(buttonPin);  //read state of switch
  
  if(buttonState==HIGH){        //if switch in up position, loop to sweep servos
                                //to vertical stop
                          
    for(pos = Rservo.read(); pos < upPos; pos += 1)  //increment upwards through loop
   {                                  
     Rservo.write(pos);      //servos are facing each other (mirrored) so
     Lservo.write(180-pos);  //as one travels from 60 (downPos) to 150 (upPos),
     delay(15);              //the other travels from the other side,     
   }                         //120 (180 - downPos) to 30 (180 - upPos)
   
   
  }else if(buttonState==LOW){   //if switch in down position, loop to sweep servos
                                //to horizontal stop
                             
     for(pos = Rservo.read(); pos > downPos; pos -= 1)  //increment down through loop
   {                                  
     Rservo.write(pos);  
     Lservo.write(180-pos);     
     delay(15);                       
   } 
  }
}

 }else if(buttonState==LOW){

If it's not high, what else can it be in a binary system, other than low? Change to:

 }else {

And use Ctrl + T in the IDE to format your code properly.