Control a DC motor using an Arduino, a transistor and a HC-05 bluetooth module

We will control a DC motor using an Arduino, a transistor and an HC-05 Bluetooth module.

The Arduino will receive the speed (a number between 0 and 255) from the Serial Monitor or HC-05 module, and using the digital output and a transistor will control the DC motor speed.
The motor will be powered from a 9v battery and the Arduino will be powered from a USB cable. We cannot connect the motor directly to an Arduino pin because the motor can draw too much current for Arduino to handle.
The transistor is used as a switch controlled by an Arduino digital pin. Using the transistor we can control how much current the DC motor will receive.

Materials used:

  • Arduino UNO
  • dc motor
  • HC-05 Bluetooth module
  • transistor bc337 / 2N3904 (2N3904 / BC547 / PN2222 / 2N4401 / NTE123AP
  • 9v battery
  • 270 Ohm resistor
  • 1N4001 diode
  • breadboard
  • jumper wires

After you gather all the materials, make the connections on the breadboard according to the Fritzing scheme that i attached on this article.

Because the Arduino and the DC motor has different power sources, we will need to have a common ground between Arduino and the 9v battery.

Placing the transistor with the flat side towards you, the pins are:

  1. first pin: ground – connect it to 9v battery andArduino
  2. second pin: connect it to pin 3 on Arduino through a 270 Ohm resistor
  3. third pin: connect it to the motor ground

Place the 1N4001 diode between the ground and the positive connectors of the DC motor. The marked side of the 1N4001 diode should be towards the positive input.

Connect the VCC and the ground pins of the HC-05 Bluetooth module to 3.3v and ground pins of Arduino respectively.
Do not connect the Tx pin of module tor the Arduino for now. We do not need a special library for the HC-05 module because we will only use it to forward data received from a smartphone, to the Arduino Rx pin.
After powering the HC-05 Bluetooth module, the yellow led will blink very fast because it is not paired with the smartphone.
Open the Arduino Studio and upload the following sketch to Arduino Uno. Make sure that nothing is connected to the Rx pin.

int motorPin = 3;
String inString = ""; 
int speedm = 0;
int rx = 0;
char inSerial[15];
void setup()
{
  pinMode(motorPin, OUTPUT);
  pinMode(rx, INPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
}

void loop()
{
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }
    if (inChar == '\n') {
//      Serial.println(inString);
      int speed = inString.toInt();
      if (speed >= 0 && speed <= 255)
      {
        speedm = speed;
      }
      inString = "";
    }
  }
  analogWrite(motorPin, speedm);
}

Set the Serial Monitor speed at 9600 baud and use it to test the project.
The sketch will prompt you, to enter a value between 0 and 255 in the Serial Monitor, and after you typed the number, the motor should start spinning at the required speed.
After successful testing with the Serial monitor, we will try to send the desired speed using a smartphone, therefore we will connect the Tx of HC-05 module to Rx of Arduino Uno.
I used an Android phone and the “S2 Terminal for Bluetooth Free” App. You can find it o0r similar apps on Google Play.

Before using the App, you should go to phone settings and pair the phone with the HC-05 Bluetooth module. If the phone requests a password, just type “1234”.
Next, open the S2 Terminal App and connect it to the Bluetooth module. After the connection was made, the yellow led will blink slowly and the green led lights up.
To send speed, type a number, press enter (new line) and then press the Send button.