Servo and DC motor

Hello, i'm very new on Arduino world but i have some experience of programming in other language.

i'm try to make my first robot car, i have 2 DC motor connect to a module L298N , all connected to a 9V battery and arduino uno.

DC motor connected to port 5,6,7 for the motor 1 and port 8,9,10 for motor 2

the code working fine to go forward and back.

At this stage I want to connect a dc servo , which i connected to the port 13, to the 5 volt and to ground, now here the issue ..

with the following code only one DC motor and servo moving but the second DC motor is stuck!

i notice that if i remove from the void setup the command "servo_motor.attach(13);" both dc motor running.

it should move servo and both DC motor...

any reason why?

thanks for help
Dm1886

#include <Arduino.h>
#include <Servo.h>
const int mot2 = 10; 
const int ava2 = 9;
const int ind2 = 8;
const int mot1 = 5; 
const int ava1 = 6;
const int ind1 = 7;
Servo servo_motor;  // create servo object to control a servo
int pos = 0;
 void moveForward()
{
 Serial.print("Going Forward\n");
 
  // turn on motor A
  digitalWrite(ava1, HIGH);
  digitalWrite(ind1, LOW);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot1, 100);

  // turn on motor B
  digitalWrite(ava2, LOW);
  digitalWrite(ind2, HIGH);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot2, 100);

  delay(2000);
}

void moveBack()
{
 Serial.print("Going BACK\n");

  // turn on motor A
  digitalWrite(ava1, LOW);
  digitalWrite(ind1, HIGH);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot1, 250);

  // turn on motor B
  digitalWrite(ava2, HIGH);
  digitalWrite(ind2, LOW);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot2, 250);
  
  delay(2000);
}

void moveServo(){ 
 for (pos = 0; pos <= 180; pos += 1) { 
    servo_motor.write(pos);             
    delay(15);                       
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    servo_motor.write(pos);              
    delay(15);                       
  }
}


void setup()
{
 
  Serial.begin(9600);
  servo_motor.attach(13); //  why if i remove this both DC motor work and if i put only one DC motor work??
  pinMode(mot2, OUTPUT);
  pinMode(ava2, OUTPUT);
  pinMode(ind2, OUTPUT);
  pinMode(mot1, OUTPUT);
  pinMode(ava1, OUTPUT);
  pinMode(ind1, OUTPUT);


}

void loop()
{
moveServo();
delay(2000);
moveForward();
delay(2000);
moveBack();
}

How is the Arduino powered? A schematic of all connections would help.

Probably you don't have enough power to run motors and a servo. If "9V battery" is a little rectangular smoke-detector battery and that's the only power you have then you definitely don't have enough power to run motors and a servo.

Steve

What is the power source for the motors and servo.

all connected to a 9V battery

If the battery is what I know as a PP3 then it will not last long

As to the motor control problem, have a look at Servo - Arduino Reference which, amongst other things says

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

Guess which pin you are using for mot2

Yes, it look like a limitation of the library, on arduino uno... i have try in on arduino mega and it work perfect with the 9 volt battery....

the strange stuff is .. i have try to move the dc motor to the pin 2,3,4,5,6,7 and servo connect on 8.. use it on arduino uno... and still not working....

on mega all perfect...

#include <Arduino.h>
#include <Servo.h>
const int mot2 = 2; 
const int ava2 = 3;
const int ind2 = 4;
const int mot1 = 5; 
const int ava1 = 6;
const int ind1 = 7;
Servo servo_motor;  // create servo object to control a servo
int pos = 0;
 void moveForward()
{
 Serial.print("Going Forward\n");
 
  // turn on motor A
  digitalWrite(ava1, HIGH);
  digitalWrite(ind1, LOW);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot1, 100);

  // turn on motor B
  digitalWrite(ava2, LOW);
  digitalWrite(ind2, HIGH);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot2, 100);

  delay(2000);
}

void moveBack()
{
 Serial.print("Going BACK\n");

  // turn on motor A
  digitalWrite(ava1, LOW);
  digitalWrite(ind1, HIGH);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot1, 100);

  // turn on motor B
  digitalWrite(ava2, HIGH);
  digitalWrite(ind2, LOW);

  // set speed to 200 out of possible range 0~255
  analogWrite(mot2, 100);
  
  delay(2000);
}

void moveServo(){ 
 for (pos = 0; pos <= 180; pos += 1) { 
    servo_motor.write(pos);             
    delay(15);                       
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    servo_motor.write(pos);              
    delay(15);                       
  }
}


void setup()
{
 
  Serial.begin(9600);
  servo_motor.attach(8); // <<<<<<<<<
  pinMode(mot2, OUTPUT);
  pinMode(ava2, OUTPUT);
  pinMode(ind2, OUTPUT);
  pinMode(mot1, OUTPUT);
  pinMode(ava1, OUTPUT);
  pinMode(ind1, OUTPUT);


}

void loop()
{
delay(4000);
moveServo();
delay(2500);
moveForward();
delay(2500);
moveBack();
delay(4000);
}

Pin 2.is not a PWM pin.

The Servo library uses one of the hardware timers. That timer can't also be used for PWM.

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

That is why Motor 2 isn't working right. Try a different PWM pins for Motor 2.