HC05 Bluetooth with LCD Display Using Arduino

HC05 Bluetooth with LCD Display : In this blog, we will guide you step-by-step through setting up a Bluetooth communication system with an LCD display using Arduino. You will learn how to send and receive data wirelessly through Bluetooth and display the received text on an LCD screen. We’ll also explain the circuit connection and how to implement the necessary code to make this work.

What You’ll Need

Before diving into the code and setup, make sure you have the following components:

  1. Arduino board (such as Arduino Uno)
  2. Bluetooth module (HC-05 or HC-06)
  3. LCD screen with I2C (16×2 or similar)
  4. Jumper wires
  5. Breadboard (optional but recommended for prototyping)

Circuit Connection

To connect the components, follow these steps:

  1. Bluetooth Module (HC-05):
    • VCC → 5V on Arduino
    • GND → GND on Arduino
    • TX → Pin 2 on Arduino (RX)
    • RX → Pin 3 on Arduino (TX)
  2. LCD with I2C:
    • VCC → 5V on Arduino
    • GND → GND on Arduino
    • SDA → Pin A4 on Arduino (for Uno)
    • SCL → Pin A5 on Arduino (for Uno)
HC05 Bluetooth with LCD Display Learn step-by-step instructions for establishing seamless Bluetooth connections between devices efficiently and effortlessly.

Code Breakdown

Now that we have the components connected, let’s go over the code. We use LiquidCrystal_I2C to interact with the LCD, SoftwareSerial to communicate with the Bluetooth module, and basic serial communication to handle data transfer.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

// Define the Bluetooth serial port
SoftwareSerial BTSerial(2, 3); // RX, TX pins for Bluetooth

// Initialize the LCD with I2C address 0x27 (this might be different depending on your LCD)
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 16 columns and 2 rows for the LCD

void setup() {
  // Start the built-in serial communication for debugging
  Serial.begin(9600);
  
  // Start Bluetooth communication
  BTSerial.begin(9600);
  
  // Initialize the LCD
  lcd.begin();  // 16 columns and 2 rows for the LCD
  lcd.print("Bluetooth Ready");
  delay(1000);
  lcd.clear();  // Clear the display after a short delay
  
  Serial.println("Bluetooth communication started...");
}

void displayScrollableText(String text) {
  int textLength = text.length();

  // If the text fits on the LCD, display it directly
  if (textLength <= 16) {
    lcd.clear();
    lcd.print(text);
    return;
  }

  // Scroll the text if it's longer than 16 characters
  for (int i = 0; i <= textLength - 16; i++) {
    lcd.clear();
    lcd.print(text.substring(i, i + 16));  // Display a substring of 16 characters
    delay(300);  // Adjust delay for the scrolling speed
  }
}

void loop() {
  // Check if there is any data received from the phone (Bluetooth)
  if (BTSerial.available()) {
    // Read the incoming data
    String receivedMessage = BTSerial.readString();

    // Print the received message for debugging
    Serial.print("Received message: ");
    Serial.println(receivedMessage);

    // Trim the received message
    receivedMessage.trim();  // Trim any extra spaces or newline characters

    // Display the message on the LCD with scrolling if needed
    displayScrollableText(receivedMessage);

    // Check if the received message is "hello"
    if (receivedMessage == "hello") {
      // Send a reply message "How are you?"
      BTSerial.println("How are you?");
      Serial.println("Replied: How are you?");
    }
  }

  // Add a small delay to avoid overloading the serial buffer
  delay(100);
}

1. Import Libraries

We start by importing the necessary libraries for the LCD and Bluetooth communication:

https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library: HC05 Bluetooth with LCD Display Using Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
  • Wire.h: This is required to use I2C communication for the LCD.
  • LiquidCrystal_I2C.h: This library helps in controlling the LCD screen over I2C.
  • SoftwareSerial.h: This library allows us to create a serial communication over different pins, which is essential for communication with the Bluetooth module.

2. Define Communication Pins

Next, we define the pins for the Bluetooth module and LCD:

SoftwareSerial BTSerial(2, 3); // RX, TX pins for Bluetooth
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 16 columns and 2 rows for the LCD
  • SoftwareSerial BTSerial(2, 3): We assign pin 2 to RX and pin 3 to TX for Bluetooth communication.
  • LiquidCrystal_I2C lcd(0x27, 16, 2): This initializes the LCD screen with the I2C address 0x27 (which might vary depending on your module) and sets the display size to 16×2.

3. Set Up Communication

In the setup() function, we initialize the serial monitor for debugging, Bluetooth communication, and the LCD display:

void setup() {
  Serial.begin(9600);
  BTSerial.begin(9600);
  lcd.begin();
  lcd.print("Bluetooth Ready");
  delay(1000);
  lcd.clear();
  Serial.println("Bluetooth communication started...");
}
  • Serial.begin(9600): This initializes the serial communication with the Arduino IDE for debugging purposes.
  • BTSerial.begin(9600): This starts Bluetooth communication at a baud rate of 9600.
  • lcd.begin(): Initializes the LCD and prepares it to display data.
  • lcd.print(“Bluetooth Ready”): Displays a welcome message on the LCD for a second before clearing it.

4. Scrolling Text Function

We created a function to handle the scrolling text effect on the LCD for messages that are too long to fit on the screen:

void displayScrollableText(String text) {
  int textLength = text.length();
  if (textLength <= 16) {
    lcd.clear();
    lcd.print(text);
    return;
  }
  for (int i = 0; i <= textLength - 16; i++) {
    lcd.clear();
    lcd.print(text.substring(i, i + 16));
    delay(300);  // Adjust delay for scrolling speed
  }
}
  • This function checks if the received text is longer than the LCD’s capacity (16 characters). If it is, the text will scroll across the screen, and if not, it will display the message directly.

5. Main Loop

Finally, in the loop() function, we continuously check for incoming Bluetooth data. If a message is received, it is displayed on the LCD:

void loop() {
  if (BTSerial.available()) {
    String receivedMessage = BTSerial.readString();
    Serial.print("Received message: ");
    Serial.println(receivedMessage);
    receivedMessage.trim();
    displayScrollableText(receivedMessage);

    if (receivedMessage == "hello") {
      BTSerial.println("How are you?");
      Serial.println("Replied: How are you?");
    }
  }
  delay(100);
}
  • BTSerial.available() checks if Bluetooth data is available to read.
  • BTSerial.readString() reads the data as a string.
  • The message is then displayed using the displayScrollableText() function.
  • If the received message is “hello,” the Bluetooth module sends back a “How are you?” reply.

Why This Circuit and Code Setup?

The circuit uses the HC-05 Bluetooth module for communication, which is one of the most commonly used Bluetooth modules in Arduino projects. The I2C LCD display allows us to send data to the screen using fewer pins, which is ideal for limited pin resources on Arduino boards like the Uno.

By using SoftwareSerial, we can establish communication with the Bluetooth module without interfering with the default serial communication. This allows you to monitor serial data on the computer (via the USB) while also handling Bluetooth data.

Conclusion

HC05 Bluetooth with LCD Display : This Bluetooth communication setup with an LCD display is a simple yet effective way to send and display messages wirelessly. It’s perfect for projects where you need to receive data from a mobile device or another Bluetooth-enabled device and display it on an LCD screen. The code is easy to modify for various other Bluetooth applications, and the circuit is beginner-friendly for anyone looking to start with Arduino projects.