Need help with controlling DC motors via USB and Arduino UNO

It seems to be the code is the problem,because I can switch on/off an LED via USB normally.But when I tried to control the DC motors, I have got no response.Here is the code that I used.


#define BLYNK_PRINT SwSerial


#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Token";

#define m11 5
#define m12 9
#define m21 10
#define m22 6

void forward()
{
  digitalWrite(m11, HIGH);
  digitalWrite(m12,LOW);
  digitalWrite(m21,HIGH);
  digitalWrite(m22,LOW);
}

void backward()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,HIGH);
  digitalWrite(m21,LOW);
  digitalWrite(m22,HIGH);
}

void right()
{
  digitalWrite(m11, HIGH);
  digitalWrite(m12,LOW);
  digitalWrite(m21,LOW);
  digitalWrite(m22,LOW);
}

void left()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,LOW);
  digitalWrite(m21,HIGH);
  digitalWrite(m22,LOW);
}

void Stop()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,LOW);
  digitalWrite(m21,LOW);
  digitalWrite(m22,LOW);
}

void setup()
{
  // Debug console
  SwSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  pinMode(m11, OUTPUT);
  pinMode(m12, OUTPUT);
  pinMode(m21, OUTPUT);
  pinMode(m22, OUTPUT);
  
}

BLYNK_WRITE(V1) {
  int x = param[0].asInt();
  int y = param[1].asInt();

  /* Do something with x and y
  SwSerial.print("X = ");
  SwSerial.print(x);
  SwSerial.print("; Y = ");
  SwSerial.println(y);*/

   if(y>220){
  forward();
   }
  else if(y<35){
  backward();
  }
  else if(x>220){
  right();
  }
  else if(x<35){
  left();
  }
  else{
  Stop();
  }
}

void loop()
{
  Blynk.run();
} 

so to clarify: if you attach a led to any of those digital pins then they act correctly, but it you connect a DC motor it doesn’t? Have you connected the DC motor directly to a 5V output of the arduino (to test it) and did it run ?

NO!! don’t even test this!! you will kill your pin at the least, most probably the whole board.

I suspect the OP has a motor controller already connected to those respective pins, which are actually controlling two of 4 mosfets or something in an H-Bridge configuration. And on that note, setting the wrong two pins HIGH can create a dead short in the motor controller… so double/triple check.

@azrahakimi this is NOT a Blynk specific issue… but if you search this forum for key words like rover, car, robot, motor, etc… you will find other examples of how this can work.

I’ve figured it out. Seems that even if I connect a USB to my board (which did powered up my LED via a USB,so I assumed it powered my DC motors too) but it didn’t. I still have to put the batteries on and at the same time connect the USB to successfully control my DC motors.Silly mistakes :smiley: but thanks everyone for your comment.

Glad you figured it out.

I am closing this topic as it was not Blynk related in the first place.