Introduction
The HC-05 Bluetooth module is a popular and easy-to-use device for wireless communication between microcontrollers and Bluetooth-enabled devices like smartphones and computers. In this project, we will learn how to control an LED using an HC-05 module and an Arduino. The LED will respond to commands sent via a mobile app, turning ON or OFF based on Bluetooth signals. This is a great beginner-friendly project for understanding Bluetooth communication and interfacing with Arduino.
Components Required
1. Arduino Uno
The microcontroller that will control the LED based on Bluetooth commands.
2. HC-05 Bluetooth Module
The HC-05 module allows the Arduino to communicate wirelessly with a smartphone or PC via Bluetooth.
3. LED
We will use an LED to visualize the control operations. The LED will turn ON or OFF based on the signals received from the Bluetooth module.
4. Resistor (220 ohm)
To limit the current to the LED and prevent it from burning out.
5. Jumper Wires
Used to connect components on the breadboard or Arduino.
6. Breadboard (optional)
To easily make connections between components.
7. Smartphone or PC with Bluetooth support
We will use a smartphone with a Bluetooth terminal app to send commands to the Arduino.
List of Components
- Arduino Uno
- HC-05 Bluetooth Module
- LED
- 220-ohm resistor
- Jumper wires
- Breadboard (optional)
- Smartphone or PC with Bluetooth app
Circuit Explanation
The HC-05 Bluetooth module communicates with the Arduino using the Serial Communication (TX and RX) pins. Since the Arduino Uno has hardware serial pins (0 for RX, 1 for TX) that are also used for programming, we will use SoftwareSerial to create additional serial ports on pins 10 and 11 to communicate with the HC-05.
Pin Connections:
- HC-05 TX (Transmit) is connected to Arduino pin 10 (SoftwareSerial RX).
- HC-05 RX (Receive) is connected to Arduino pin 11 (SoftwareSerial TX).
- LED is connected to pin 9 of the Arduino through a 220-ohm resistor.
The HC-05 module receives power (5V and GND) from the Arduino. Once the circuit is set up, the smartphone sends Bluetooth commands (like ‘1’ for turning the LED ON and ‘0’ for turning it OFF) to the HC-05, which communicates these commands to the Arduino. Based on the received command, the Arduino will control the LED accordingly.
Circuit Diagram:
Code
We will use the SoftwareSerial
library to create a software serial port for the HC-05 module. The Arduino will read commands from the Bluetooth module and control the LED accordingly.
#include <SoftwareSerial.h>
// Define pins for SoftwareSerial communication
SoftwareSerial BTSerial(10, 11); // RX on pin 10, TX on pin 11
// Define LED pin
const int ledPin = 9;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Begin serial communication with HC-05 Bluetooth module
BTSerial.begin(9600); // HC-05 default baud rate is 9600
Serial.begin(9600); // Start serial communication for monitoring
}
void loop() {
// Check if data is available from the Bluetooth module
if (BTSerial.available()) {
char command = BTSerial.read(); // Read the incoming command
// Print the received command to the serial monitor
Serial.print("Received: ");
Serial.println(command);
// Control the LED based on the received command
if (command == '1') {
digitalWrite(ledPin, HIGH); // Turn ON the LED
Serial.println("LED ON");
}
else if (command == '0') {
digitalWrite(ledPin, LOW); // Turn OFF the LED
Serial.println("LED OFF");
}
}
}
Code Explanation
Libraries and Pin Definitions
#include <SoftwareSerial.h>
: This library allows us to create a software serial port. Since the Arduino Uno has only one hardware serial port (pins 0 and 1), we use software serial to communicate with the HC-05 on pins 10 and 11.SoftwareSerial BTSerial(10, 11);
: We create a SoftwareSerial object calledBTSerial
, which uses pin 10 as RX (receive) and pin 11 as TX (transmit).const int ledPin = 9;
: We define pin 9 as the pin connected to the LED.
Setup Function
pinMode(ledPin, OUTPUT);
: We set the LED pin as an output so that the Arduino can control it.BTSerial.begin(9600);
: This starts communication with the HC-05 Bluetooth module at a baud rate of 9600, which is the default for the HC-05.Serial.begin(9600);
: This initializes the serial monitor for debugging purposes. We can use the serial monitor to see what commands the Arduino is receiving from the HC-05.
Loop Function
if (BTSerial.available())
: This checks if any data has been received from the HC-05. If data is available, the following block of code is executed.char command = BTSerial.read();
: The received command is read and stored in thecommand
variable.if (command == '1')
: If the received command is ‘1’, the LED will turn ON (digitalWrite(ledPin, HIGH)
), and a message will be printed to the serial monitor indicating that the LED is ON.else if (command == '0')
: If the received command is ‘0’, the LED will turn OFF (digitalWrite(ledPin, LOW)
), and a message will be printed to the serial monitor indicating that the LED is OFF.
Testing and Result
After uploading the code to your Arduino, follow these steps:
- Power up the HC-05 Bluetooth module and pair it with your smartphone.
- Open a Bluetooth terminal app on your phone and connect to the HC-05 module.
- Send ‘1’ to turn ON the LED and ‘0’ to turn it OFF.
This project demonstrates how simple Bluetooth communication can control devices like an LED. You can extend this concept to control other components, such as relays, motors, or sensors.
Conclusion
In this tutorial, we learned how to control an LED using an HC-05 Bluetooth module and Arduino. We used the SoftwareSerial library to create a separate serial interface for the Bluetooth module, freeing up the default serial pins for debugging. This project lays the groundwork for more complex Bluetooth-based projects, such as home automation systems or wireless robots.