Posted on

Interfacing Bluetooth Module HC05 with PIC Microcontroller

Interfacing Bluetooth with PIC Microcontroller is the third tutorial in a series of tutorials on Design Embedded System with PIC Microcontroller. In this tutorial we will control two DC Motors and a buzzer connected to the PIC microcontroller from our android phone via bluetooth.

In case you are new to PIC Microcontroller and MPLAB X IDE I strongly suggest to visit the tutorial – Getting started with MPLAB X IDE.

For interfacing Bluetooth with PIC Microcontroller we will require the following components which can be purchased in OpzLab web store. Links to the individual components are also provided.

  1. PICKIT3 Debugger / Programmer
  2. PIC16F15325
  3. Mini Breadboard
  4. Power Supply
  5. DC Motors
  6. L298N Motor Driver
  7. HC-05 Bluetooth Modules
  8. Buzzer
  9. Jumper Wires

Follow the below tutorial to control the DC Motors in forward, reverse, left and right direction and blow the buzzer horn using your android phone using Bluetooth.

Start MPLAB X IDE, create a new project with the device settings as mentioned in the tutorial Interfacing LED with PIC Microcontroller.

After the project is created open MPLAB Code Configurator and proceed with the below settings.

Do Not disable Low Voltage Mode and Do Not change MCLR settings. Let it be default.

Change the HF Internal Clock to 8 MHz and Clock Divider to 1 under Project Resource -> System -> System Module -> Easy Setup.

Assign the Output Ports for the motor controls and buzzer and disable analog for the ports under Project Resources -> System -> Pin Module.

Double Click on Device Resources -> EUSART -> EUSART1 to select UART1 module for bluetooth serial communication.

Enable Receiver and Transmitter under Project Resources -> Peripherals -> EUSART1. Keep all other default settings make sure the Baud Rate is set to 9600.

Change the Rx and Tx ports to pin number 2 and 3 respectively. Make sure to remove Rx1 and Tx1 assignments on any other pins.

Disable Analog on all the port pins.

Save and and click on Generate to create Header and Source Files. Close MPLAB Code Configurator.

Update the source file main.c with below code.

#include "mcc_generated_files/mcc.h"

#define BUZZER RC2
#define MOTOR_L_R RA2   // Left Motor Red Terminal
#define MOTOR_L_B RC0   // Left Motor Black Terminal
#define MOTOR_R_R RC1   // Right Motor Red Terminal
#define MOTOR_R_B RC3   // Right Motor Black Terminal

void forward() {
    MOTOR_L_R = 1;
    MOTOR_L_B = 0;
    MOTOR_R_R = 1;
    MOTOR_R_B = 0;
}

void reverse() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 1;
    MOTOR_R_R = 0;
    MOTOR_R_B = 1;
}

void left() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 0;
    MOTOR_R_R = 1;
    MOTOR_R_B = 0;
}

void right() {
    MOTOR_L_R = 1;
    MOTOR_L_B = 0;
    MOTOR_R_R = 0;
    MOTOR_R_B = 0;
}

void stop() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 0;
    MOTOR_R_R = 0;
    MOTOR_R_B = 0;
}

void main(void) {
    char ch = '0';
    SYSTEM_Initialize();
    BUZZER = 1;
    while (1) {
        ch = EUSART1_Read();
        if (ch == 'b') {
            BUZZER = 0;
            __delay_ms(100);
            BUZZER = 1;
            __delay_ms(100);
            ch = '0';
        }
        if (ch == 'f') {
            stop();
            __delay_ms(1000);
            forward();
        } else if (ch == 'r') {
            stop();
            __delay_ms(1000);
            reverse();
        } else if (ch == 'l') {
            stop();
            __delay_ms(1000);
            left();
        } else if (ch == 'w') {
            stop();
            __delay_ms(1000);
            right();
        } else if (ch == 's') {
            stop();
        }
    }
}

Circuit diagram

Hit the Download button to program and run the device from MPLAB X IDE. The pic microcontroller will be programmed after few seconds.

The red light on the Bluetooth module will blink rapidly. It is in discover mode. Scan for HC-05 device in you android phone and pair it with pass code 1234.

Install BlueTooth Serial Controller from Google Play in you Android phone.

Search for the paired HC-05 device and connect. The red light on the Bluetooth module will now blink less frequently. A serial communication link has been now established between your android phone and the PIC microcontroller.

Send the below commands to interact with the motors and buzzer.

You can also configure buttons in the android app to control the motors and the buzzer. Good Luck!