Control DC Motor via Bluetooth

  Arduino Uno R3Arduino Uno R3 Microcontroller

  16.00 JD                  Add to cart

Arduino Mega 2560
Arduino Mega 2560 Microcontroller Board

  23.00 JD                  Add to cart

  Arduino Leonardo

Arduino Leonardo

  17.00 JD                  Add to cart

 

In this project we will control a DC motor with a Smartphone via Bluetooth. This project is great to learn more about:

  • DC motors
  • Interfacing Arduino with your Smartphone
  • Bluetooth
  • L293D IC

 

Parts required

  • 1x Arduino
  • 1x Bluetooth Module (for example: HC-05 or 06
  • 1x Smartphone (any Android will work)
  • BlueTerm application
  • 1x L293D IC
  • 1x DC motor
  • 1x Breadboard
  • Jumper Cables

 

Schematics

Note

You can only connect upload the code. TX  and  RX cables to your Arduino, after you upload the code

Two common mistakes

1. You need to remove the RX and TX cables when you're uploading the sketch to your Arduino.

2. Sometimes people connect the TX from the Bluetooth module to the TX of the Arduino...

that's wrong and it won't work. Make sure you connect it properly, the TX into RX and the RX into the TX.

Note

If the HC-05 Bluetooth Module asks for a password, it’s '1234'.

Upload the code


 

/*

* Control DC motor with Smartphone via bluetooth

*/

int motorPin1 = 3; // pin 2 on L293D IC

int motorPin2 = 4; // pin 7 on L293D IC

int enablePin = 5; // pin 1 on L293D IC

int state;        

int flag=0; //makes sure that the serial only

prints once the state 

void setup() {  

// sets the pins as outputs: 

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

// sets enablePin high so that motor can turn on:

digitalWrite(enablePin, HIGH);

// initialize serial communication at 9600 bits per

second: Serial.begin(9600);

}

void loop() {

//if some date is sent, reads it and saves in state

if(Serial.available() > 0){

state = Serial.read();

flag=0;

}

// if the state is '0' the DC motor will turn off

if (state == '0') {

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low

digitalWrite(motorPin2, LOW); // set pin 7 on L293D low

if(flag == 0){

Serial.println("Motor: off");

flag=1;

}

}

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

else if (state == '1') {

digitalWrite(motorPin1, LOW); // set pin 2 on

L293D low

digitalWrite(motorPin2, HIGH); // set pin 7 on

L293D high

if(flag == 0){

Serial.println("Motor: right");

flag=1;

}

}

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

else if (state == '2') {

digitalWrite(motorPin1, HIGH); // set pin 2 on

L293D high

digitalWrite(motorPin2, LOW); // set pin 7 on

L293D low

if(flag == 0){

Serial.println("Motor: left");

flag=1;

}

}

}

For the communication between the Smartphone and the Bluetooth module use the BlueTerm app. It's completely free, so you just need to go to “Play store” and download it. Then you just need to connect your Smartphone with the Bluetooth module.