Go Down

Topic: DC motor infering with Servo (Read 3771 times) previous topic - next topic

Spatial_Inertia

Im trying to create a somewhat autonomous car. I bought a smaller RC car from Radioshack, ripped out all the internal circuit board components, and am going to replace them with my arduino. I also bought this motor shied from Radioshack -> http://www.ebay.com/itm/like/400778673527?lpid=82&chn=ps


My RC car came with two motors built into it. One of them spins the back tires, while the other is used to steer the front tires. I ripped out the front motor for steering and replaced with small servo that i have. My Motor shield uses pins 8 - 13, and i have ultrasonic sensor hooked up to pins 6 and 7, with my servo connected to pin 5. I have no programming errors(that i know of). But after i compile my code, and upload it to the Arduino, my servo motor will tick, like its trying to do something, and my DC motor doesn't move at all.

When i comment out the code the set the servo to pin 5, the DC motor runs just fine. But when I have servo active, the DC motor doesn't run at all. If i unplug the Servo sensor pin(5), the dc motor still doesn't run at all.

So it seems when I do any kind of initialization of my Servo, the dc motors doesn't run.


here is an example of the first part of my code.

#include <MotorDriver.h>
#include <Servo.h>

#define servoPin 5
#define trigPin 6
#define echoPin 7


long duration, distance;
Servo myservo;
int pos = 0; //used to track where the servo is

void setup() {

  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 
  //motor code for my Arduino Shield I got from Radio Shack
  motordriver.init();
  motordriver.setSpeed(200, MOTORB); //probably dont need this since i only have 1 motor connected.
  motordriver.setSpeed(200, MOTORA); //setting up only one motor here, because the other set isn't used at all.
 
  //servo motor used to steer
  myservo.attach(servoPin);  // attaches the servo on servoPin to the servo object
  myservo.write(15); //sets the position of the servo so it always aligns the wheels straight
 

}



the rest of the code is just what to do with the information gotten from the ping sensor, probably not important, so i wont clutter up my post with it.


So my question is, does anyone have any ideas why the DC motor connected to the shield doesnt work when I initialize my servo?

Spatial_Inertia

#1
Jan 11, 2015, 09:22 pm Last Edit: Jan 11, 2015, 09:28 pm by Spatial_Inertia
Additionally, both the DC motor connected to the shield, and the Servo motor connected to pin 5 on the shield work independently of each other in different programs. So all parts are functioning separately, just not together.

I have also unplugged the DC motor(But kept my original code in there, so the Ardunio still thinks i have them connected, and tried to see if the servo would work. I didn't. I have also tried different pins for the signal wire of my Servo, and it still doesnt work. I tried pins 3, 4, and 5 for the Servo.

zoomkat

Quote
Additionally, both the DC motor connected to the shield, and the Servo motor connected to pin 5 on the shield work independently of each other in different programs. So all parts are functioning separately, just not together.
Do not power motors/servos from the arduino board. Use an external power supply for them.
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

dlugi

I have the same problem as you. Did you manage to solve it?

I'm using two power sources: one for logic, one for motors/servo. But it's working as in your case. I can't figure out why, when I attach servo in code, one of the DC motors stops running. The second one is doing fine.

A_Khorram

Hello,

I am building an obstacle avoidance robot using the Arduino UNO board. The robot consists of a pair of dc motors being driven using the L293D H-Bridge which has been functioning just fine for a line tracer robot project. I have added a HiTEC HS-422 servo motor (in parallel with a 100 micro farad decoupling capacitor) for the distance measurement part to be able to rotate the ping sensor to the left (~10 degrees) and right (~170 degrees).

Both DC motors and the servo have their own dedicated power source not to overload the Arduino power output and their grounds are connected to the common ground with the Arduino board (reported as a common mistake).

DC motors and the servo work just fine on their own as the robot goes forward and backward and turns right and left and servo passes the sweep test and moves nicely to any predefined angle. As I try to run the DC motors and the servo together, the servo turns to almost 0 degree and kind of twitches around 0 degree regardless of the predefined angle such as 90 or 170 degrees. I have tried the following scenarios and the following code for wiring the H-Bridge to overcome the problem but had no success:
First scenario:
Controlling the speed of the DC motors with a potentiometer and analogWrite(right_mot_Enable_Pin,Motor_Speed):
L293D-pin-1 connected to Arduino-pin- ~6
L293D-pin-2 connected to Arduino-pin- 8
L293D-pin-3 connected to left motor
L293D-pin-4 connected to common ground
L293D-pin-5 connected to common ground
L293D-pin-6 connected to left motor
L293D-pin-7 connected to Arduino-pin- ~10
L293D-pin-8 connected to + 9 V Battery

L293D-pin-9 connected to Arduino-pin- ~3
L293D-pin-10 connected to Arduino-pin- ~5
L293D-pin-11 connected to right motor
L293D-pin-12 connected to common ground
L293D-pin-13 connected to common ground
L293D-pin-14 connected to right motor
L293D-pin-15 connected to Arduino-pin- ~9
L293D-pin-16 connected to +5 V from Arduino

Servo-signal-pin connected to Arduino-pin- ~11

Second scenario:
No speed control on the DC motors:
L293D-pin-1 connected to +5 V from Arduino
L293D-pin-2 connected to Arduino-pin- ~5
L293D-pin-3 connected to left motor
L293D-pin-4 connected to common ground
L293D-pin-5 connected to common ground
L293D-pin-6 connected to left motor
L293D-pin-7 connected to Arduino-pin- ~6
L293D-pin-8 connected to + 9 V Battery

L293D-pin-9 connected to +5 V from Arduino
L293D-pin-10 connected to Arduino-pin- 8
L293D-pin-11 connected to right motor
L293D-pin-12 connected to common ground
L293D-pin-13 connected to common ground
L293D-pin-14 connected to right motor
L293D-pin-15 connected to Arduino-pin- ~9
L293D-pin-16 connected to +5 V from Arduino

Servo-signal-pin connected to Arduino-pin- 4




#include <Servo.h>

Servo myservo;
int Motor_Speed = 0;
   
const int left_mot_forward_Pin = 8;    // Left Motor connected to digital pin 8
const int left_mot_backward_Pin = 10;    // left Motor connected to digital pin 10
const int right_mot_forward_Pin = 9;    // right Motor connected to digital pin 9
const int right_mot_backward_Pin = 5;    // right Motor connected to digital pin 5
const int left_mot_Enable_Pin = 6;    // Left Motor Enable Pin
const int right_mot_Enable_Pin = 3;    // Right Motor Enable Pin
const int Pot_Pin = A3;    // Pot Pin
void setup()
{
Serial.begin(9600);
Motor_Speed = map(analogRead(Pot_Pin), 0, 1023, 0, 255);
myservo.attach(11);  // attaches the servo on pin 11 to the servo object
pinMode(left_mot_forward_Pin, OUTPUT); // Left Motor
pinMode(left_mot_backward_Pin, OUTPUT); // Left Motor
pinMode(right_mot_forward_Pin, OUTPUT); // Right Motor
pinMode(right_mot_backward_Pin, OUTPUT); // Right Motor
}

void loop()
{
myservo.write(90);   //Rotate the servo to face the front
moveForward();
 
}
void moveForward() //This function tells the robot to go forward
{
  digitalWrite(right_mot_forward_Pin, HIGH); //Right Motor Forward 
  digitalWrite(right_mot_backward_Pin, LOW); //Right Motor Backward
  analogWrite(right_mot_Enable_Pin,Motor_Speed);
 
  digitalWrite(left_mot_forward_Pin, HIGH); //Left Motor Forward 
  digitalWrite(left_mot_backward_Pin, LOW); //Left Motor Backward
  analogWrite(left_mot_Enable_Pin,Motor_Speed);
}

TomGeorge


A_Khorram, it would be worth your while starting a new thread rather than add to an old one.

Also;
Can you please post a copy of your sketch, using code tags?
They are made with the  </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

To help clarify things.

Tom.... :)
You can request from the moderator to move your thread for you.

Everything runs on smoke, let the smoke out, it stops running....

SARSOUT_Aymen

I 've had the same problem.
I solved it by changing Arduino UNO by Arduino DUE

nielyay

Hi.

Quote
here is an example of the first part of my code.
*I still didnt understand what do you want.

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

#define trigPin 6
#define echoPin 7

long duration, distance;

Servo myservo;

byte pos = 0;
byte motordriver;

byte motorA = 2;
byte motorB = 3;

void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  myservo.attach(5);
}

void loop()
{
  int trigPinState = digitalRead(trigPin);

  if (trigPinState == HIGH)
  {
    digitalWrite(motorA, LOW);
    digitalWrite(motorB, HIGH);
  }
  else
  {
    digitalWrite(motorA, HIGH);
    digitalWrite(motorB, LOW);
  }

  if (motorA > 0)
  {
  myservo.write(0);
  }
  else if (motorB > 0)
  {
  myservo.write(15);
  }
}


*but here i modificate your code

Quote
DC motor connected to the shield doesnt work when I initialize my servo?
*make a if else command, the motor will run if the trigPin has connected, and when motor run, the servo will turns 15 degree


Go Up