Codes
Codes
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.
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)
}
void loop() {
if (Serial.available()) { // Check if data is available
receivedChar = Serial.read(); // Read the received character
Serial.print("Received: ");
Serial.println(receivedChar); // Debugging
default:
Serial.println("Invalid Command! Use A/a, B/b, C/c, D/d");
break;
}
}
}
📌 Wiring Connections:
⚠ 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
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.
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)
}
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
}
}
📌 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).
💡 You can also send text commands manually from a Bluetooth terminal app.
For example:
📌 Components Required
1. Arduino Uno
2. HC-05 Bluetooth Module
3. 4-Channel Relay Module
4. 16x2 LCD Display (with I2C module)
// Initialize I2C LCD at address 0x27 (Check your LCD module's address if
needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);
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
📌 Wiring Connections
HC-05 Bluetooth Module
HC-05 Pin Arduino Pin
VCC 5V
GND GND
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")
🔥 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:
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);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
digitalWrite(9, LOW);
}
}
}