Bluetooth Power Switch

Project Description: This project implements an AC power switch controlled over a Bluetooth connection. Host can reside on any Bluetooth enabled device, such as Android or IOS phones.

Key Components:

  • HC05/06
  • PIC16F628A
  • VIPER22AS
  • 12V/10A Electromagnetic Relay

Features:

Single Output Board View:

Four Outputs Board View:

PIC Config Register Settings:

// CONFIG for PIC16F628A
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
Macros &  Definitons
#include <xc.h>

#define OUTPUT_HIGH 1
#define OUTPUT_LOW  0

#define LON  OUTPUT_HIGH
#define LOFF OUTPUT_LOW

#define Relay1Pin PORTAbits.RA0
#define Relay2Pin PORTAbits.RA1
#define Relay3Pin PORTAbits.RA2
#define Relay4Pin PORTAbits.RA3

#define Switch1Pin PORTAbits.RA5
#define ATMode     PORTBbits.RB4
#define LED0Pin    PORTBbits.RB6
#define LED1Pin    PORTBbits.RB7

#define SetLED0(x)          LED0Pin = x
#define SetLED1(x)          LED1Pin = x
#define SetRELAY1(x)        Relay1Pin = x
#define SetRELAY2(x)        Relay2Pin = x
#define SetRELAY3(x)        Relay3Pin = x
#define SetRELAY4(x)        Relay4Pin = x

#if !defined(_BOOL)
  typedef enum _BOOL {
    FALSE=0, TRUE
  } BOOL; /* Undefined size */

  #define _BOOL _BOOL
#endif
PIC Init:
void init_mcu_core(void) {
  PORTA = 0;
  PORTB = 0;
  TRISA = 0x20;
  TRISB = 0x02;
  INTCON = 0;
  PIE1 = 0;
  PIR1 = 0;
  CMCON = 0x07;
  PCON = 0x08;
  T1CON = 0x00;
  SPBRG = 25; //9600 baud rate
  RCSTA = 0x90;
  TXSTA = 0x24; 
  PIE1bits.RCIE = 1;
  PIE1bits.TMR1IE = 1;
  INTCONbits.PEIE = 1;
  INTCONbits.GIE = 1;
}

The function manipulating the Relay:

void change_load_state(unsigned char cmd) {
  char temp =0x30;
  
  switch (cmd) {
    case 'A':
      SetRELAY1(LON);
    break;
    case 'B':
      SetRELAY2(LON);
    break;
    case 'C':
      SetRELAY3(LON);
    break;
    case 'D':
      SetRELAY4(LON);
    break;
    case 'a':
      SetRELAY1(LOFF);
    break;
    case 'b':
      SetRELAY2(LOFF);
    break;
    case 'c':
      SetRELAY3(LOFF);
    break;
    case 'd':
      SetRELAY4(LOFF);
    break;
    default:
      if (cmd > 0x2F && cmd < 0x40) {
        Relay1Pin = (cmd & 0x01) ? 1 : 0;
        Relay2Pin = (cmd & 0x02) ? 1 : 0;
        Relay3Pin = (cmd & 0x04) ? 1 : 0;
        Relay4Pin = (cmd & 0x08) ? 1 : 0;
      }
    break;
  }

  if (Relay1Pin)
    temp += 1;
  if (Relay2Pin)
    temp += 2;
  if (Relay3Pin)
    temp += 4;
  if (Relay4Pin)
    temp += 8;

  TXREG = temp;
}

Blinking LED:

void blink_board_alive_indicator(void) {
  static unsigned char timeCtr = 0;
  if (++timeCtr == 1)
    SetLED0(LON);
  else if (timeCtr == 3)
    SetLED0(LOFF);
  else if (timeCtr > 100)
    timeCtr = 0;
}

Interrupt Handler:

void interrupt system_isr(void) {
  if (PIR1bits.RCIF) { // No need to clear RCIF
    change_load_state(RCREG);
  }

  if (PIR1bits.TMR1IF) {
    blink_board_alive_indicator();
    PIR1bits.TMR1IF = 0;
  }
}

Main Function:

int main(void) {
  init_mcu_core();
  T1CONbits.TMR1ON = 1;
  while (1) {
    NOP();
    NOP();
  }

  return0;
}

Diptrace CAD files:

Sinlge Relay Board

Four Relay Board

Source Files:

PIC micro

Android

Leave a Reply

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

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