Servo + Dc motor

Hi, I was wondering if anyone knows how to fix a problem where when i attach a servo to my robot. Somehow my right dc motor no longer gets an signal. Once i detach the servo it works fine again.

It might be helpful if you posted a schematic, pictures and code of what exactly you are trying to do. If you are incorporating other hardware (for instance, one or more shields, or other external hardware), you need to indicate that as well and provide specification info for those too (manufacturer and model numbers are good).

this is the code i’m using, more specifically the sweep function where i sweep for some data and then move according to the results.

#include <Servo.h>

//Variable Declarations

//IR and Obstacle Detection
int IRpin = 1;                                       //analog pin for reading the IR sensor
//float distance[30];                                //array for distance from obstacles obtained from servo movement
int theta;                                           //angle of the servo
int distance;                                        //distance converted from volts
int volts;                                           //vin obtained from IR sensor

//Colour Sensor

int ServoPin = 11;                                   //digital pin for Servo motor
int pos = 45;                                        //initial position for the Servo
Servo myservo;                                       //create servo object

//DC Motors and Encoders
int RAPin = 9;                                       //high for forwards  - ccw
int RBPin = 10;                                      //high for backwards - cw
int LAPin = 5;                                       //high for forwards  - ccw
int LBPin = 6;                                       //high for backwards - cw 
int REPin = 2;                                       //digital pin for right encoder
int LEPin = 4;                                       //digital pin for left encoder
const float mmPerCount = 0.0458672527;               //values must never change
const float mmPerDeg = 1.5706;                       //hard coded via calibration/calculation
    /*NOTE: Floating point numbers are not exact
      and may yield strange results when compared. 
      For example 6.0 / 3.0 may not equal 2.0. */
volatile int countR = 0;                            //R encoder counter
volatile int countL = 0;                            //L encoder counter

void setup() {
  Serial.begin(9600);                           //start the serial port
  pinMode(2, INPUT);                            //set RIGHT encoder to pin 2
  attachInterrupt(0, countRup, FALLING);        //attach interrupt to RIGHT encoder
  pinMode(4, INPUT);                            //set LEFT encoder to pin 4
  attachInterrupt(1, countLup, FALLING);        //attach interrupt to LEFT encoder

void loop(){
  volts = analogRead(IRpin)*0.0048828125;
  distance =  27.249*pow(volts, -1.2479);
  if(distance > 25){
     straight(50, 50, true);
  if(distance <=25){

boolean colorCheck()
  //MODIFIES: black, white
  //EFFECTS: checks value from color sensor, if the right colour return true
  //         else return false.

//Servo Functions
 float[] sweep() 
  //MODIFIES: distance[]
  //EFFECTS: moves servo from 45 degree to 135 at increments of 3 degrees
  //         and also saves the distance at each position in an array. 
  //         Return distance[].
    counter = 0;
    for(pos = 45; pos <135; pos +=3){
      volts = analogRead(IRpin)*0.0048828125;
      distance[counter] = 27.249*pow(volts, -1.2479(;
    return distance[];
//DC Motor Functions
void straight(float distance, int rbtspeed, boolean direct)
  //REQUIRES: distance is a number in millimeters, direction is T or F
  //          0 < distance < 1450
  //          this is due to integer overflow at 32768
  //EFFECTS: moves robot forwards or backwarads desired input distance
    //calculate number of encoder counts in given distance
    int count = distance/mmPerCount;           
    while((count>=countR)||(count >= countL))
      //set direction   
      if(direct == true)          
      {  forward(rbtspeed);  }
      {  backward(rbtspeed);  }
  void rotate(int degree)          
  //REQUIRES: -360 < degree < 360
  //EFFECTS: Rotates robot cw or ccw given degrees
    //calculate number of encoder counts in given degrees
    int count = abs(degree)*mmPerDeg/mmPerCount;  
    //mid range robot speed for rotation
    int rbtspeed = 177;        
       if(degree > 0)
       {   clockwise(rbtspeed);  }           
       {  counterwise(rbtspeed);  }
  void stop()
  //MODIFIES: countR, countL - Resets interrupts encoder counts to zero
  //EFFECTS: restricts power to motors, thus stopping robot movements
    //set all motor pins to zero    
    analogWrite(RAPin, 0);                
    analogWrite(RBPin, 0);
    analogWrite(LAPin, 0);
    analogWrite(LBPin, 0);
    //reset encoder counts to zero
    countR = 0;                          
    countL = 0;
  void forward(int rbtspeed)
  //REQUIRES: 0 < rbtspeed < 255
  //EFFECTS: Moves motors FORWARDS (counter clockwise) at a given speeds
    analogWrite(RBPin, 0);             //set B pins low to move FORWARD  
    analogWrite(LBPin, 0);
    analogWrite(RAPin, rbtspeed);
    analogWrite(LAPin, rbtspeed); 
  void backward(int rbtspeed)
  //REQUIRES: 0 < rbtspeed < 255
  //EFFECTS: Moves motors BACKWARDS (clockwise) at given speeds
    analogWrite(RAPin, 0);            //set A pins low to move BACKWARDS  
    analogWrite(LAPin, 0);
    analogWrite(RBPin, rbtspeed);
    analogWrite(LBPin, rbtspeed); 
  void clockwise(int rbtspeed)
  //REQUIRES: 0 < rbtspeed < 255
    analogWrite(RAPin, 0);            //Rmotor left back ccw
    analogWrite(RBPin, rbtspeed);
    analogWrite(LBPin, 0);            //Lmotor rightforward  cw
    analogWrite(LAPin, rbtspeed); 
  void counterwise(int rbtspeed)
  //REQUIRES: 0 < rbtspeed < 255
    analogWrite(RBPin, 0);            //Rmotor forward ccw
    analogWrite(RAPin, rbtspeed);
    analogWrite(LAPin, 0);            //Lmotor back cw
    analogWrite(LBPin, rbtspeed);
  //NOTE: Interrupts will be in effect while the robot is moving (due to encoders)
  void countRup()   //Right encoder interrupt 
  //MODIFIES: countR - increments
  //EFFECTS: Increase right encoder count
  {  countR++; }

  void countLup()   //Left encoder interrupt
  //MODIFIES: countL - increments
  //EFFECTS: Increase left encoder count
  {  countL++; }

this is the picture link for the microcontroller how we assigned the pins:

this is the schematic of the power supply:

this is the schematic of the h bridge:

thanks for your help. let me know if you need more info

In your sweep function, you have this code (which I am not sure how it compiles!):

distance[counter] = 27.249*pow(volts, -1.2479(;

Should be:

distance[counter] = 27.249*pow(volts, -1.2479);

Also - I (and probably nobody else) can see your pics - they go to a google groups view that requires a membership or something to view them; please repost them.



Here is re-post hopefully you guys can see it now. And with the code I corrected it after. It was really about the content within the servo/sweep function code. As it is whenever I servo.attached even if i detach after use, my signal to the right motor is not sent for some reason. even if i am just making it move straight. Thanks a lot.

I am not seeing anyplace in your code where you are calling the sweep() function. You need to post the code that doesn’t work…?

void loop() {

counter = 0;
for(pos = 45; pos < 135; pos += 3)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
  //  Serial.println(pos);
    volts = analogRead(IRpin)*0.0048828125;
    distance[counter] =  27.249*pow(volts, -1.2479);
   // Serial.println(distance[counter]);
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(45);    // waits 15ms for the servo to reach the position 

  for(int i =0; i<30; i++){
        command = 0;
        command = 1;
    if(command = 1){
    if(command = 0){

this is the code i used, i cleaned it up into a function sweep, but i didn’t use it in the code i uploaded cause it was giving me the error i described.

In that last code you posted, if it hits that “break;” statement because any one of the distance values is under 30 (which could very well be the case), it will pop out of the for() loop. Also, there’s an extra brace (}) after the break statement that shouldn’t be there. Plus you have “if (command = 1){” and the same for checking for zero - those are assignment statements; you need “==” for comparison.

Hm I mean to make it pop out of the for loop, and I changed those assignment errors. These are the causes for the right motor's signal being no outputted? Since I'm still getting output into the left motor when command is 1. Sorry for the trouble, I'm new at programming robotics haha. Thanks.