One DC motor command only runs when Arduino is connected to pc

I used the Arduino Uno R3 board. When it receives a Bluetooth signal, it can move forward, reverse, left, or right. The forward and reverse work well. The turn left and right only work when I plug it into PC.

forward and reverse make two DC motors work. turn left and right make one work. So I think there is no matter about power. Here's my code.

#include <Servo.h>
Servo sm = Servo();


#include <Ultrasonic.h>
Ultrasonic u = Ultrasonic(11, 12); //trig,echo


void setup() {
  // put your setup code here, to run once:
 

  pinMode(3, OUTPUT); //ENA 0-255(speed)
  pinMode(4, OUTPUT); //IN1
  pinMode(5, OUTPUT); //IN2
  analogWrite(3 , 250);

  pinMode(6, OUTPUT); //ENB 0-255(speed)
  pinMode(9, OUTPUT); //IN3
  pinMode(8, OUTPUT); //IN4
  analogWrite(6 , 250);

  pinMode(7, OUTPUT);//LED

 sm.attach(2);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  int x = Serial.read();

  //FORWARD
  if (x == '1') {

    sm.write(90);
    delay(300);
    int d = u.read();
    if (d > 15) {
      digitalWrite(7, LOW);

      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      delay(300);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(9, LOW);
      digitalWrite(8, LOW);
    }

    if (d < 15) {
      digitalWrite(7, HIGH);

    }

  }

  //REVERSE
  if (x == '2') {

    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    delay(300);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
  }


  //TURN RIGHT
  if (x == '3') {
    sm.write(30);
    delay(300);
    int d = u.read();

    if (d > 15) {

      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      delay(300);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(9, LOW);
      digitalWrite(8, LOW);
    }

    if (d < 15) {
      digitalWrite(7, HIGH);

    }
    sm.write(90);

  }

  
  //TURN LEFT
  if (x == '4') {
    sm.write(120);
    delay(300);
    int d = u.read();
    if (d > 15) {
      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(9, LOW);
      digitalWrite(8, LOW);
      delay(300);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(9, LOW);
      digitalWrite(8, LOW);
    }

    if (d < 15) {
      digitalWrite(7, HIGH);

    }

    sm.write(90);

  }

}

Hi, @techo22
Welcome to the forum.
Thanks for using code tags. :+1:

Can you please post a copy of your circuit diagram?

How are you powering your project when the PC is not connected?

What are you using to drive/control your motors? (link to specs/data).

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

how did you wire the BT receiver? (I assume on pin 0 and 1) when you plug to the PC, do you open the Serial monitor?

How did you wire and power the Servo?

How do you power the system when not connected to USB?

PS: side note, to make the code easier to read, create functions for each direction so that the loop could be

void loop() {
  switch (Serial.read()) {
    case '1': forward();    break;
    case '2': reverse();    break;
    case '3': turnRight();  break;
    case '4': turnLeft();   break;
    default: break;
  }
}

You perform Serial.read without checking if Serial.avalable.
In the code there is "if ( d > 15)" and "if( d < 15)". What if d == 15?

that's OK.

Serial.read() returns -1 if there is nothing to read and thus is ignored in all the if() tests (or my switch).

I never really understood why all the examples insist on using available() and thus an expensive calculation (with a modulo etc) just to know if there is something to deal with... Just read the port, if there is something, deal with it, if not then read again...

I power the L298N motor driver module from two 18650 batteries (3.7V) and give power to the Arduino board from it.

Yes, I wired the BT receiver to pin 1. and I didn't open the serial monitor.
I powered the L298N motor driver module from two 18650 batteries (3.7V) and give power to the Arduino board from its output. I wired Servo to pin 2 in Arduino board and power it also from the driver module.

did you join the GND of all the power supplies ?

Yes I do

where is that plugged? do you use some sort of voltage regulation?

a drawing of your setup / wiring would help

I wired the Vin pin of the Arduino board to the 5V output pin in the motor driver module.

It's working properly without Servo code lines.

Considering the voltage drop of 2V in the driver, if you are using 5V motors you’ll need to provide 7V at minimum

your two 18650 batteries might not be enough

I assume your module has an on-board (78M05 ?) 5V regulator which is enabled or disabled through a jumper. Do you have the jumper? (with it, the 5V regulator is live and supplying logic power supply (Vss) from the motor power supply (Vs). In this case the 5V pin acts as an output pin and can delivers 5V up to 0.5A).

Hi,

Vin is the same as the DC Socket on the UNO.
You need to connect the 5V supply pin on the driver board to the 5V pin of the UNO.

Can you please post picture(s) of your project and tell us WHICH L298N driver board you have, there is more than one.

PLEASE ARE YOU USING A UNO?
PLEASE POST LINK TO SPECS/DATA OF DRIVER PCB?
PLEASE POST A CIRCUIT DIAGRAM.

Do you have a DMM?

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

Thanks. The problem was that I wired Power to Vin in Arduino board. changed it to a 5V pin and it worked.

18650s may have a voltage range between 2.5 volts and 4.2 volts with a nominal voltage of 3.7V

As long as you stay above 7V you should be fine but if the batteries run a bit low you'll experience probably some issues

Hi,

Good but it would be nice for you to post a schematic, you were asked for a circuit diagram back in post #2, so you could have had your proplem solved 14 posts ago.

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

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