#include <SoftwareSerial.
h>
// Define Bluetooth module serial pins
const int btRX = 2; // RX of Bluetooth module (TX of Arduino)
const int btTX = 3; // TX of Bluetooth module (RX of Arduino)
SoftwareSerial btSerial(btRX, btTX);
// Define relay control pin
const int relayPin = 7;
char command ;
void setup() {
// Start serial communication with the computer
Serial.begin(9600);
// Start serial communication with Bluetooth module
btSerial.begin(9600);
// Set relayPin as an output
pinMode(relayPin, OUTPUT);
// Initialize relay to off
digitalWrite(relayPin, LOW);
Serial.println("Bluetooth Relay Control Initialized.");
}
void loop() {
// Check if data is available from Bluetooth module
if (btSerial.available()) {
command = btSerial.read(); // Read the incoming byte
// Debugging output to the Serial Monitor
Serial.print("Received command: ");
Serial.println(command);
// Control relay based on received command
if (command == '1') {
// Turn relay ON
digitalWrite(relayPin, HIGH);
btSerial.println("Relay ON");
}
else if (command == '0') {
// Turn relay OFF
digitalWrite(relayPin, LOW);
btSerial.println("Relay OFF");
// You can add other code here if needed
}