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 a HC-05 bluetooth module.

The Arduino will receive the speed (a number between 0 and 255) from Serial Monitor or from HC-05 module, and using the digital output and a transistor will controll the DC motor speed.
The motor will be powered from a 9v battery and the Arduino will be powered from 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 controled by an Arduino digital pin. Using the transistor we can control how much current the DC motor will receive.

Materials i 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 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 common ground between Arduino and te 9v battery.

Placing transistor with the flat side towards you, the pins are:
1 ground of 9v battery and to arduino
2 connect to pin 3 on Arduino through a 270ohm resistor
3 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 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 smartphone, to the Arduino Rx pin.
After power 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 schetch to Arduino Uno. Make sure that nothing is connected to 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 shoud start spinning at 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 at  https://play.google.com/store/apps/details?id=jp.side2.apps.btterm   .

Before using the App, you shoud 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 to the bluetooth module. After the connection was made, the yellow led will blink slowly and the green led lights up.
In order to send speed, type a number, press enter (new line) and then press Send button.

Leave a Reply

Your email address will not be published. Required fields are marked *