Problems to control motor dc(to control them I use motor driver l298n) with servo motot

Hey guys I am having trouble with servo motor, When I use servo motor I can't control motor dc. Do guys have any idea to solve it?

Hi!

This problem (which you didn´t describe), can happen because of your wiring (which we don´t know) or because of your code (which we also don´t know). So how about spending a little more time providing enough information on the problem, the schematics and the code? :wink:

No code.
No schematic.
No pictures of your set up.
No description of what is not working.

Sure... should be pretty easy to solve.

There is the code


#include <NewPing.h>
#include<Servo.h>
Servo servo;

#define SERVO_PIN 13
#define MAX_DISTANCE 100
//Motor A
int PWMA = 9; 
int IN1 = 8; 
int IN2 = 7; 
//Motor B
int PWMB = 3; 
int IN4 = 4; 
int IN3 = 5;


//sensor ultrassonico
int trigPin = 10;
int echoPin = 11;
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
int cm;
void setup() {
   pinMode(PWMA, OUTPUT);   
  pinMode(IN1, OUTPUT);   
  pinMode(IN2, OUTPUT);   
  pinMode(PWMB, OUTPUT);   
  pinMode(IN3, OUTPUT);   
  pinMode(IN4, OUTPUT);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT); 
  servo.write(90);
  delay(200);
  para();
}

void esquerda() {
analogWrite(PWMA,40); // Motor Direito
 analogWrite(PWMB,0); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
}

void direita() {
 analogWrite(PWMA,0); // Motor Direito
 analogWrite(PWMB,40); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);  
 
} 

void para (){
 analogWrite(PWMA,0); // Motor Direito
 analogWrite(PWMB,0); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
  
}
void frente (){
 analogWrite(PWMA,80); // Motor Direito
 analogWrite(PWMB,80); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
  
}

void tras(){
   analogWrite(PWMA,80); // Motor Direito
 analogWrite(PWMB,80); // Motor Esquerdo
 digitalWrite(IN1,LOW);   
 digitalWrite(IN2,HIGH);  
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW); 
}
int readDistance(){
  int cm = sonar.ping_cm();
  return cm;
  }




void loop() {
cm = readDistance();
if(cm==10)
{servo.write(90);
esquerda();
}
}

I am using
Ultrasonic sensor HC-SR04
Servo motor 9g
arduino uno R3
Motor Driver L298N

do you mean servo or DC motor. a 9g servo is controlled by a 1-2 msec pulse. a DC motor with a motor shield

the posted code is incomplete

#include <NewPing.h>
#include<Servo.h>
Servo servo;

#define SERVO_PIN 13
#define MAX_DISTANCE 100
//Motor A
int PWMA = 9; 
int IN1 = 8; 
int IN2 = 7; 
//Motor B
int PWMB = 3; 
int IN4 = 4; 
int IN3 = 5;


//sensor ultrassonico
int trigPin = 10;
int echoPin = 11;
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
int cm;

void setup() {
   pinMode(PWMA, OUTPUT);   
  pinMode(IN1, OUTPUT);   
  pinMode(IN2, OUTPUT);   
  pinMode(PWMB, OUTPUT);   
  pinMode(IN3, OUTPUT);   
  pinMode(IN4, OUTPUT);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT); 
  servo.attach(2);
  servo.write(90);
  delay(200);
  para();
}

void esquerda() {
analogWrite(PWMA,40); // Motor Direito
 analogWrite(PWMB,0); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
}

void direita() {
 analogWrite(PWMA,0); // Motor Direito
 analogWrite(PWMB,40); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);  
 
} 

void para (){
 analogWrite(PWMA,0); // Motor Direito
 analogWrite(PWMB,0); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
  
}
void frente (){
 analogWrite(PWMA,80); // Motor Direito
 analogWrite(PWMB,80); // Motor Esquerdo
 digitalWrite(IN1,HIGH);   
 digitalWrite(IN2,LOW);  
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH); 
  
}

void tras(){
   analogWrite(PWMA,80); // Motor Direito
 analogWrite(PWMB,80); // Motor Esquerdo
 digitalWrite(IN1,LOW);   
 digitalWrite(IN2,HIGH);  
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW); 
}
int readDistance(){
  int cm = sonar.ping_cm();
  return cm;
  }




void loop() {
cm = readDistance();
if(cm==10)
{servo.write(0);
esquerda();
}
}
 

Dc motors are controlled with motor drive l298n and 9g servo motor is controlled by arduino.

doesn't the arduino control the motor shield have direction and enable pins controlled with PWM (i.e. analogWrite())

What do u mean?

?

are you suggesting the motor is not controlled by the arduino?

They are controlled by arduino.

but I am using motor driver too

yes, the arduino controls the motor shield that provide power to the motor.

what is the interface to the motor shield?
ultimately you just want to control direction and speed.

yes I just wanna control the direction and speed. But I can't when I use servo motor. But I when I don't use servo motor it works well.

How many rounds of this ping-pong game with
tooo short postings
asking back for details
repeat

do you wanna play?

all your code does is:

turn servo at position 90 degrees
in case the distance measured by the ultrasonic sensor is exactly 10 cm do two things:

  1. turn servo to position 0 degrees
  2. switch on motor on the right making your car turning left in circles

If this is what you can watch when running your code
The code does exactly what the code is suposed to do

If you observe something different you should

describe in detail

all the differencies

best regards Stefan

ok, my guess is the power supply can’t sustain both the servo and the motor.

Everything else is a guess.

You cannot control a "servo motor" with a DC motor driver. Arduino has a ready made servo library (Servo.h). Post a link to your servo datasheet.

" 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."
RTFM

Hi,
Is this to do with your line follower?

Why don't you post a circuit diagram?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia: