IoT Base Home Automation
Here's an improved and optimized version of your IoT-based home automation Arduino code
using an HC-05 Bluetooth module and 4 relays. This version includes better structure, error
handling, and improved efficiency.
Enhancements in this Version:
✅ More readable and structured code
✅ Uses an array to manage relays efficiently
✅ Ensures relays are initialized correctly
✅ Provides better debugging information
📌 Arduino Code for Home Automation
cpp
CopyEdit
// Define relay pins in an array for better management
const int relayPins[] = {4, 5, 6, 7}; // Relay pins connected to Arduino
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]); // Number of
relays
char receivedChar; // Variable to store received data
void setup() {
Serial.begin(9600); // Initialize Serial communication for Bluetooth HC-
05
// Set all relay pins as OUTPUT and turn them OFF initially
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // HIGH means relay is OFF (Active
LOW Relay)
}
Serial.println("Bluetooth Home Automation Ready...");
}
void loop() {
if (Serial.available()) { // Check if data is available
receivedChar = Serial.read(); // Read the received character
Serial.print("Received: ");
Serial.println(receivedChar); // Debugging
// Control relays based on received character
switch (receivedChar) {
case 'A': controlRelay(0, LOW); break; // Turn ON Relay 1
case 'a': controlRelay(0, HIGH); break; // Turn OFF Relay 1
case 'B': controlRelay(1, LOW); break; // Turn ON Relay 2
case 'b': controlRelay(1, HIGH); break; // Turn OFF Relay 2
case 'C': controlRelay(2, LOW); break; // Turn ON Relay 3
case 'c': controlRelay(2, HIGH); break; // Turn OFF Relay 3
case 'D': controlRelay(3, LOW); break; // Turn ON Relay 4
case 'd': controlRelay(3, HIGH); break; // Turn OFF Relay 4
default:
Serial.println("Invalid Command! Use A/a, B/b, C/c, D/d");
break;
}
}
}
// Function to control relay with debug messages
void controlRelay(int relayIndex, int state) {
if (relayIndex >= 0 && relayIndex < numRelays) {
digitalWrite(relayPins[relayIndex], state);
Serial.print("Relay ");
Serial.print(relayIndex + 1);
Serial.println(state == LOW ? " ON" : " OFF");
} else {
Serial.println("Error: Invalid relay index!");
}
}
📌 Wiring Connections:
Component Arduino Uno
HC-05 VCC 5V
HC-05 GND GND
HC-05 TX RX (Pin 0) (⚠ Needs disconnection while uploading code!)
HC-05 RX TX (Pin 1) (⚠ Use voltage divider to step down from 5V to 3.3V!)
Relay 1 IN Pin 4
Relay 2 IN Pin 5
Relay 3 IN Pin 6
Relay 4 IN Pin 7
⚠ Note:
The relays use Active LOW logic, so LOW means ON and HIGH means OFF.
Use a voltage divider (1KΩ & 2KΩ) to convert Arduino TX (5V) to HC-05 RX
(3.3V).
📌 How to Use the System
1. Pair HC-05 with your Smartphone (Default PIN: 1234 or 0000).
2. Use a Bluetooth Terminal App like "Bluetooth Terminal HC-05".
3. Send Commands:
o 'A' → Turn ON Relay 1
o 'a' → Turn OFF Relay 1
o 'B' → Turn ON Relay 2
o 'b' → Turn OFF Relay 2
o 'C' → Turn ON Relay 3
o 'c' → Turn OFF Relay 3
o 'D' → Turn ON Relay 4
o 'd' → Turn OFF Relay 4
📌 Why is This Code Better?
✅ Uses an array for relays → Easier to expand in future
✅ Has error handling for unexpected commands
✅ Includes debug messages for easier troubleshooting
✅ More efficient with a separate controlRelay() function
📌 Possible Future Upgrades
🔥 Use ESP8266/ESP32 to control via WiFi
🔥 Add Voice Control using Google Assistant or Alexa
🔥 Add a Smartphone App for a better UI
This should be a perfect solution for your home automation project. Let me know if you need
further improvements! 🚀😊
wite that type of code which that also controlled from voice
ChatGPT said:
To control your IoT-based home automation system using both Bluetooth commands and
voice control, we will integrate HC-05 Bluetooth module with a voice control app like
"Arduino Bluetooth Voice Controller" available on Android. This app converts voice
commands to text and sends them to the Arduino.
🔥 Features of this Code:
✅ Controls appliances via Bluetooth and voice commands
✅ Supports simple text-based voice commands
✅ Structured and optimized for easy expansion
✅ Uses an array for easy relay management
📌 Arduino Code for Bluetooth + Voice Control Home Automation
cpp
CopyEdit
// Define relay pins in an array
const int relayPins[] = {4, 5, 6, 7}; // Relay module connections to Arduino
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]); // Number of
relays
String receivedCommand; // Variable to store received text command
void setup() {
Serial.begin(9600); // Initialize Serial communication for Bluetooth HC-
05
// Set all relay pins as OUTPUT and turn them OFF initially
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // HIGH means relay is OFF (Active
LOW Relay)
}
Serial.println("Bluetooth & Voice-Controlled Home Automation Ready...");
}
void loop() {
if (Serial.available()) { // Check if data is available
receivedCommand = Serial.readString(); // Read received text command
receivedCommand.trim(); // Remove spaces and newlines
Serial.print("Received: ");
Serial.println(receivedCommand); // Debugging
processCommand(receivedCommand); // Process the received command
}
}
// Function to process voice and text commands
void processCommand(String command) {
command.toLowerCase(); // Convert to lowercase for easy comparison
if (command == "turn on light" || command == "light on" || command == "a")
{
controlRelay(0, LOW);
} else if (command == "turn off light" || command == "light off" ||
command == "a off") {
controlRelay(0, HIGH);
} else if (command == "turn on fan" || command == "fan on" || command ==
"b") {
controlRelay(1, LOW);
} else if (command == "turn off fan" || command == "fan off" || command ==
"b off") {
controlRelay(1, HIGH);
} else if (command == "turn on tv" || command == "tv on" || command ==
"c") {
controlRelay(2, LOW);
} else if (command == "turn off tv" || command == "tv off" || command ==
"c off") {
controlRelay(2, HIGH);
} else if (command == "turn on ac" || command == "ac on" || command ==
"d") {
controlRelay(3, LOW);
} else if (command == "turn off ac" || command == "ac off" || command ==
"d off") {
controlRelay(3, HIGH);
} else {
Serial.println("Invalid Command! Try again.");
}
}
// Function to control relay with debug messages
void controlRelay(int relayIndex, int state) {
if (relayIndex >= 0 && relayIndex < numRelays) {
digitalWrite(relayPins[relayIndex], state);
Serial.print("Relay ");
Serial.print(relayIndex + 1);
Serial.println(state == LOW ? " ON" : " OFF");
} else {
Serial.println("Error: Invalid relay index!");
}
}
📌 Wiring Connections
Component Arduino Uno
HC-05 VCC 5V
HC-05 GND GND
HC-05 TX RX (Pin 0) (⚠ Needs disconnection while uploading code!)
HC-05 RX TX (Pin 1) (⚠ Use voltage divider to step down from 5V to 3.3V!)
Relay 1 IN Pin 4
Relay 2 IN Pin 5
Relay 3 IN Pin 6
Relay 4 IN Pin 7
⚠ Important Notes:
Relays use Active LOW logic, so LOW means ON and HIGH means OFF.
Use a voltage divider (1KΩ & 2KΩ) to convert Arduino TX (5V) to HC-05 RX
(3.3V).
📌 How to Use with Voice Control
1. Pair HC-05 Bluetooth module with your smartphone (PIN: 1234 or 0000).
2. Install "Arduino Bluetooth Voice Controller" from the Play Store.
3. Connect to HC-05 in the app.
4. Use Voice Commands like:
o 🗣 "Turn on light" → Turns ON Relay 1
o 🗣 "Turn off light" → Turns OFF Relay 1
o 🗣 "Turn on fan" → Turns ON Relay 2
o 🗣 "Turn off fan" → Turns OFF Relay 2
o 🗣 "Turn on TV" → Turns ON Relay 3
o 🗣 "Turn off TV" → Turns OFF Relay 3
o 🗣 "Turn on AC" → Turns ON Relay 4
o 🗣 "Turn off AC" → Turns OFF Relay 4
💡 You can also send text commands manually from a Bluetooth terminal app.
For example:
Send "A" to turn ON Relay 1
Send "a" to turn OFF Relay 1
📌 Why is This Code Better?
✅ Supports both text and voice commands
✅ More user-friendly with natural language processing
✅ Uses an array for easier relay expansion
✅ Converts commands to lowercase to avoid mismatches
✅ Includes debug messages for troubleshooting
📌 Possible Future Upgrades
🔥 Use ESP8266/ESP32 for WiFi Control (so you can control from anywhere)
🔥 Integrate with Google Assistant or Alexa (for better automation)
🔥 Add a Mobile App for a better U
write a code when we also add display in this project
ChatGPT said:
Now, let's enhance your IoT-based home automation project by integrating an LCD Display
(16x2 with I2C module). This display will show the status of each appliance in real-time
whenever a command is received via Bluetooth or voice control.
🔥 Features of This Enhanced Code
✅ Supports both Bluetooth & Voice control
✅ Displays appliance status on a 16x2 LCD screen
✅ Uses an I2C LCD module to save Arduino pins
✅ Well-structured and optimized for future expansions
📌 Components Required
1. Arduino Uno
2. HC-05 Bluetooth Module
3. 4-Channel Relay Module
4. 16x2 LCD Display (with I2C module)
📌 Arduino Code: Bluetooth + Voice Control + LCD Display
cpp
CopyEdit
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize I2C LCD at address 0x27 (Check your LCD module's address if
needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define relay pins in an array
const int relayPins[] = {4, 5, 6, 7}; // Relay connections
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]); // Number of
relays
String receivedCommand; // Variable to store received command
void setup() {
Serial.begin(9600); // Initialize Serial communication for Bluetooth HC-
05
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("Home Automation");
// Set all relay pins as OUTPUT and turn them OFF initially
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // HIGH means relay is OFF (Active
LOW Relay)
}
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting for Cmd...");
}
void loop() {
if (Serial.available()) { // Check if data is available
receivedCommand = Serial.readString(); // Read received text command
receivedCommand.trim(); // Remove spaces and newlines
Serial.print("Received: ");
Serial.println(receivedCommand); // Debugging
processCommand(receivedCommand); // Process the received command
}
}
// Function to process voice and text commands
void processCommand(String command) {
command.toLowerCase(); // Convert to lowercase for easy comparison
if (command == "turn on light" || command == "light on" || command == "a")
{
controlRelay(0, LOW, "Light ON");
} else if (command == "turn off light" || command == "light off" ||
command == "a off") {
controlRelay(0, HIGH, "Light OFF");
} else if (command == "turn on fan" || command == "fan on" || command ==
"b") {
controlRelay(1, LOW, "Fan ON");
} else if (command == "turn off fan" || command == "fan off" || command ==
"b off") {
controlRelay(1, HIGH, "Fan OFF");
} else if (command == "turn on tv" || command == "tv on" || command ==
"c") {
controlRelay(2, LOW, "TV ON");
} else if (command == "turn off tv" || command == "tv off" || command ==
"c off") {
controlRelay(2, HIGH, "TV OFF");
} else if (command == "turn on ac" || command == "ac on" || command ==
"d") {
controlRelay(3, LOW, "AC ON");
} else if (command == "turn off ac" || command == "ac off" || command ==
"d off") {
controlRelay(3, HIGH, "AC OFF");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Command");
Serial.println("Invalid Command! Try again.");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting for Cmd...");
}
}
// Function to control relay with LCD display update
void controlRelay(int relayIndex, int state, String message) {
if (relayIndex >= 0 && relayIndex < numRelays) {
digitalWrite(relayPins[relayIndex], state);
Serial.print("Relay ");
Serial.print(relayIndex + 1);
Serial.println(state == LOW ? " ON" : " OFF");
// Update LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting for Cmd...");
} else {
Serial.println("Error: Invalid relay index!");
}
}
📌 Wiring Connections
HC-05 Bluetooth Module
HC-05 Pin Arduino Pin
VCC 5V
GND GND
TX RX (Pin 0) (⚠ Disconnect while uploading code!)
RX TX (Pin 1) (⚠ Use voltage divider to step down from 5V to 3.3V!)
Relay Module
Relay IN Pin Arduino Pin
IN1 4
IN2 5
IN3 6
IN4 7
16x2 LCD (with I2C Module)
LCD I2C Pin Arduino Pin
VCC 5V
GND GND
SDA A4
SCL A5
📌 How It Works
1. Pair HC-05 Bluetooth Module with your smartphone (PIN: 1234 or 0000).
2. Install "Arduino Bluetooth Voice Controller" from the Play Store.
3. Connect to HC-05 in the app.
4. Send Voice/Text Commands:
o 🗣 "Turn on light" → Turns ON Relay 1 (LCD shows "Light ON")
o 🗣 "Turn off light" → Turns OFF Relay 1 (LCD shows "Light OFF")
o 🗣 "Turn on fan" → Turns ON Relay 2 (LCD shows "Fan ON")
o 🗣 "Turn off fan" → Turns OFF Relay 2 (LCD shows "Fan OFF")
o 🗣 "Turn on TV" → Turns ON Relay 3 (LCD shows "TV ON")
o 🗣 "Turn off TV" → Turns OFF Relay 3 (LCD shows "TV OFF")
o 🗣 "Turn on AC" → Turns ON Relay 4 (LCD shows "AC ON")
o 🗣 "Turn off AC" → Turns OFF Relay 4 (LCD shows "AC OFF")
📌 Why is This Code Better?
✅ Includes a 16x2 LCD display to show real-time status
✅ Supports both text and voice commands
✅ Uses an array for easy relay expansion
✅ Provides feedback on LCD, improving user experience
📌 Possible Future Upgrades
🔥 Use ESP8266/ESP32 for WiFi Control (so you can control from anywhere)
🔥 Integrate with Google Assistant or Alexa (for voice automation)
🔥 Add a Mobile App for a better UI
what is the range between HC-05 and mobile device
ChatGPT said:
The range of the HC-05 Bluetooth module depends on environmental conditions and obstacles.
Here’s a breakdown of its typical range:
📌 HC-05 Bluetooth Module Range
✅ Indoors (Obstacles like walls, furniture, etc.) → 5 to 10 meters (16 - 33 feet)
✅ Outdoors (Clear line of sight, no obstacles) → 10 to 20 meters (33 - 66 feet)
✅ Optimal Conditions (Minimal interference, ideal setup) → Up to 30 meters (98 feet)
📌 Factors That Affect Range
1. Obstacles: Walls, furniture, and metal objects reduce signal strength.
2. Interference: Wi-Fi routers, microwaves, and other Bluetooth devices can cause interference.
3. Power Supply: A stable 5V power supply ensures the best performance.
4. Antenna Orientation: Keeping the HC-05 antenna exposed (not covered by enclosures)
improves range.
Simple code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX pins for HC-05
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
void loop()
{
if (mySerial.available() > 0)
{
char data = mySerial.read();
Serial.println(data);
if (data == 'B')
{
digitalWrite(12, HIGH);
}
else if (data == 'A')
{
digitalWrite(12, LOW);
else if (data == 'D')
{
digitalWrite(11, HIGH);
else if (data == 'C')
{
digitalWrite(11, LOW);
else if (data == 'F')
{
digitalWrite(10, HIGH);
else if (data == 'E')
{
digitalWrite(10, LOW);
else if (data == 'H')
{
digitalWrite(9, HIGH);
else if (data == 'G')
{
digitalWrite(9, LOW);
}
}
}