top of page

Projects

Search

Controlling DC motor with Bluetooth

  • Writer: Mohamed Khaled
    Mohamed Khaled
  • Oct 16, 2018
  • 3 min read

Updated: Oct 23, 2018

Task objective:

It is required to control the movement of a DC motor with Bluetooth chip, by sending a serial signal to control the direction of the motor.

Component of the circuit:

1. Arduino.

2. DC motor.

3. Bluetooth HC-05.


What is Bluetooth HC-05?


HC-05 Bluetooth Module is an easy to use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. Its communication is via serial communication which makes an easy way to interface with controller or PC. HC-05 Bluetooth module provides switching mode between master and slave mode which means it able to use neither receiving nor transmitting data.


Specification:

1.Model: HC-05.

2. Input Voltage: DC 5V.

3. Communication Method: Serial Communication.

4. Master and slave mode can be switched.


Pin Definition:



The Connection of arduino with Bluetooth:


1. connect with male-female jumper, the VCC of the Bluetooth with the 5V pin in the Arduino.

2. connect with male-female jumper, the GND of the Bluetooth with the GND pin in the Arduino.

3. connect with male-female jumper, the TX of the Bluetooth with the 0 pin (RX) in the Arduino.

4. connect with male-female jumper, the RX of the Bluetooth with the 1 pin (TX) in the Arduino.


By connect this connection, the communication between the Bluetooth and Arduino is in serial, so when the buletoth send anything the arduino will resive it in the serial input pin.



Main functions to Read, and Write in Arduino:


1. Serial.begin() Function:


Description

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit.


Syntax

Serial.begin(speed) Serial.begin(speed, config)

Arduino Mega only:

Serial1.begin(speed) Serial2.begin(speed) Serial3.begin(speed) Serial1.begin(speed, config) Serial2.begin(speed, config) Serial3.begin(speed, config)


Parameters

speed: in bits per second (baud) - long

config: sets data, parity, and stop bits.


2. Serial.read() Function:


Description

Reads incoming serial data. read() inherits from the Stream utility class.


Syntax

Serial.read()

Arduino Mega only: Serial1.read() Serial2.read() Serial3.read()


Returns

the first byte of incoming serial data available (or -1 if no data is available) - int


The read function is to read from the RX the received data.


Example:


3. Serial.print()


Description

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example-

  • Serial.print(78) gives "78"

  • Serial.print(1.23456) gives "1.23"

  • Serial.print('N') gives "N"

  • Serial.print("Hello world.") gives "Hello world."

An optional second parameter specifies the base (format) to use; permitted values are BIN(binary, or base 2), OCT(octal, or base 8), DEC(decimal, or base 10), HEX(hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example-

  • Serial.print(78, BIN) gives "1001110"

  • Serial.print(78, OCT) gives "116"

  • Serial.print(78, DEC) gives "78"

  • Serial.print(78, HEX) gives "4E"

  • Serial.print(1.23456, 0) gives "1"

  • Serial.print(1.23456, 2) gives "1.23"

  • Serial.print(1.23456, 4) gives "1.2346"

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For


example:

Serial.print(F(“Hello World”))

To send data without conversion to its representation as characters, use Serial.write().


Syntax

Serial.print(val) Serial.print(val, format)


Parameters

val: the value to print - any data type


Returns

size_t: print() returns the number of bytes written, though reading that number is optional.



By using this functions we start the communication channel between the arduino and the bluetooth by Serial.begin and read from the bluetooth with Serial.read, and print in the serial monitor by Serial.print


How to use the Bluetooth to control?


By connecting the bluetooth, and writing the code you have to have an application on a mobile phone that sends a number or char that the bluetooth chip will receive and give it to the arduino through the serial connection between them then run the code with it.


How the control happened?


The bluetooth send either 1, 2, or 3 when the code receive it it go through Ifs and see which one will enter and change the direction of the motor or make it stop.


Code:


void loop() {

// put your main code here, to run repeatedly:

count=Serial.read();

Serial.print("sensvalue=");

Serial.print(sensvalue);

Serial.print("\n");

if(count==1)

{

digitalWrite(r1,HIGH);

digitalWrite(r2,LOW);

}

else

{

if(count==2)

{

digitalWrite(r1,LOW);

digitalWrite(r2,LOW);

delay(100);

digitalWrite(r1,LOW);

digitalWrite(r2,HIGH);

}

else

{

if(count==3)

{

digitalWrite(r1,LOW);

digitalWrite(r2,LOW);

}

}

}

}


link:

https://www.arduino.cc/reference/en/language/functions/communication/serial/

https://drive.google.com/open?id=11mv5jOvV9jhy4RTcsUkrxQYPQWzEB5Lt

 
 
 

Comments


Giza systems.

Education Found

FabLab New Cairo.

  • Facebook Clean Grey
  • LinkedIn Clean Grey

© 2018 by Mohamed Khaled.

mm.png
bottom of page