Bluetooth Module Interfacing with PIC16F877A

This article is a continuation of the series of tutorials on the PIC16F877A Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have interfaced the IR sensor with PIC16F877A. In this tutorial, we are going to see Bluetooth Module Interfacing with PIC16F877A.

Prerequisites

Before starting this tutorial we should know the below topics. If you know already, please go further.

Components Required

We have our own EmbeTronicX store called ChipTronicX. You can buy from there.

  • PIC16F877A Development Board
  • Bluetooth Module
  • DC Motor
  • L293D (Motor Driver IC)
  • Smart Phone with Bluetooth Terminal App

Bluetooth Module (HC-05)

Introduction

HC‐05 module is an easy-to-use Bluetooth SPP (Serial Port Protocol) module, designed for a transparent wireless serial connection setup. The HC-05 Bluetooth Module can be used in a Master or Slave configuration, making it a great solution for wireless communication. This serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with a complete 2.4GHz radio transceiver and baseband. It uses a CSR Bluecore 04‐External single-chip Bluetooth system with CMOS technology and with AFH (Adaptive Frequency Hopping Feature).

The Bluetooth module HC-05 is a MASTER/SLAVE module. By default the factory setting is SLAVE. The Role of the module (Master or Slave) can be configured only by AT COMMANDS. The slave modules cannot initiate a connection to another Bluetooth device but can accept connections. The master module can initiate a connection to other devices. The user can use it simply for a serial port replacement to establish a connection between MCU and GPS, PC to your embedded project, etc.

Hardware Features

  • Typical ‐80dBm sensitivity.
  • Up to +4dBm RF transmit power.
  • 3.3 to 5 V I/O.
  • PIO(Programmable Input/Output) control.
  • UART interface with programmable baud rate.
  • With integrated antenna.
  • With edge connector.

Software Features

  • Slave default Baud rate: 9600, Data bits:8, Stop bit:1,Parity:No parity.
  • Auto‐connect to the last device on power as default.
  • Permit pairing device to connect as default.
  • Auto‐pairing PINCODE:”1234” as default.

Pin Description

The HC-05 Bluetooth Module has 6pins. They are as follows:

ENABLE:

When enable is pulled LOW, the module is disabled which means the module will not turn on and it fails to communicate. When enable is left open or connected to 3.3V, the module is enabled i.e the module remains on and communication also takes place.

Vcc:

Supply Voltage 3.3V to 5V

GND:

Ground pin

TXD & RXD:

These two pins act as a UART interface for communication

STATE:

It acts as a status indicator. When the module is not connected to / paired with any other Bluetooth device, the signal goes Low. At this low state, the led flashes continuously which denotes that the module is not paired with another device. When this module is connected to/paired with any other Bluetooth device, the signal goes High. At this high state, the led blinks with a constant delay say for example 2s delay which indicates that the module is paired.

BUTTON SWITCH:

This is used to switch the module into AT command mode. To enable AT command mode, press the button switch for a second. With the help of AT commands, the user can change the parameters of this module but only when the module is not paired with any other BT device. If the module is connected to any other Bluetooth device, it starts to communicate with that device and fails to work in AT command mode.

Bluetooth Module Interfacing with PIC16F877A

Connection

Bluetooth Module

  • Rx – RC6 (Tx)
  • Tx – RC7 (Rx)

DC Motor (L293D Driver)

  • In 1 – RD0
  • In 2 – RD1
  • En 1 – Vcc
  • In 3 – RD2
  • In 4 – RD3
  • En 2 – Vcc

Bluetooth Module Interfacing with PIC16F877A

Source Code

The program given below is the HC-05 Bluetooth Module Interfacing with PIC16F877A. This process is quite different from others since we are going to use android mobile to control and communicate with PIC16F877A. Here the Bluetooth module acts as an interface between our mobile and PIC16F877A board. Before getting into the execution process, follow the given procedure:

  • First of all, the user should install an application called Bluetooth SPP PRO from the play store which is a free application.
  • After installation, pair the Bluetooth module to your mobile as like connecting one device to another using Bluetooth. The default pairing code is 1234.
  • Upload the given program to the PIC16F877A board.
  • The Bluetooth SPP PRO has three types of communication modes. Here Byte stream mode is used to communicate. So select that mode and give the input as below.

F – Forward

R – Reverse

L – Left

W – Right

  • So We can control the motor using Mobile by Bluetooth. We can use this setup for developing robots using Bluetooth.
#include<htc.h>
#include<pic.h>


__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

#define IN1 RD0
#define IN2 RD1
#define IN3 RD2
#define IN4 RD3

void ser_int();
void tx(unsigned char);
unsigned char rx();

void main()
{
    TRISC6=TRISC7=1;
    TRISD=0;              //Port D is act as Output
    unsigned char get;
    ser_int();
    
    while(1) {
        get=rx();
        tx(get);
        if(get == 'F') {
                /*Forward*/
                IN1 = 1;
                IN3 = 1;
                IN2 = 0; 
                IN4 = 0;
        } else if (get == 'R') {
                /*Reverse*/
                IN2 = 1;
                IN4 = 1;
                IN1 = 0;
                IN3 = 0;
                
        } else if (get == 'W') {
                /*Right*/
                IN1 = 1;
                IN2 = 0;
                IN3 = 0;
                IN4 = 0;
                
        } else if (get == 'L') {
                /*Left*/
                IN3 = 1;
                IN2 = 0;
                IN1 = 0;
                IN4 = 0;
                
        } else if (get == 'S') {
                /*Off*/
                IN1 = IN2 = IN3 = IN4 = 0;
        }
    }   
}   

void ser_int()
{
    TXSTA=0x20; //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
    RCSTA=0b10010000; //Serial Port enabled,8-bit reception
    SPBRG=17;           //9600 baudrate for 11.0592Mhz
    TXIF=RCIF=0;
}
    
void tx(unsigned char a)
{
    TXREG=a;
    while(!TXIF);
    TXIF = 0;
}

unsigned char rx()
{
    while(!RCIF);
    RCIF=0;
    return RCREG;
}

In our next tutorial, we will see how to interface the LDR sensor with PIC16F877A.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL TutorialsArduino Tutorials


Discover more from EmbeTronicX

Subscribe to get the latest posts sent to your email.

12 thoughts on “Bluetooth Module Interfacing with PIC16F877A”

  1. Hi,
    i have tried your code for PIC18F25K22. But it is not working.

    // CONFIG1H
    #pragma config FOSC = HSMP  // Oscillator Selection bits->HS oscillator (medium power 4-16 MHz)
    #pragma config PLLCFG = OFF  // 4X PLL Enable->Oscillator used directly
    #pragma config PRICLKEN = ON  // Primary clock enable bit->Primary clock enabled
    #pragma config FCMEN = OFF  // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor disabled
    #pragma config IESO = OFF  // Internal/External Oscillator Switchover bit->Oscillator Switchover mode disabled

    // CONFIG2L
    #pragma config PWRTEN = OFF  // Power-up Timer Enable bit->Power up timer disabled
    #pragma config BOREN = OFF  // Brown-out Reset Enable bits->Brown-out Reset disabled in hardware and software
    #pragma config BORV = 190  // Brown Out Reset Voltage bits->VBOR set to 1.90 V nominal

    // CONFIG2H
    #pragma config WDTEN = OFF  // Watchdog Timer Enable bits->Watch dog timer is always disabled. SWDTEN has no effect.
    #pragma config WDTPS = 32768  // Watchdog Timer Postscale Select bits->1:32768

    // CONFIG3H
    #pragma config CCP2MX = PORTC1  // CCP2 MUX bit->CCP2 input/output is multiplexed with RC1
    #pragma config PBADEN = OFF  // PORTB A/D Enable bit->PORTB<5:0> pins are configured as digital I/O on Reset
    #pragma config CCP3MX = PORTB5  // P3A/CCP3 Mux bit->P3A/CCP3 input/output is multiplexed with RB5
    #pragma config HFOFST = ON  // HFINTOSC Fast Start-up->HFINTOSC output and ready status are not delayed by the oscillator stable status
    #pragma config T3CMX = PORTC0  // Timer3 Clock input mux bit->T3CKI is on RC0
    #pragma config P2BMX = PORTB5  // ECCP2 B output mux bit->P2B is on RB5
    #pragma config MCLRE = INTMCLR  // MCLR Pin Enable bit->RE3 input pin enabled; MCLR disabled

    // CONFIG4L
    #pragma config STVREN = ON  // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
    #pragma config LVP = OFF  // Single-Supply ICSP Enable bit->Single-Supply ICSP disabled
    #pragma config XINST = OFF  // Extended Instruction Set Enable bit->Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    #pragma config DEBUG = OFF  // Background Debug->Disabled

    // CONFIG5L
    #pragma config CP0 = OFF  // Code Protection Block 0->Block 0 (000800-001FFFh) not code-protected
    #pragma config CP1 = OFF  // Code Protection Block 1->Block 1 (002000-003FFFh) not code-protected
    #pragma config CP2 = OFF  // Code Protection Block 2->Block 2 (004000-005FFFh) not code-protected
    #pragma config CP3 = OFF  // Code Protection Block 3->Block 3 (006000-007FFFh) not code-protected

    // CONFIG5H
    #pragma config CPB = OFF  // Boot Block Code Protection bit->Boot block (000000-0007FFh) not code-protected
    #pragma config CPD = OFF  // Data EEPROM Code Protection bit->Data EEPROM not code-protected

    // CONFIG6L
    #pragma config WRT0 = OFF  // Write Protection Block 0->Block 0 (000800-001FFFh) not write-protected
    #pragma config WRT1 = OFF  // Write Protection Block 1->Block 1 (002000-003FFFh) not write-protected
    #pragma config WRT2 = OFF  // Write Protection Block 2->Block 2 (004000-005FFFh) not write-protected
    #pragma config WRT3 = OFF  // Write Protection Block 3->Block 3 (006000-007FFFh) not write-protected

    // CONFIG6H
    #pragma config WRTC = OFF  // Configuration Register Write Protection bit->Configuration registers (300000-3000FFh) not write-protected
    #pragma config WRTB = OFF  // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
    #pragma config WRTD = OFF  // Data EEPROM Write Protection bit->Data EEPROM not write-protected

    // CONFIG7L
    #pragma config EBTR0 = OFF  // Table Read Protection Block 0->Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR1 = OFF  // Table Read Protection Block 1->Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR2 = OFF  // Table Read Protection Block 2->Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
    #pragma config EBTR3 = OFF  // Table Read Protection Block 3->Block 3 (006000-007FFFh) not protected from table reads executed in other blocks

    // CONFIG7H
    #pragma config EBTRB = OFF  // Boot Block Table Read Protection bit->Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

    #include “mcc_generated_files/mcc.h”
    #include <xc.h>

    #define IN1 PORTBbits.RB0
    #define IN2 PORTBbits.RB1
    #define IN3 PORTBbits.RB2
    #define IN4 PORTBbits.RB3

    void ser_int();
    void tx(unsigned char);
    unsigned char rx();

    void main()
    {
      ser_int();
      TRISCbits.TRISC6=1;
      TRISCbits.TRISC7=1;
      TRISB=0;       //Port B is act as Output
      PORTB=0;
       
       
       
      unsigned char get;
      while(1) {
        get=rx();
        tx(get);
        if(get == ‘F’) {
            /*Forward*/
            IN1 = 1;
            IN3 = 1;
            IN2 = 0; 
            IN4 = 0;
        } else if (get == ‘R’) {
            /*Reverse*/
            IN2 = 1;
            IN4 = 1;
            IN1 = 0;
            IN3 = 0;
             
        } else if (get == ‘W’) {
            /*Right*/
            IN1 = 1;
            IN2 = 0;
            IN3 = 0;
            IN4 = 0;
             
        } else if (get == ‘L’) {
            /*Left*/
            IN3 = 1;
            IN2 = 0;
            IN1 = 0;
            IN4 = 0;
             
        } else if (get == ‘S’) {
            /*Off*/
            IN1 = IN2 = IN3 = IN4 = 0;
        }
      }  
    }  

    void ser_int()
    {
      TXSTA=0x20; //BRGH=0, TXEN = 1, Asynchronous Mode, 8-bit mode
      RCSTA=0b10010000; //Serial Port enabled,8-bit reception
      SPBRG=25;      //9600 baudrate for 11.0592Mhz
      TXIF=RCIF=0;
    }
       

    void tx(unsigned char a)
    {
      TXREG=a;
      while(PIR1bits.TX1IF==1);
      PIR1bits.TX1IF=0;
    }

    unsigned char rx()
    {
      while(PIR1bits.RC1IF==1);
      PIR1bits.RC1IF=0;
      return RCREG;
    }

    Reply
  2. hi sir
    I m use MPLAB for programming but i fund this message ‘Error  [939] ; . no file arguments’ , but I don’t know where the error is. Please can you help me.

    Reply
      • hello sir,
        i have some bugs can you help me please
        ”

        Executing: “C:\Program Files (x86)\HI-TECH Software\PICC\LITE\9.60\bin\picl.exe” -C “C:\Users\21266\Desktop\metal detect\metall.c” –chip=16F877A -P -q -g –asmlist “–errformat=Error  [%n] %f; %l.%c %s” “–msgformat=Advisory[%n] %s” “–warnformat=Warning [%n] %f; %l.%c %s”
        Warning [349] C:\Users\21266\Desktop\metal detect\metall.c; 13.1 non-prototyped function declaration for “ser_int”
        Warning [349] C:\Users\21266\Desktop\metal detect\metall.c; 28.1 non-prototyped function declaration for “rx”
        Error  [249] C:\Users\21266\Desktop\metal detect\metall.c; 38.1 probable missing “}” in previous block
        Error  [984] C:\Users\21266\Desktop\metal detect\metall.c; 39.10 type redeclared
        Error  [285] C:\Users\21266\Desktop\metal detect\metall.c; 41.1 no identifier in declaration
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 41.1 missing basic type; int assumed
        Error  [314] C:\Users\21266\Desktop\metal detect\metall.c; 41.1 “;” expected
        Error  [984] C:\Users\21266\Desktop\metal detect\metall.c; 43.8 type redeclared
        Error  [313] C:\Users\21266\Desktop\metal detect\metall.c; 43.8 function body expected
        Error  [285] C:\Users\21266\Desktop\metal detect\metall.c; 44.1 no identifier in declaration
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 44.1 missing basic type; int assumed
        Error  [314] C:\Users\21266\Desktop\metal detect\metall.c; 44.1 “;” expected
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 47.5 missing basic type; int assumed
        Error  [983] C:\Users\21266\Desktop\metal detect\metall.c; 47.5 storage class redeclared
        Error  [984] C:\Users\21266\Desktop\metal detect\metall.c; 47.5 type redeclared
        Error  [239] C:\Users\21266\Desktop\metal detect\metall.c; 47.5 identifier “RD2” redefined (from line 134)
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 48.5 missing basic type; int assumed
        Error  [983] C:\Users\21266\Desktop\metal detect\metall.c; 48.5 storage class redeclared
        Error  [984] C:\Users\21266\Desktop\metal detect\metall.c; 48.5 type redeclared
        Error  [239] C:\Users\21266\Desktop\metal detect\metall.c; 48.5 identifier “RD1” redefined (from line 135)
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 49.5 missing basic type; int assumed
        Error  [983] C:\Users\21266\Desktop\metal detect\metall.c; 49.5 storage class redeclared
        Error  [984] C:\Users\21266\Desktop\metal detect\metall.c; 49.5 type redeclared
        Error  [239] C:\Users\21266\Desktop\metal detect\metall.c; 49.5 identifier “RD3” redefined (from line 133)
        Error  [285] C:\Users\21266\Desktop\metal detect\metall.c; 50.1 no identifier in declaration
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 50.1 missing basic type; int assumed
        Error  [314] C:\Users\21266\Desktop\metal detect\metall.c; 50.1 “;” expected
        Error  [285] C:\Users\21266\Desktop\metal detect\metall.c; 50.2 no identifier in declaration
        Warning [374] C:\Users\21266\Desktop\metal detect\metall.c; 50.2 missing basic type; int assumed
        Error  [314] C:\Users\21266\Desktop\metal detect\metall.c; 50.2 “;” expected
        “

        Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from EmbeTronicX

Subscribe now to keep reading and get access to the full archive.

Continue reading