Home > Uncategorized > How to Connect HC-05 to Arduino Nano

How to Connect HC-05 to Arduino Nano

July 21st, 2020

The HC-05 is a simple and affordable Bluetooth module that can be connected to the Arduino Nano in a few simple steps and transfer data as a master and slave drive. In this tutorial, we will learn how to wire the HC-05 to the Arduino Nano and write up a simple Bluetooth module to get you started!

Part One: Wiring

As seen from the above document, you will need a few jumper cables, a 1k ohm resistor, and a 2k ohm resistor. You can also use a breadboard to connect the module to your Arduino board. (See below diagram) *PLEASE NOTE THATTX SHOULD GO TO D3 AND RX SHOULD GO TO D2, THE DOCUMENT HAS IT FLIPPED.

To start all the way on the right, we have the VCC pin, which will connect to the 5V pin on the Arduino Nano. The VCC pin will receive power in the form of 5V from the Arduino Nano. Next we have the GND pin, which connects to the corresponding GND pin on the Arduino Nano. This will just send back the voltage to the Arduino Nano and complete the circuit. Now we have the TXD or Transmitter Pin, which transmit data in the of 3.3v from the HC-05 to the Arduino Nano’s D2 Pin. The Arduino Nano can handle this, but when we move over to the receiver pin, the Arduino Nano is transmitting 5 Volts to the HC-05. This is too much for the HC-05 as it sends and receives data in the form of 3.3V, and we risk damaging the module. So, we have to put 2 resistors as seen in the diagrams above. Now that we have successfully wired our HC-05 module, we can move onto the code!

Part 2: Code

Before we get started with the code, your going to want to download BT Terminal from the Google Playstore so that you communicate between your module and phone. Now for the code:

#include <SoftwareSerial.h>

// Define the data transmit/receive pins in Arduino

#define TxD 2

#define RxD 3

SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth

void setup() {

mySerial.begin(9600); // For Bluetooth

Serial.begin(9600); // For the IDE monitor Tools -> Serial Monitor

// Any code that you want to run once....

}

void loop() {

// put your main code here, to run repeatedly:

boolean isValidInput; do { byte c; // get the next character from the bluetooth serial port

while ( !mySerial.available() ) ; // LOOP...

c = mySerial.read(); // Execute the option based on the character recieved

Serial.print(c); // Print the character received to the IDE serial monitor

switch ( c ) {

case 'a': // You've entered a

// Do the code you need when 'a' is received.....

mySerial.println( "You've entered an 'a'" );

isValidInput = true;

break;

case 'b': // You've entered b

// Do the code you need when 'b' is received.....

mySerial.println( "You've entered an 'b'" );

isValidInput = true;

break;

default:

// Do the code you need when any other character is received.....

mySerial.println( "Please enter 'a' or 'b'" );

isValidInput = false;

break;

}

} while ( isValidInput == true ); // Repeat the loop

}

I borrowed the following code from the people at TechDepot Egypt, check them out here: https://www.instructables.com/id/Connecting-HC-05-Bluetooth-Module-to-Arduino/

Part 3: Putting it All Together

Run the program after you have entered in the code. Go to your phone and connect to the Bluetooth module under settings first. It will ask you for a 4 Digit pin. This will most likely be 1234 or 0000. Once that is done go back to the BT Terminal app and connect to the module under them. Now you can send any messages from the app and use the HC-05 Bluetooth module to your advantage!

Uncategorized

  1. No comments yet.
  1. No trackbacks yet.