Arduino - Control DC Motor Via Bluetooth | Random Nerd Tutorials

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

If you don’t have the L293 IC you can make the same circuit using the H bridge, anyway I really recommend you to read more about that and the IC datasheet. There’s plenty of tutorials about that. Also If you don’t have a bluetooth module yet you can see my review about this bluetooth module and where to buy one. So Let’s start…

Parts required

IMG_0244

  • Arduino UNO – read Best Arduino Starter Kits
  • 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

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

I can only TX and RX cables after you upload the Arduino code.

schematics

Two common mistakes

  • You need to remove the RX and TX cables when you’re uploading the sketch to your Arduino.
  • 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 this code below

Make sure you remove the wires from RX and TX otherwise the code won’t upload properly! /* * Control DC motor with Smartphone via bluetooth * created by Rui Santos, https://randomnerdtutorials.com */ 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; } } }

View raw code

For the android communication with our bluetooth module I’ve used 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 smarthphone with the bluetooth module. Remember to remove the TX and RX cables. (you can see in youtube video below how that’s done).

I’ve only set 3 commands to control the DC motor:

  • ‘0’ – Turns off the DC motor
  • ‘1’ – DC motor rotates to right
  • ‘2’ – DC motor rotates to left

Watch the video demonstration

Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.

Tag » Arduino - Control 4 Dc Motor Via Bluetooth Code