A motor start working crazy when I turn on my robot

I'm developing an object avoiding robot with Arduino UNO board, two motors DC 3-6V, an ultrasonic sensor HC-SRO4, and the driver l298n, all these supplied with 3 lithium batteries of 3.7V each (11,1V in total). I've programmed the code and it works perfectly. The problem is that when I turn on the battery and the robot turns on, before starting with the code that I uploaded it, the motor on the left starts working forward crazy for about 5 seconds. Do you know how to solve it? I would be really grateful if you could help me, every contribution is perfect. Thanks.

Here's the code I'm using, just in case the error is there:

const int pinTrigger=6;
const int pinEcho=5;
long distance=0;
long time =0;


 const int IN1 = 13;
 const int IN2 = 12;
 const int IN3 = 11;
 const int IN4 = 10;
 const int ENA = 3;
 const int ENB = 9;


void setup() {
  
  pinMode(pinTrigger,OUTPUT); 
  pinMode(pinEcho,INPUT);

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

}

void loop() {
  
  mesure();
    if(distance < 19) {

      obstacle();
    }
    if(distance > 18) {
      
      forward();
    }

}

void mesure() {
  digitalWrite(pinTrigger,LOW);
  delayMicroseconds(2);
  digitalWrite(pinTrigger,HIGH);
  delayMicroseconds(10);  
  digitalWrite(pinTrigger, LOW);
  time = pulseIn(pinEcho, HIGH);   
  distance = time/29/2 ;
}

void forward(){

  digitalWrite(IN1, LOW);                
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);                                
  digitalWrite(IN4, HIGH);  
  analogWrite(ENA, 90); 
  analogWrite(ENB, 90); 

}

void right (){

    digitalWrite(IN1, HIGH);                
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);                                
    digitalWrite(IN4, LOW);  
    analogWrite(ENA, 150); 
    analogWrite(ENB, 200);

}

void stop(){

    digitalWrite(IN1, LOW);                
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);                                
    digitalWrite(IN4, LOW);  
    analogWrite(ENA, 0); 
    analogWrite(ENB, 0);

}

void obstacle (){
  stop();
  delay(300);
  right();
  delay(500);
  stop();
  delay(100);  
}

What happen if you turn the motors off in the setup function?

Also, slow down your ultrasonic ping rate.

That may be a pin state left over from the bootload or initialization stage. Set all the motor pins to sensible values in setup() and see if that helps.

When an Arduino boots up, all of the digital pins are floating and can cause problems like this. Any noise from the AC line or capacitive coupling can make these pins drift around. Take a 10K resistor and connect it between the pin and either +5V or GND. An arrangement like this should solve the problem.

1 Like

Hi.

I've tried this, but it still doesn't work.

void setup() {
  
  pinMode(pinTrigger,OUTPUT); 
  pinMode(pinEcho,INPUT);

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

    digitalWrite(IN1, LOW);                
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);                                
    digitalWrite(IN4, LOW);  
    analogWrite(ENA, 0); 
    analogWrite(ENB, 0);
    delay(200);

}

Sorry, I'm new in programming, how can I slow down the ultrasonic ping rate and how it affects that?

Sorry, I'm new in programming. I've searched what you're saying, but I'm not getting anything, how can I set all the motors pins to sensible values?

As far as I know, if I connect the pin of the motor with a resistor directly to 5V o GND, the Arduino UNO board will burn. The (IN, IN2, IN3, IN4) motor pins are connected to digitial Pins in the Arduino Board. What do you really mean?

Do not power motors from the Arduino 5V.

It is a good idea to do what @proxy303 suggests in post #4: connect digital pins to power or ground, via a 10K resistor.

how can I slow down the ultrasonic ping rate

put a delay() between ping function calls, or use the millis() timer to schedule the events, as described here: https://www.baldengineer.com/blink-without-delay-explained.html

I've tried this, and it doesn't work. Here's a photo, just in case I did something wrong. (I did it while the board was connected to the computer, and I did it with all the digital pins).

It's nothing to do with the motors going wild.
The sensor you're using can't distinguish between the pings it sends out, so an echo from a distant object could be mistaken for an echo from a nearby object, if one ping follows-on too closely after another.
You should limit the ping rate to no more than about 20 to 30 pings per second, depending on the environment, to allow distant echoes to die away.

Do not change any wiring while the power is on. That is a great way to destroy an Arduino, and possibly also burn out the USB port on your computer.

I've read the link that @jremington sent, and I don't understand how can I link these commands with my ultrasonic sensor. If someone can help me with the commands, I would appreciate it.

Post the code you tried, using code tags, and explain where the difficulty seems to be.

This looks correct. Now connect the motor driver pin to row 23 on the breadboard where the resistor is.

Never connect the motor directly to the arduino! This can kill it!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.