Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

HC-05 Bluetooth Module

A HC-05 Bluetooth serial communications module is interfaced to an Arduino Nano microcontroller. A custom Android phone app is used to communicate with the Arduino Nano microcontroller via the HC-05 bluetooth module to control LED's, and exchange ASCII character data. This page provides the basic functionality to incorporte bluetooth communications into a project using the Arduino Nano.

There are many situations in which the external environment (water, dust, temperature, physical locality etc) makes housing and mounting the control components of an electronics project (i.e., I/O devices such as switches, screens, keypads etc) problematic. In such situations it would be convenient that the circuity and associated components such as relays, motors, lights, sensors and other peripherals could be mounted in an appropriately sealed housing and controlled via radio/RF. Thus allowing easy and convenient location of the "user interface" while simultaneously potentially making housing other circuit components easier/less expensive. See this Garden Light Project for an example where Bluetooth connectivity enabled a convenient user interface (remote control via Android Smart Phone App of LEDs for lighting of a walking path).

The ubiquitous availability of smart phones, with bluetooth capabilities, presents an excellent opportunity to utilise a smart phone for "radio control" of electronics projects, in particular, those in situations and locations that keyboards, keypads and output screens could be damaged by water, dust etc. Further, such utilisation of smart phones also presents the opportunity for storage of collected data onto SD card/phone memory for later downloading to PC/desktop computer.

In order to implement bluetooth connectivity of electronics projects with a smart phone for control/data storage two components are required, (1) bluetooth capability for the electronics project and (2) the appropriate "app" for the smart phone to send/receive commands/data (assumes the smart phone has bluetooth capability).

The first requirement is readily provided by the widely available HC-05 Bluetooth SSP (Serial Port Protocol) module. The HC-05 is a fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps modulation complete with 2.4GHz radio transceiver and baseband. The HC-05 uses CSR Bluecore 04-External single chip Bluetooth system with CMOS technology and with AFH (Adaptive Frequency Hopping) feature. Even more conveniently, the HC-05 is readily available from ebay suppliers mounted on a carrier board with 3.3V regulator and pull-down switch for AT-command programming.

Note, when purchasing the bluetooth serial communications module (particularly if from an ebay supplier) double check if a HC-05 or HC-06 module is being supplied. The HC-05 can be used in either slave or master mode, whereas, the HC-06 can only be used in slave mode.

The second requirement for availability of suitable smart phone "apps" is again readily met on phones based upon Android (no doubt other smart phones based upon alternative operating system software also have suitable "apps"). Available phone apps (focusing on the cost "free" apps) do provide the ability to send/receive data with many having "buttons" etc that could be utilised to start/stop LED's, motors etc. However, the ability to create a custom app that is tailor-made to your specific needs is highly desirable. This is why I favour Android based smart phones using the free Android Studio IDE, as this enables producing your own bluetooth customised app "relatively" easily, and does not require any cost outlay (assuming you have a PC and Android based smart phone).

The Circuit Details Section provides information about physical connection of the HC-05 to an Arduino Nano microcontroller, which in turn is connected to PC via USB. A number of peripherals (3 x LED's) are also connected to the Arduino Nano to provide test cases for control via Bluetooth and the Android phone app. Wiring connections are given in the Schematics Section.

The Testing/Experimental Results Section discusses the various steps used in testing the integration of bluetooth with the Arduino Nano via the HC-05. The majority of the effort was with developing the smart phone app. However, a fully functional bluetooth app was completed after a "week of afternoon's" never having been exposed to Android/Java previously - which is a testament to the excellent Android Studio IDE and online examples/tutorials available. But, this also presupposes you have some previous programming experience (i.e. "general" knowledge of "programming"), and preferrably "object orientated" programming so that you are aware of "classes", "inheritance" and "instantiating" instances etc.

While not attempting to be an Android smart phone programming tutorial, some details about the methods and techniques required specifically for bluetooth connectivity are detailed in the Testing/Experimental Results Section.


The circuit consists largely of the usual minimum requirements for a PIC (PIC18F248 dealt with here) that is, power supply, oscillator (external crystal oscillator - 40MHz) and in-circuit serial programming (ICSP). A number of "peripherals" are attached to provide test-cases for the Android based Smart Phone app to control via Bluetooth.

These include a number of LED's, a LM35 temperature sensor via the PIC on-board analog to digital convertor (ADC), and pulse-width-modulation (PWM) control of the speed of an attached 12V fan. Finally, the HC-05 bluetooth module is interfaced to the PIC18F248 via the serial port (which is also connected to a PC RS-232 connection). Thus information can be transferred from Smart Phone to PIC18F248 to PC and vice-versa.

The majority of the circuit is based upon the DIY PIC Development Board.

Circuit Operation

A "wall wart" power supply was chosen rather than constructing a dedicated DC power supply dropping/converting from an AC wall socket. Surplus chargers from laptops are readily available (in this case supplying 16-24V with 65W max) which provide not only a safer option (compared to construction from a suitable transformer, rectifier, connection to AC etc) but also a much more economical option (generally zero cost for a surplus charger, compared to ten's of dollars for a suitable transformer, let alone cost of ancillary circuitry, PCB etc).

The surplus laptop charger requires a suitable socket connection and a voltage regulator, in this case a LM317T, to provide the regulated 5V generally required by PIC microcontrollers. The power supply circuit is given in the Schematics Section. The LM317T circuit is the standard design direct from the datasheet, with input and output capacitors to provide smoothing and the resistor/potentiometer to provide selection of output voltage.

The HC-05 module (ebay purchased versions may vary, so check your particular component) accepts 3-6V, but the TX/RX pins are 3.3V. Since the DIY PIC Development Board has a 3.3V source (from LM1086 regulator), the HC-05 was powered by 3.3V. A simple voltage divider is used to convert the 5V output from the PIC TX pin to 3.3V suitable for input to the HC-05 RX pin. It was found that the 3.3V output from the HC-05 TX pin was sufficient for the PIC input RX pin, so not voltage conversion from 3.3V to 5V was necessary.

The LM35 temperature sensor is powered by 5V and connected to the PIC ADC in order to record temperature to provide a real-time signal for the PIC micrcontroller to transmit to the Android Smart Phone app via Bluetooth/HC-05. A 5V voltage reference is provide by a LM336 to provide a standard, stable voltage signal to the PIC ADC module.

A IRF540N n-channel MOSFET is controlled via pulse width modulation (PWM) to power and enable speed control of an attached 12V fan. A number of LED's are connected to the PIC output pins in order to provide a further test case for bluetooth control.

A MAX232 is used to enable RS-232 communication between the PIC micrcontroller and an attached PC.


Software/Firmware

The HC-05 module provides all the necessary components to enable Bluetooth wireless communication using a serial port protocol. This means that any firmware for the PIC microcontroller simply treats the HC-05 as if it was a "normal" RS-232 communications device. The HC-05 provides TX and RX pins (3.3V) which are simply connected to the RX and TX pins of the microcontroller, and then standard C compiler USART functions used to send and receive data. This "basic" functionality assumes that the default HC-05 settings (baud rates etc) are suitable and that the HC-05 will act in "slave" mode.

The HC-05 is also capable of accepting AT commands in order to change baud rates, device ID, etc and if operating in slave or master mode (1). This functionality is not generally required (i.e., the HC-05 settings are determined/fixed at the same time the firmware is loaded into the PIC microcontroller) and any changes to the HC-05 are more conveniently done via a USB-RS232 TTL converter allowing a Windows PC serial terminal program (or equivalent for non Windows PC platform) to send AT commands via a serial COM port.

This enables simple configuration of the HC-05 without complicating the firmware necessary for the PIC microcontroller. The communications instructions between PC and HC-05 via USB-RS232 TTL (for changing HC-05 settings) and between PIC microcontroller and HC-05 (for serial wireless transfer of data) are detailed in the Testing/Experimental Results Section below.

The HC-05 is envisaged for use initially to provide wireless serial communications in slave mode, thus another device (Android based Smart Phone in this case) must initiate the Bluetooth communciations (the "pairing" of devices etc). This again makes the PIC microcontroller firmware relatively straight forward, with the Android phone operating system/applications automagically provides the necessary communications channel setup steps. The development of the software for a custom Android based phone app is outlined in the Testing/Experimental Results Section below.

This means a smart phone can be used as a "terminal" for a PIC microcontroller based circuit enabling control of peripherals, data logging and storage.


Note: Image loading can be slow depending on server load.

  • HC-05 SchematicHC-05 Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    HC-05 Schematic

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
Resistors
2R1,R610K1/4W, 10% 
1R21k1/4W, 10% 
1R33h1/4W, 10% 
1R42k1/4W, 10% 
1R51501/4W, 10% 
3R7-R93301/4W, 10% 
Capacitors
2C1,C222pFCeramic 
1C30.33uF 
1C40.1uF 
5C5-C91uF 
Diodes
2D1,D31N-4001 
1D2LM336-2V52.5V voltage reference IC
3D4-D6LEDRed, Green, Yellow LED
Integrated Circuits
1U1PIC18F248PIC microcontroller  datasheet
1U27805Linear Voltage Regulator  datasheet
1U4MAX232ERS232 Driver/Receiver datasheet
1U6LM35Temperature Sensor datasheet
Miscellaeous
1J1CONN-H55-pin connector for ICSP
1SW1SW-SPDT 
1X110MHzCrystal Oscillator
1Z1LHC-05Bluetooth Module datasheet
Description Downloads
HC-05 - Bill of Materials Text File Download

Testing of the HC-05 Bluetooth module consisted of a number of stages. Initially, a USB-TTL converter was used to provide connection of the HC-05 to a PC via USB cable. This enabled using a Windows PC terminal program to send AT commands to the HC-05. Therefore, basic operation of the HC-05 (i.e., did it power up correctly and return device ID etc) could be checked before moving on to custom circuitry and software. This is important as the quality of HC-05 boards from ebay suppliers can sometimes be low (although I have experienced no trouble to date with the HC-05's purchased).

Secondly, the HC-05 is then tested within a circuit interfaced to the Arduino Nano connected to various peripherals. The Bluetooth communications being conducted via free "apps" available for Android based smart phones. This enables testing of the firmware within the Arduino Nano that data can be sent and received via Bluetooth to the Arduino Nano, controlling various peripherals.

Finally, a custom designed and implemented app is produced to send and receive data from the Arduino Nano via bluetooth. This staged process allowed the development and testing to proceed smoother in that errors and bugs could be isolated to the current phase.

Initial Testing with USB-TTL converter

The HC-05 accepts AT commands (when in command mode) which is useful to test if the module is powering up correctly in the first instance. A USB-TTL converter (I used a $2 converter purchased from ebay) makes it convenient to connect the HC-05 to a PC via USB (see the Schematics Section for the simply wiring required). The USB-TTL converter takes power from the USB socket (no other power supply necessary), and the particular converter I had also output 3.3V which again is convenient to power-up the HC-05.

The initial step with the USB-TTL converter is to downloaded the necessary USB drivers (if using Windows) which I sourced from the Prolific Technology Inc site (2) which at the time of download was a file titled "PL2303_Prolific_ DriverInstaller_v1.12.0.zip". Unzip the file, click the installer and follow the usual steps for installing the driver software. When the USB-TTL converter is then connected to the USB port of the PC, Windows should do the usual driver installation steps.

The next step is to use a suitable serial port (COM) terminal emulation program to send commands to the HC-05 via the USB-TTL converter. I used "Terminal" which is very useful for debugging serial communication and is available for free (3) (download the zip file, unzip, and use the installer as usual for a Windows program).

The last photograph in the Photographs Section shows how the HC-05 is connected to the USB-TTL converter and then to the PC (Schematic Section for wiring). In order to send AT commands to the HC-05 via the USB-TTL connection, the small-button on the bottom right-hand side of the HC-05 must be pushed when powering up (i.e. push and hold the button and then insert the USB cable into the PC USB port, when the LED on the HC-05 slowly flashs (~every 2 seconds) the button can be released.

With the USB-TTL connected to the HC-05 and the PC (and the HC-05 is in AT command mode) start the Terminal emulation program on the PC. Connect to the appropriate COM port (baud rate 38400, 8 bits, no parity, 1 stop bit). Enter the letters "AT" (without the quotes) into the transmit field (ensure the CR+LF is selected as equal to CR) and then click send. The letters "OK" should be received back indicating that the HC-05 is in AT command mode.You can then try other AT commands such as "AT+ADDR" which will return the device address of the HC-05.

Note that by using the push button on the HC-05 on power-up causes the HC-05 to enter "mini" AT command mode, which only allows a subset of the AT commands to be used (and a baud rate of 38400 is set and cannot be changed). There are a number of other methods with which to put the HC-05 into "full" AT command mode (which is necessary to use the NAME command for example) (4), but this is not necessary for basic usage.

Success with getting the HC-05 to enter AT command mode and return information to AT commands means that the HC-05 is operational and ready for use in the PIC microcontroller circuit. Note the default settings for the HC-05 for normal communications (i.e. while not in AT command mode) is 9600 baud, 8 bits, no parity, and 1 stop bit.


Testing Arduino Nano/HC-05 with 3rd Party Phone Apps

When the HC-05 has been checked to be operational (see previous section detailing testing with USB-TTL converter and serial port emulation program), the module can be interfaced to the Arduino Nano and bluetooth communications checked using free Smart Phone apps that are specifically for HC-05 to microcontroller communication. I am using Android based Smart Phones and via Google Play successfully used apps titled "Arduino BlueControl" by "BroXcode" and "Arduino BlueTooth" by "Circuit Magic" (there are many others that are also likely suitable).

The Schematics Section details the wiring connections necessary to interface the HC-05 to the Arduino Nano I/O pins (and a number of peripherals to enable testing of control via Bluetooth). The firmware for the Arduino Nano is relatively straight forward as the HC-05 "looks like" a normal RS-232 serial communications port. The following code snippet 1 demonstrates basic functionality for the Arduino Nano to receive bytes of data from one of the free Bluetooth HC-05 phone apps previously mentioned, and then echo the received data to PC via RS-232.

Code Snippet 1:


 #include 

 // default hardware serial port/pins for connection to PC
 // the connected PC/serial monitor allows user data entry
 // and display of sent/received data via bluetooth

 // setup for connection to bluetooth module
 const byte rxPin = 3;
 const byte txPin = 2;
 SoftwareSerial BTSerial(rxPin, txPin); // RX TX

 void setup() {
   // define pin modes for tx, rx for bluetooth module
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   BTSerial.begin(9600); // instantiate "start" bluetooth
   Serial.begin(9600);   // instantiate "start" serial port

   Serial.print("started"); // output prompt to show program running OK
 }

 String messageBuffer = ""; // buffer to accumulate received characters
 String message = "";

 void loop() {
   // loop for receiving data via bluetooth module
   while (BTSerial.available() > 0) {
     char data = (char) BTSerial.read();
     messageBuffer += data;
     if (data == ';') { // ";" character used to denote "end of message/string"
       message = messageBuffer;
       messageBuffer = "";
       Serial.print(message); // output to serial monitor/connected PC display
     }
   }
   // loop for sending data via bluetooth module (data entered via serial
   // monitor/connected PC
   while (Serial.available() > 0) { // receive data from the serial monitor/connected PC
     char data = (char) Serial.read();
     messageBuffer += data;
     if (data == '~'){ // "~" character used to denote "end of message/string"
       message = messageBuffer;
       messageBuffer = "";
       Serial.print(message);   // echo to serial monitor
       BTSerial.print(message); // send to bluetooth terminal
     }
   }
 }
   				

The above code snippet, coupled with one of the free Android phone apps enables rapid testing if the HC-05 has been successfully interfaced to the Arduino Nano enabling Bluetooth serial communication. The code snippet shows the use of "delimiters" (i.e., the ";" for receiving and "~" for sending) together with the concept of a message buffer. This enables the program to handle incoming individual characters to form strings which can then represent "commands". If the application only needs to receive individual characters, which will be interpreted via appropriate IF THEN ELSE statements to determine necessary actions, the use of "message buffers" could be depensed with.

The Downloads Section at the bottom of Testing/Experimental Results has the full source code used for communication with the Arduino Nano via the HC-05 bluetooth module to control LED's. See xxx.html for details on the Arduino Nano receiving data from a thermocouple (with a real time graph of the data on the Android phone app) as demonstrated working in the Video Section.


Custom Developed Phone App

There are many available phone apps (focusing on the cost "free" apps) that do provide the ability to send/receive data with many having "buttons" and other user interface elements that could be utilised to start/stop LED's, motors etc. While this is useful for initial development and testing of custom circuits incorporating Bluetooth communication functionality, the ability to create a custom app that is tailor-made to specific needs is highly desirable. I focus on Android based smart phones using the free Android Studio IDE, as this enables producing your own bluetooth customised app "relatively" easily, and does not require any initial cost outlay (assuming you have a PC and Android based smart phone).

The first step is installing the Android Studio IDE which provides the software development environment necessary for implementing, testing and uploading a custom app to an Android based smart phone. Android Studio is developed by Google and is free with download and installation instructions located at this reference (5). There are numerous online tutorials teaching how to use Android Studio and the fundamentals of Android app development (6),(7). For those with prior programming experience, the Android Studio IDE and associated tools makes app development relatively straight-forward. While for those first time programmers, the Android Studio IDE and Java is a good starting point for learning in any case.

After having "mastered" the concepts of Android Studio and the framework behind Android based apps, the next step is dealing with incorporating Bluetooth functionality into an app. Again, there are numerous online tutorials and recommend the following reference as a starting point, which enables simply pairing the Android smart phone with the test circuit and toggling LED's (8).

Once completing this simple toggling of LED's on and off via Bluetooth and Android phone app, extending the concepts to control motors and other peripherals is then just a straight-forward use of the various user interface elements provided by the Android Studio IDE, in-conjunction with the appropriate firmware code within the PIC microcontroller to act upon received data to control peripherals as desired.

I won't include any further detailed instructions about the Android Studio IDE, developing apps for Android based phones and or incorporating bluetooth communications funtionality, as there are already numerous excellent online sources, and the depth and breadth of necessary information really depends upon the readers prior experience and requirements. However, for those with particular questions, please use the comments section below and I will attempt to answer your queries.


Downloads

Description Downloads
Example code with Arduino Nano/HC-05 Bluetooth interface, as demonstrated in the Video Section: CCS C Source Code Download
Header File Source Code Download

The HC-05 RX and TX pins are 3.3V and therefore voltage conversion will be necessary if interfacing to a 5V microcontroller or similar. A simple resistor voltage divider is sufficient for 5V to 3.3V when connecting the TX pin of the PIC microcontroller to the RX pin of the HC-05. It was found that no conversion was necessary for the 3.3V from the HC-05 TX pin to the RX pin of the PIC microcontroller.

As discusssed in the Testing/Experimental Results Section, the use of a USB-TTL converter to provide connection of the HC-05 to a PC via USB cable is useful for initial testing of the HC-05 module. This enables using a Windows PC terminal program to send AT commands to the HC-05 to test basic operation. This is very useful if there are any doubts about the quality of the HC-05 module and or if it has been powered up with incorrect polarity and or voltage etc.

The actual operation of the HC-05 in slave mode as a wireless serial communications peripheral (i.e. "wireless" RS-232) is relatively straight-forward (remembering HC-05 RX pin to TX of PIC, and HC-05 TX to RX of PIC). Note, there is no need for a MAX232 or similar as no voltage conversion from the RS-232 standard to TTL levels is necessary with the HC-05. The PIC microcontroller firmware only requires the standard USART routines generally available with various compilers, recommended with an interrupt driven approach for serial data capture using a circular buffer. Baud rates up to 38400 were tested (20MHz PIC) without apparent problems.

Development of custom apps for smart phone bluetooth communications to the HC-05 are relatively straight forward once the Android Studio IDE is installed correctly and the necessary plug-ins are downloaded. Steps I followed are given in the Testing/Experimental Results Section. I find it necessary to start Android Studio (using Windows 8.1 on a PC) by right-clicking and "run as administrator" to ensure that the Android Debug Bridge (ADB) software is loaded/starts - which is necessary to upload developed apps to an attached device (e.g. phone). Finally, it is obviously important that an attached phone is in "developer mode" when trying up upload/debug software from Android Studio.


Note: Video loading can be slow depending on server load.

The video below shows demonstration of a custom developed Android phone app using bluetooth to control the speed of a 12V fan via PWM, toggling LED's on and off and capturing real time data from a LM35 temperature sensor via ADC interfaced to a PIC18F248 and HC-05. The demonstration code is discussed in the sections above.

ref001: https://www.itead.cc/wiki/Serial_Port_Bluetooth_Module_(Master/Slave)_:_HC-05

ref002: http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225&pcid=41

ref003: https://sites.google.com/site/terminalbpp/

ref004: http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/

ref005: https://developer.android.com/studio/install.html

ref006: https://www.ntu.edu.sg/home/ehchua/programming/android/Android_HowTo.html

ref007: https://developer.android.com/training/index.html

ref008: http://diyhacking.com/arduino-bluetooth-basics/

Comments/Questions (2)

Topic: HC-05 Bluetooth Module
Sort
oldbuttkicken says...
I installed Android Studio and did the "hello world'' test app with the emulator no problems. But, I can't see my Samsung phone when attached via USB and so can't upload to check on my actual phone, any ideas??
1st September 2016 12:32pm
FadsToObsessions says...
My first suggestion/question is have you put the phone in "developer mode" (just google android phone developer options for how to do this if necessary). You could also check the USB cable itself (if not original, third party cables sometimes are only for charging, you need a "data cable"). Hope this helps.
1st September 2016 6:32pm
Page 1 of 1

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with correct Membership Level can download.


Membership starts at only $2, so join now!