Control 2 DC Motors via Bluetooth and Arduino



Control 2 DC Motors via Bluetooth and Arduino Tutorial: In this tutorial, you will learn how to control speed and direction of dc motor using Arduino. I have already posted a project on controlling dc motor speed using pic microcontroller. This tutorial is also same like that expect we are using Arduino and Bluetooth module to control speed and direction of dc motor.  It is a very simple project in this project we learned how to control the DC motor, a direction of the motors, communication between motors, and Smartphone this is done by using Bluetooth module HC-05, that attached to Arduino UNO.



Components Required:

  • Arduino Uno: It is easy to use and easy to interface the different devices due to its digital pins. Here we interface Bluetooth module and DC motor at the same time.
  • 2 DC Motors
  • L293D Motor Driver: Directly connect the motor to Arduino is not a good because motor requires more current that is less which Arduino provides. So to solve this problem a motor driver is used which is connected between DC motor and Arduino.
  • Bluetooth Module HC-05

Circuit Diagram:dc motor speed and direction control using bluetooth and Arduino

Connection:



  • 5V and ground of the IC to 5V and ground of Arduino.
  • Motor 1 pins connect the IC pins 3 and 6.
  • Motor 2 pins connect the IC pins 11 and 13.
  • Input1 and input2 pins of IC connect 3 and 4 pins of Arduino respectively.
  • Input3 and input4 pins of the IC connect 8 and 9 pins of Arduino respectively
  • Enable1 and Enable2 connect the 2 and 11 pins of
  • Bluetooth pin Vcc and GND connect to Vcc and GND of Arduino directly, and RX and TX pins connected to TX and RX of Arduino respectively.

Working

First of all, we connect the Bluetooth module to our Smartphone. Then according to coding instruction, we sent the message. 1st then we send the message 1 the message is the motor is going forward; motor spin if we want to stop the motor so, send the message to 3 motors is stop. If we want to turn the motor left or reversed sent the messages 2 and 5 motor is turn left and reversed respectively.

Code

int motor1Pin1 = 3; // pin 2 on IC

int motor1Pin2 = 4; // pin 7 on IC

int enable1Pin = 2; // pin 1 on IC

int motor2Pin1 = 8; // pin 10 on IC

int motor2Pin2 = 9; // pin 15 on IC

int enable2Pin = 11; // pin 9 on IC

int state;

int flag=0;       

int stateStop=0; 

void setup() {

    // sets the pins as outputs:

    pinMode(motor1Pin1, OUTPUT);

    pinMode(motor1Pin2, OUTPUT);

    pinMode(enable1Pin, OUTPUT);

    pinMode(motor2Pin1, OUTPUT);

    pinMode(motor2Pin2, OUTPUT);

    pinMode(enable2Pin, OUTPUT);

    // sets enable1Pin and enable2Pin high so that motor can turn on:

    digitalWrite(enable1Pin, HIGH);

    digitalWrite(enable2Pin, HIGH);

    // initialize serial communication at 9600 bits per second:

    Serial.begin(9600);

}

void loop() {

    //Start Serial Communication

    if(Serial.available() > 0){    

      state = Serial.read();  

      flag=0;

    }  

    // if the state is '1' the DC motor will go forward

    if (state == '1') {

        digitalWrite(motor1Pin1, HIGH);

        digitalWrite(motor1Pin2, HIGH);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, HIGH);

        if(flag == 0){

          Serial.println("Go Forward!");

          flag=1;

        }

         delay(3000);

        state=3;

        stateStop=1;

    }

    // if the state is '2' the motor will turn left

    else if (state == '2') {

        digitalWrite(motor1Pin1, HIGH);

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Turn LEFT");

          flag=1;

        }

        delay(3000);

        state=3;

        stateStop=1;

    }

    // if the state is '3' the motor will Stop

    else if (state == '3' || stateStop == 1) {

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("STOP!");

          flag=1;

        }

        stateStop=0;

    }

    // if the state is '4' the motor will turn right

    else if (state == '4') {

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, LOW);

        digitalWrite(motor2Pin1, LOW);

        digitalWrite(motor2Pin2, HIGH);

        if(flag == 0){

          Serial.println("Turn RIGHT");

          flag=1;

        }

        delay(3000);

        state=3;

        stateStop=1;

    }

    // if the state is '5' the motor will Reverse

    else if (state == '5') {

        digitalWrite(motor1Pin1, LOW);

        digitalWrite(motor1Pin2, HIGH);

        digitalWrite(motor2Pin1, HIGH);

        digitalWrite(motor2Pin2, LOW);

        if(flag == 0){

          Serial.println("Reverse!");

          flag=1;

        }

         delay(3000);

        state=3;

        stateStop=1;

    }}

}



Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,395 other subscribers