Arduino HC05 Bluetooth Code

 








Arduino HC05 Bluetooth Code( 4 Channel Relay Control)


The video is uploaded to zero electronics youtube channel 

Watch that video and see how a this can be made...

Arduino Bluetooth App Download Here
To Control 100 Devices 


Download

Program :

#include <SoftwareSerial.h>


// Define the Arduino pins connected to the HC-05 module

SoftwareSerial BT(0,1); // RX | TX (HC-05 TX to D2, HC-05 RX to D3 - with level shifter!)


// Define the Arduino pins connected to the Relay Module inputs (Active LOW)

const int RELAY1_PIN = 12; // IN1

const int RELAY2_PIN = 10; // IN2

const int RELAY3_PIN = 8; // IN3

const int RELAY4_PIN = 7; // IN4


char incoming_data;


void setup() {

  // Start the hardware serial monitor for debugging

  Serial.begin(9600);

  Serial.println("HC-05 4-Channel Relay Control Ready.");

  

  // Start the software serial communication with the HC-05

  BT.begin(9600); // Default baud rate for HC-05 Data Mode


  // Set the relay pins as outputs

  pinMode(RELAY1_PIN, OUTPUT);

  pinMode(RELAY2_PIN, OUTPUT);

  pinMode(RELAY3_PIN, OUTPUT);

  pinMode(RELAY4_PIN, OUTPUT);


  // Set all relays to OFF initially (HIGH signal for active LOW relay)

  digitalWrite(RELAY1_PIN, HIGH);

  digitalWrite(RELAY2_PIN, HIGH);

  digitalWrite(RELAY3_PIN, HIGH);

  digitalWrite(RELAY4_PIN, HIGH);

}


void loop() {

  if (BT.available()) { // Check if data is available from the Bluetooth module

    incoming_data = BT.read(); // Read the incoming character

    Serial.print("Received: ");

    Serial.println(incoming_data); // Echo data to the serial monitor


    // --- Control Logic (Example commands: '1'-'4' for ON, 'A'-'D' for OFF) ---


    // Relay 1 Control

    if (incoming_data == '1') {

      digitalWrite(RELAY1_PIN, LOW);  // Active LOW: ON

      Serial.println("Relay 1: ON");

    } else if (incoming_data == 'A') {

      digitalWrite(RELAY1_PIN, HIGH); // Active LOW: OFF

      Serial.println("Relay 1: OFF");

    }


    // Relay 2 Control

    else if (incoming_data == '2') {

      digitalWrite(RELAY2_PIN, LOW);

      Serial.println("Relay 2: ON");

    } else if (incoming_data == 'B') {

      digitalWrite(RELAY2_PIN, HIGH);

      Serial.println("Relay 2: OFF");

    }

    

    // Relay 3 Control

    else if (incoming_data == '3') {

      digitalWrite(RELAY3_PIN, LOW);

      Serial.println("Relay 3: ON");

    } else if (incoming_data == 'C') {

      digitalWrite(RELAY3_PIN, HIGH);

      Serial.println("Relay 3: OFF");

    }


    // Relay 4 Control

    else if (incoming_data == '4') {

      digitalWrite(RELAY4_PIN, LOW);

      Serial.println("Relay 4: ON");

    } else if (incoming_data == 'D') {

      digitalWrite(RELAY4_PIN, HIGH);

      Serial.println("Relay 4: OFF");

    }


    // All Relays Control

    else if (incoming_data == '9') { // All ON

      digitalWrite(RELAY1_PIN, LOW);

      digitalWrite(RELAY2_PIN, LOW);

      digitalWrite(RELAY3_PIN, LOW);

      digitalWrite(RELAY4_PIN, LOW);

      Serial.println("All Relays: ON");

    } else if (incoming_data == '0') { // All OFF

      digitalWrite(RELAY1_PIN, HIGH);

      digitalWrite(RELAY2_PIN, HIGH);

      digitalWrite(RELAY3_PIN, HIGH);

      digitalWrite(RELAY4_PIN, HIGH);

      Serial.println("All Relays: OFF");

    }

  }

}


Previous Post Next Post