BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

DC Motor Control


#1

I’m having trouble controlling my dc motor from my phone. I can control a servo just fine, but the dc motor will not spin. I am using an Arduino Uno, Adafruit v2 motorshield, and HM-10 Bluetooth module. I know it has to be the code that is wrong since I have everything wired properly and that I can control a servo which proves that the communication between my phone and Bluetooth works fine. If anyone can help narrow down what might be wrong with my code that would be great. Thanks!

#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS=Adafruit_MotorShield();
Adafruit_DCMotor *motor1=AFMS.getMotor(1);
Servo servo;

SoftwareSerial SwSerial(10, 11); // RX, TX
SoftwareSerial SerialBLE(10, 11);

char auth[] = "d651b25ee9394d8fb295690a7317a317";

void setup() {
  Serial.begin(9600);
  delay(1000);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE,auth);// switched inside ()

  Serial.println("Waiting for connections...");
  Serial.println("Adafruit Motorshield v2 - DC Motor");
  AFMS.begin();
  motor1->setSpeed(255);
  servo.attach(7);
}

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

BLYNK_WRITE(V7){
  servo.write(param.asInt());
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue == 1) {
    motor1->run(FORWARD);
  }
  if(pinValue == 0) {
    motor1->run(RELEASE);   
  }
}

#2

Hello

As per the directions you deleted in order to post this… I fixed your posted code formatting :wink:

This is not really a code fixit shop :stuck_out_tongue: but have you tested those motor commands without Blynk… say using a loop or physical button to process a similar type of function?

Do you even have the Motor Shield configured properly???

And how can you use the same pins for two serial devices?

I think your issues are totally non-Blynk related… get it working without Blynk first, then add in the Blynk control of the tested and working functions.


#4

I have yet to get the motors to work any other way other than connecting them to the breadboard, which makes them rotate. I’ve watched a lot of tutorial videos and researched how to set up the Adafruit, and I think that is fine. The soldered connections are fine and the power light is on. I am able to control a servo from the motorshield as well so I figure it must come down to the coding for the dc motors.

I deleted the SoftwareSerial SwSerial(10,11) in the code as well. I have my Bluetooth module connected to the 10 and 11 pins. What else could I do? I think other parts of the code look fine what say you?


#5

As your issue is not directly Blynk related (Blynk can’t “control” that shield, the associated library does), I think I have already made a valid suggestion :wink:

PS Servo and DC motor control are totally separate functions… even if on the same shield.


#6

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

Ok, so I am trying to create a little vehicle. 2 dc motors will drive the vehicle. 1 dc motor will engage a conveyor belt system and a servo will be used to raise and lower the conveyor belt system. I have connected to my HM-10 Bluetooth module from my phone and have been able to control the servo with it. I am having trouble controlling the dc motors that are connected to an Adafruit v2.3 motorshield. The dc motors spin when I upload a test code that varies their speed and direction, but I can’t control them with my phone. Take a look at my code and see if there’s anything wrong in there. I’m confident that my wiring is done correctly. At this point, I’m just not sure what’s wrong with it but it has to be Blynk related (or so I am thinking) or the code associated with it. Any advice would be great thanks!


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Servo.h>
#define BLYNK_PRINT Serial

Adafruit_MotorShield AFMS=Adafruit_MotorShield();

Adafruit_DCMotor *motor1=AFMS.getMotor(1);
Adafruit_DCMotor *motor2=AFMS.getMotor(2);
Adafruit_DCMotor *motor3=AFMS.getMotor(3);

Servo servo1;

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();
  servo1.attach(6);
  motor3->setSpeed(100); //digging arm speed test number
}

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

//LEFT MOTOR FORWARD
BLYNK_WRITE(V1) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(FORWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR FORWARD
BLYNK_WRITE(V2) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(FORWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//LEFT MOTOR BACKWARDS
BLYNK_WRITE(V3) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(BACKWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR BACKWARDS
BLYNK_WRITE(V4) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(BACKWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//MOTOR SPEED
BLYNK_WRITE(V5) {
  int pinValue=param.asInt();
  Serial.print("Motor Speed: ");
  Serial.println(pinValue);
  
  motor1->setSpeed(pinValue);
  motor2->setSpeed(pinValue);
}

//SERVO UP DOWN
BLYNK_WRITE(V6) {
  servo1.write(param.asInt());
 }

//DIG FORWARD
BLYNK_WRITE(V7) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//DIG BACKWARDS
BLYNK_WRITE(V8) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);  
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//EMERGENCY STOP
BLYNK_WRITE(V9) {
  int pinValue=param.asInt();
  Serial.print("Emergency Stop: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(RELEASE);
    motor2->run(RELEASE);
    motor3->run(RELEASE);
  }
  //if(pinValue==0) {
   // motor3->run(RELEASE);    
  //}
}


#7

Please have a look at the following example as you’re missing some bits https://examples.blynk.cc/?board=Arduino%20Uno&shield=HM10%20or%20HC08&example=GettingStarted%2FBlynkBlink


#8

I added the few parts to my code from the example that you linked me to and still not working. Also, now I have both SoftwareSerial SwSerial(10,11) and SoftwareSerial SerialBLE(10,11) as it says to in the example sketch. However, this is also the part the Gunner told me I need to change, so I’m a little confused there.

Also, now I am trying to connect to my HM-10 from my phone and it’s not doing it even though I was never having issues with this before. It says “Can’t connect. Please retry or choose another device.” If I try to connect and while it says “Connecting” I hit ok in the top left and then go back to try to connect it says that I am connected. Yet, when I hit the play button it immediately says that I am not connected.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Servo.h>
#define BLYNK_PRINT Serial

SoftwareSerial SwSwerial(10,11);
SoftwareSerial SerialBLE(10,11);

Adafruit_MotorShield AFMS=Adafruit_MotorShield();

Adafruit_DCMotor *motor1=AFMS.getMotor(1);
Adafruit_DCMotor *motor2=AFMS.getMotor(2);
Adafruit_DCMotor *motor3=AFMS.getMotor(3);

Servo servo1;

void setup() {
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();
  servo1.attach(6);
  motor3->setSpeed(100); //digging arm speed test number
}

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

//LEFT MOTOR FORWARD
BLYNK_WRITE(V1) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(FORWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR FORWARD
BLYNK_WRITE(V2) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(FORWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//LEFT MOTOR BACKWARDS
BLYNK_WRITE(V3) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(BACKWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR BACKWARDS
BLYNK_WRITE(V4) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(BACKWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//MOTOR SPEED
BLYNK_WRITE(V5) {
  int pinValue=param.asInt();
  Serial.print("Motor Speed: ");
  Serial.println(pinValue);
  
  motor1->setSpeed(pinValue);
  motor2->setSpeed(pinValue);
}

//SERVO UP DOWN
BLYNK_WRITE(V6) {
  servo1.write(param.asInt());
 }

//DIG FORWARD
BLYNK_WRITE(V7) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//DIG BACKWARDS
BLYNK_WRITE(V8) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);  
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//EMERGENCY STOP
BLYNK_WRITE(V9) {
  int pinValue=param.asInt();
  Serial.print("Emergency Stop: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(RELEASE);
    motor2->run(RELEASE);
    motor3->run(RELEASE);
  }
  //if(pinValue==0) {
   // motor3->run(RELEASE);    
  //}
}
``

#9

Woops added another 2 lines of code I forgot my auth code somehow.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Servo.h>
#define BLYNK_PRINT Serial

SoftwareSerial SwSwerial(10,11);
SoftwareSerial SerialBLE(10,11);

Adafruit_MotorShield AFMS=Adafruit_MotorShield();

Adafruit_DCMotor *motor1=AFMS.getMotor(1);
Adafruit_DCMotor *motor2=AFMS.getMotor(2);
Adafruit_DCMotor *motor3=AFMS.getMotor(3);

Servo servo1;

char auth[]="*******";

void setup() {
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");
  Blynk.begin(SerialBLE,auth);
  AFMS.begin();
  servo1.attach(6);
  motor3->setSpeed(100); //digging arm speed test number
}

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

//LEFT MOTOR FORWARD
BLYNK_WRITE(V1) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(FORWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR FORWARD
BLYNK_WRITE(V2) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(FORWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//LEFT MOTOR BACKWARDS
BLYNK_WRITE(V3) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(BACKWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR BACKWARDS
BLYNK_WRITE(V4) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(BACKWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//MOTOR SPEED
BLYNK_WRITE(V5) {
  int pinValue=param.asInt();
  Serial.print("Motor Speed: ");
  Serial.println(pinValue);
  
  motor1->setSpeed(pinValue);
  motor2->setSpeed(pinValue);
}

//SERVO UP DOWN
BLYNK_WRITE(V6) {
  servo1.write(param.asInt());
 }

//DIG FORWARD
BLYNK_WRITE(V7) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//DIG BACKWARDS
BLYNK_WRITE(V8) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);  
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//EMERGENCY STOP
BLYNK_WRITE(V9) {
  int pinValue=param.asInt();
  Serial.print("Emergency Stop: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(RELEASE);
    motor2->run(RELEASE);
    motor3->run(RELEASE);
  }
  //if(pinValue==0) {
   // motor3->run(RELEASE);    
  //}
}