Advanced Arduino Guide: Build A Smart Home Control System Using Bluetooth Module

Advanced Arduino Guide: Build A Smart Home Control System Using Bluetooth Module

Advanced Arduino Guide: Build A Smart Home Control System Using Bluetooth Module

Welcome back to the Advanced Arduino Guide series! In previous articles, we learned how to create various interesting projects using different sensors and modules. In this article, we will further expand our knowledge by building a smart home control system that can be controlled via a mobile phone using a Bluetooth module.

Materials Needed

Before we begin, make sure you have the following materials:

  • Arduino Uno development board

  • Several Dupont wires

  • Bluetooth module (recommended HC-05)

  • Relay module (for controlling household appliances)

  • Household appliances (e.g., lights, fans, etc.)

Connecting the Circuit

  1. Plug the Arduino board into the USB port of your computer.

  2. Use Dupont wires to connect the VCC pin of the Bluetooth module to the positive terminal of the circuit.

  3. Use Dupont wires to connect the GND pin of the Bluetooth module to the negative terminal of the circuit.

  4. Use Dupont wires to connect the TX pin of the Bluetooth module to digital pin 2 on the Arduino board.

  5. Use Dupont wires to connect the RX pin of the Bluetooth module to digital pin 3 on the Arduino board.

  6. Use Dupont wires to connect the signal pin of the relay module to digital pin 4 on the Arduino board.

Writing the Program

Open the Arduino IDE, create a new program, and copy the following code into the editor:

#include <SoftwareSerial.h>   

SoftwareSerial bluetooth(2, 3);  // Define the RX/TX pins for the Bluetooth module as 2 and 3  

#define RELAY_PIN 4           // Define the relay control pin as 4  

void setup() {  
// Set the relay control pin as output  
  pinMode(RELAY_PIN, OUTPUT);  
// Initial state of the relay is off  
  digitalWrite(RELAY_PIN, HIGH);  

// Initialize serial communication  
  Serial.begin(9600);  
// Initialize Bluetooth module communication  
  bluetooth.begin(9600);  
}  

void loop() {  
// If the Bluetooth module has data to send  
  if (bluetooth.available()) {  
// Read the command sent by the Bluetooth module and store it in the variable command  
     char command = bluetooth.read();  

    switch (command) {  
// If command is 1, turn on the relay      
      case '1':  
        digitalWrite(RELAY_PIN, LOW);  
        Serial.println("Device is turned on.");  
        break;  
// If command is 0, turn off the relay        
      case '0':  
        digitalWrite(RELAY_PIN, HIGH);  
        Serial.println("Device is turned off.");  
        break;  
    }  
  }  
}

Note:This program controls the relay switch via the Bluetooth module, enabling control of household appliances.

The setup() function initializes the relay pin as output and sets up serial and Bluetooth communications. The loop() function continuously listens for data sent from the Bluetooth module; if data is received, it reads and checks the command. If it is ‘1’, the relay is turned on; if it is ‘0’, the relay is turned off. The bluetooth.available() function checks if the Bluetooth module has data to send. The command = bluetooth.read() function reads the data sent by the Bluetooth module. The case ‘1’ turns on the relay, while the case ‘0’ turns off the relay.

Uploading the Program

Click the upload button in the Arduino IDE to upload the program to the Arduino board.

Controlling Smart Home with Mobile Phone

Now, open the Bluetooth settings on your mobile phone and connect to the Arduino board. Then, you can use a Bluetooth serial terminal application (such as Serial Bluetooth Terminal) to send commands. Sending the command ‘1’ will turn on the household appliances, while sending the command ‘0’ will turn them off.

Explore More Possibilities

Congratulations on successfully building a smart home control system that can be controlled via your mobile phone! You can add more functionalities and sensors to achieve more complex smart home applications.

Don’t forget to follow our WeChat official account for more tutorials and project examples about Arduino. If you have any questions, feel free to leave a comment, and we will be happy to assist you.

Leave a Comment