Problem when controlling dc motors with servo motors

Hello,
I am willing to control 2 dc motors and a servo motor at the same time. The code was working good ( both servo motors are working fine and do what it must be done ) until i tried to control the dc motors, more precisely when i have added these two lines in the code : analogWrite(ENA,255); analogWrite(ENB,128);
( dc motors don t work and servo motors too ) I am using a long code but this is the most important part of it.

  if(readString == "Hello"){
    //Serial.println(speed); 
    //Serial.println(effect); 
    while( counter < number_Balls ){
      CheckStop(); // a function that will stop the servo and dc motors
      if (millis() - previousMillis >= interval) {
        previousMillis = millis();
        if(servo_Controller == 0){
          pos = 0;
        }
        if(servo_Controller == 1){
          pos = 90;
          servo_Controller = 0;
          counter++;
          balls_Stock--;
          Serial.println(balls_Stock);
        }
        myservo.write(pos);
        if( pos == 0){
          servo_Controller = 1;
        }
        analogWrite(ENA,255);
        analogWrite(ENB,128);
      }
    }   
    counter = 0; 
    EEPROM.update(0, balls_Stock);
    readString="";
  }  
  //ENDS HERE

The CheckStop function :

void CheckStop(){
  if (Serial.available())  {
    char c = Serial.read();  
    stopString += c;
  }
  if (stopString == "stop") {
    myservo.write(90);
    Serial.println("nadir");
    counter = number_Balls*2;
    stopString="";
  }
}

I am using a L298N to control the two motors and this is how i am connecting it :

ENA=5;
IN1=2;
IN2=3;
ENB=6;
IN3=4;
IN4=7;

and i have two servos one connected to 9 pin and the other one to 10 pin
I am using the Servo.h for controlling the servos.

Please post the complete program

...R

If you know where and what the problem is then just fix it.

If you don't know what the problem is then how do you know which bits of code are important to it? I'm guessing the problem is either in an "unimportant" bit of the code or it's in the components you've chosen or the way they are connected or powered, none of which you're telling us about.

The code you've posted shows one servo not two and although your function CheckStop() claims to stop the DC motors it doesn't even try to do anything to any motors. It all seems to have very little to do with your description.

Full code, a circuit diagram and complete component details would possibly be helpful.

Steve

Thanks for the replies, here is code complete :

#include <Servo.h> 
#include <EEPROM.h>

int ENA=5; //Connecté à Arduino pin 5(sortie pwm)
int IN1=2; //Connecté à Arduino pin 2
int IN2=3; //Connecté à Arduino pin 3
String readString;

//-- MOTEUR B --
int ENB=6; //Connecté à Arduino pin 6(Sortie pwm)
int IN3=4; //Connecté à Arduino pin 4
int IN4=7; //Connecté à Arduino pin 7
String stopString;
Servo myservo;
int position = 0;
int speed,number_Balls,effect;
int balls_Stock = 0;
int counter = 0;
unsigned long previousMillis = 0;
const long interval = 500;
int servo_Controller = 0;
int pos = 0;

void setup() {
 pinMode(ENA,OUTPUT);//Configurer les broches comme sortie
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 digitalWrite(ENA,LOW);// Moteur A - Ne pas tourner (désactivation moteur)
 digitalWrite(ENB,LOW);// Moteur B - Ne pas tourner (désactivation moteur)

 // Direction du Moteur A
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);

 // Direction du Moteur B
 // NB: en sens inverse du moteur A
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
  pinMode(LED_BUILTIN, OUTPUT); // TO TEST - DELETE LATER
  pinMode(7,INPUT); // TO TEST - DELETE LATER
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {
  // Get commands from Serial
  if (Serial.available())  {
    char c = Serial.read();
    
    if (c == ',') {      
      if(readString.indexOf("NA") >=0) {
        readString=readString.substring(2);
        speed = readString.toInt();
      }
      if(readString.indexOf("DI") >=0) {
        readString=readString.substring(2);
        number_Balls = readString.toInt();
      }
      if(readString.indexOf("RE") >=0) {
        readString=readString.substring(2);
        effect = readString.toInt();
      }
      
      readString="";

    }  
    else {     
      readString += c;
    }
  }
  // ENDS HERE
 
  // Get numbers of balls in stock and display it
  balls_Stock = EEPROM.read(0);
  if(digitalRead(7)==HIGH){
    delay(200);
    balls_Stock++;
    EEPROM.update(0, balls_Stock);
    Serial.println(balls_Stock);
  }
  //Display it on Start
  if(readString == "limiter"){
    balls_Stock = EEPROM.read(0);
    Serial.println(balls_Stock);
    readString="";
  }
  // ENDS HERE

  //Do the precise function
  if(readString == "jobDone"){
    //Serial.println(speed); // WRITE A DC MOTOR CODE
    //Serial.println(effect); // DC MOTOR CODE 
    while( counter < number_Balls ){
      analogWrite(ENA,255);
      analogWrite(ENB,128);
      CheckStop();
      if (millis() - previousMillis >= interval) {
        previousMillis = millis();
        if(servo_Controller == 0){
          pos = 0;
        }
        if(servo_Controller == 1){
          pos = 90;
          servo_Controller = 0;
          counter++;
          balls_Stock--;
          CheckStop();
          Serial.println(balls_Stock);
          CheckStop();
        }
        CheckStop();
        myservo.write(pos);
        if( pos == 0){
          servo_Controller = 1;
        }
        myservo.write(180);
        delay(2000);
      }
    }   
    counter = 0; 
    EEPROM.update(0, balls_Stock);
    readString="";
  }  
  //ENDS HERE

  // Control of servo in Precise Layout
  if (readString == "left") {
    position = position + 10;
    if(position < 10){
      position = 0;  
    }
    if(position > 180){
      position = 180;   
    }
    myservo.write(position);
    readString="";
  }

  if (readString == "right") {
    position = position - 10;
    if(position < 10){
      position = 0;  
    }
    if(position > 180){
      position = 180; 
    }
    myservo.write(position); 
    readString="";
  }
  // ENDS HERE
}

//Stop ALL
void CheckStop(){
  if (Serial.available())  {
    char c = Serial.read();  
    stopString += c;
  }
  if (stopString == "stop") {
    myservo.write(90);
    Serial.println("nadir");
    counter = number_Balls*2;
    analogWrite(ENA,0);
    analogWrite(ENB,0);
    stopString="";
  }
}
//ENDS HERE

I mentioned before that i have posted only the important part of the code cause i am using a bt module to control the called "system" that is why i thought it is not important to post it . and yes you are right i have not posted the updated version of code for the function CheckStop.
for the components, i am using :

  • 2 DC MOTORS
  • L298N
  • 2 SERVO MOTORS
  • ARDUINO UNO
  • HC-05
    and for later i will be adding a STEPPER and A4988.

i am connecting the dc motors to the L298N like this :
ENA=5
IN1=2
IN2=3
ENB=6
IN3=4
IN4=7
Then the driver itself power the arduino from the 5V output while servo motors are connected to 9 and 10 pin of the arduino. The driver is powered with 12V .

for the record, i have used both dc motors with same circuit and controlled both with a potentiometer and it worked fine, the problem is when i add the servo motor.

wicklo:
and yes you are right i have not posted the updated version of code for the function CheckStop.

That reads as if there is a newer version of the CheckStop() function that you have not posted.

for the components, i am using :

  • 2 DC MOTORS
  • L298N
    - 2 SERVO MOTORS
  • ARDUINO UNO
  • HC-05
    and for later i will be adding a STEPPER and A4988.

The program you posted only has 1 servo

You need to describe in more detail what actually happens when you run the program.

And the code is very raggedy. There should be no need to get serial data within CheckStop() - just have a single place in the program where the serial input is collected.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Your code still only shows a single servo. Where is the second servo?

My first guess is that the problem is with the power. So what EXACTLY is powering the system? Battery? What type, size? Or power supply? What is the current capability?

What type of servo or servos? Where are the servo power and ground connected?

Steve

The code with two servos :

#include <Servo.h> 
#include <EEPROM.h>

int ENA=5; //Connecté à Arduino pin 5(sortie pwm)
int IN1=2; //Connecté à Arduino pin 2
int IN2=3; //Connecté à Arduino pin 3
String readString;

//-- MOTEUR B --
int ENB=6; //Connecté à Arduino pin 6(Sortie pwm)
int IN3=4; //Connecté à Arduino pin 4
int IN4=7; //Connecté à Arduino pin 7
String stopString;
Servo myservo;
Servo Servo_direction;
int position = 0;
int speed,number_Balls,effect;
int balls_Stock = 0;
int counter = 0;
unsigned long previousMillis = 0;
const long interval = 500;
int servo_Controller = 0;
int pos = 0;

void setup() {
 pinMode(ENA,OUTPUT);//Configurer les broches comme sortie
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 digitalWrite(ENA,LOW);// Moteur A - Ne pas tourner (désactivation moteur)
 digitalWrite(ENB,LOW);// Moteur B - Ne pas tourner (désactivation moteur)

 // Direction du Moteur A
 digitalWrite(IN1,LOW); 
 digitalWrite(IN2,HIGH);

 // Direction du Moteur B
 // NB: en sens inverse du moteur A
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
  pinMode(LED_BUILTIN, OUTPUT); // TO TEST - DELETE LATER
  pinMode(7,INPUT); // TO TEST - DELETE LATER
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {
  // Get commands from Serial
  if (Serial.available())  {
    char c = Serial.read();
    
    if (c == ',') {      
      if(readString.indexOf("NA") >=0) {
        readString=readString.substring(2);
        speed = readString.toInt();
      }
      if(readString.indexOf("DI") >=0) {
        readString=readString.substring(2);
        number_Balls = readString.toInt();
      }
      if(readString.indexOf("RE") >=0) {
        readString=readString.substring(2);
        effect = readString.toInt();
      }
      
      readString="";

    }  
    else {     
      readString += c;
    }
  }
  // ENDS HERE
 
  // Get numbers of balls in stock and display it
  balls_Stock = EEPROM.read(0);
  if(digitalRead(7)==HIGH){
    delay(200);
    balls_Stock++;
    EEPROM.update(0, balls_Stock);
    Serial.println(balls_Stock);
  }
  //Display it on Start
  if(readString == "limiter"){
    balls_Stock = EEPROM.read(0);
    Serial.println(balls_Stock);
    readString="";
  }
  // ENDS HERE

  //Do the precise function
  if(readString == "jobDone"){
    //Serial.println(speed); // WRITE A DC MOTOR CODE
    //Serial.println(effect); // DC MOTOR CODE 
    while( counter < number_Balls ){
      analogWrite(ENA,255);
      analogWrite(ENB,128);
      CheckStop();
      if (millis() - previousMillis >= interval) {
        previousMillis = millis();
        if(servo_Controller == 0){
          pos = 0;
        }
        if(servo_Controller == 1){
          pos = 90;
          servo_Controller = 0;
          counter++;
          balls_Stock--;
          CheckStop();
          Serial.println(balls_Stock);
          CheckStop();
        }
        CheckStop();
        myservo.write(pos);
        if( pos == 0){
          servo_Controller = 1;
        }
        myservo.write(180);
        delay(2000);
      }
    }   
    counter = 0; 
    EEPROM.update(0, balls_Stock);
    readString="";
  }  
  //ENDS HERE

  // Control of servo in Precise Layout
  if (readString == "left") {
    position = position + 10;
    if(position < 10){
      position = 0;  
    }
    if(position > 180){
      position = 180;   
    }
    ervo_direction.write(position);
    readString="";
  }

  if (readString == "right") {
    position = position - 10;
    if(position < 10){
      position = 0;  
    }
    if(position > 180){
      position = 180; 
    }
    ervo_direction.write(position); 
    readString="";
  }
  // ENDS HERE
}

//Stop ALL
void CheckStop(){
  if (Serial.available())  {
    char c = Serial.read();  
    stopString += c;
  }
  if (stopString == "stop") {
    myservo.write(90);
    Serial.println("nadir");
    counter = number_Balls*2;
    analogWrite(ENA,0);
    analogWrite(ENB,0);
    stopString="";
  }
}
//ENDS HERE

i am using a HS422 servo and a tower pro micro servo and 2 dc motors : 457RE540 both servos are getting power from Arduino and i am not really sure of the name of the power supply . But we have one on the labo so i guess it must be working fine (as far as i know). When i apply the power and send the command to get into that loop some time the servo move to 90 and nothing after that happened and i can t hear any noise from the dc motors .
P.S : i am still new to arduino i will try to improve my code

Power problems it is!

540 size motors use far more current than an L298N can safely supply. You need a much better motor driver. Pololu have a good range. I'd look for one capable of around 10A, maybe more.

And servos should never be powered from the Arduino 5V pin, particularly not large servos like the HS422. They don't work well and can damage the Arduino. Get a 12V to 5V 3A (or more) step-down converter and run the servos from that.

BTW You have never even run that code you just posted. You never attach() the second servo and you keep writing to "ervo_direction" so it will not even compile.

Steve

slipstick:
BTW You have never even run that code you just posted. You never attach() the second servo and you keep writing to "ervo_direction" so it will not even compile.

Seems to me that the OP just wants to waste everyone's time - including his own.

...R