HC05 HELP

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

HC05 HELP

Post by siliconchip »

Hi all

Ive been trying to learn about bluetooth comms over the last month and have purchased 2 HC05 modules ive researched the net alot trying to connect with my phone but ive failed at every point theres lots out there regards arduino but nothing regards flowcode and pic micros. All id like to start with is using my phone to turn an led on and off and progress from there but im unsure of how to start with writing within flowcode i have tried but im guessing at every point and failing. Has anyone a simple program using a pic micro that suits my needs so i can disect it and get too grips with the fundamentals of the HC05 and bluetooth so that hopefully i can go on to creating my own designs in the future

Cheers bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

I think I still have one of those modules somewhere. If so, I'll try and knock something up but I don't think it will be until later in the week.

Hopefully someone else will have jumped in by then :)

Regards

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer

i appreciate that any info i can get would be great to try to start understanding how to construct a program for these using pic micros

cheers bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Just found mine and it is a "slave"module. That means it can only operate as a "slave" and is intended to connect to a "master" such as a PC, Phone or Master module. It cannot connect to another "slave" at all.

Depending on vendor, there are Master modules, Slave modules and modules that can be configured as both, which can be a little confusing.

When I start to play with any comms module that utilises "AT" commands, I first connect the module to my PC so I can run a Terminal program. That way I can issue commands and see any response. Once I'm happy that my settings (baud etc) are correct I then use FC to create code. I consider this as valuable as a 1-second flasher sanity check :)

I'll try and get back to you later in the week.

Regards

Edit...
Forgot to say that as far as I'm aware, even numbered devices are slaves and odd numbered are configurable. My HC06 is a slave but a HC05 is configurable (which is preconfigured as Master and should pretty much connect straight out of box once paired).

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

A friend has a couple of HC05's lying around so I'm collecting them some time tomorrow (Sunday). When I get a chance my plan will be to connect one to my laptop via a USB-Serial and send some AT commands to see what's what.

With it as Master my android phone should hopefully find and connect, and if I'm running a terminal program at each end I should then be able to talk. Well, best laid plans and all that.....

There appears to be an extraordinary amount of conflicting advice regarding HC05/06 online so sending AT's and seeing any reply from my laptop seems a good starting point.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Using a USB-TTL converter plugged into my laptop I connected as follows

EN = N/C
VCC = +5v(on board 3.3v reg)
Gnd = 0v
Tx = Rx of USB gadget
Rx = Tx of USB gadget (via a voltage divider as the HC05 is +3.3v and not 5v tolerant)
State= N/C

Pressing and holding the on-board button I applied power which puts it into "AT Command Mode". The onboard LED blinks a couple of times every couple of seconds to indicate it is ready to accept AT commands.

I ran Terminal, but any terminal emulator should work. Settings I used were 38400-N-1 and you need both LF and CR at the end of your command.

Sending AT returned OK so I knew I was good so far :)

Many AT commands to do all sorts but the ones I'm most interested in are

AT+ROLE? and AT+ROLE=x (where x=0/1/2. for Slave / Master / Slave in Echo)
AT+CMODE? and AT+CMODE=x (where x= 0/1/2 for Manual Connect / Auto-Connect / Slave Loop

Issuing AT+ROLE? it returned 0 so it was already in Slave Mode
Issuing AT+CMODE? it returned 1 so it would Auto-Connect

As the module was already as I wished I issued AT+RESET which as you might have guessed restarts it. Note if you change certain parameters it will automatically restart thereby exiting Command Mode. Changing ROLE will do that.

With the module restarted I changed the terminal speed to 9600 as unless you configure differently it accepts AT commands at a different speed to transmission.

Using my android phone I could scan and see the HC05. Connection was iffy from downstairs but in the same room I could connect with ease (pin is 1234 but can be changed). Running a Terminal program on my phone I could make a serial connection and pass data without any trouble.

Rebooting the HC05 it came back up as before and again I was passing data with a couple of clicks on my phone :)

I believe crApple phones have issues connecting to these as I see forums are full of people seeking a solution and I think android v8 has an issue but don't quote me on these.

So now that I can connect and configure using my laptop by issuing AT commands it will be easy enough to do so from a microcontroller just by using the UART.

Next, I'm going to try and get something to talk to the module when in Master mode (AT+ROLE=1) and see what commands I need to issue. Once I establish them I can knock up a FC chart based on such.

Regards

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

Hi chipfryer

This is great ive just got hold of a usb serial device and need to make a plug up for this to the HC05 then i can follow along once again thanks

Bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi Bob

I don't think you will have much trouble. Remember to use a voltage divider / level shifter on the HC05 Rx pin and pay attention to your baud.... It caught me out :)

AT Command Mode = 38400 / Transmission = 9600

There are a few "terminal" apps in the Play Store, the one I used is "Serial Bluetooth Terminal" but no doubt others will work just as well.

To test with the HC05 in Master, I'll probably just use one connected to my laptop as a Slave, just as in post above, then configure one as a Master whilst connected to another laptop running Terminal.

Not sure when I'll get a chance but should hopefully be early in the week but I'm not expecting too much trouble in getting them to talk. As mentioned earlier it's always a good idea to use a Terminal program to verify that any module is behaving correctly. It saves so much trouble later.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Connected up in Command Mode to another HC05 as before and it too was set as Slave / Auto-connect.

I issued the following

AT+NAME=Master-HC05
AT+ROLE=1

This changed the name to Master-HC05, set it as a Master and automatically restarted it, which of course exits from Command Mode.

Using a different laptop I then connected to the previous module and issued the following

AT+NAME=Slave-HC05
AT+ADDR?
AT+RESET

This changed the name to Slave-HC05, returned it's address in Hex, then restarted..

The address is returned in the format aabb:cc:ddeeff but the point to note are the separators. When we connect we won't use colons.

Connecting again to the Master module I issued the following

AT+PAIR=aabb,cc,ddeeff,x

where aabb,cc,ddeeff is the address of the Slave module and x is the timeout in seconds. Note the different separator, this time it's a comma. This also restarted the device and within a few seconds they had paired.

Changing baud at both ends to 9600 on my laptops I could send data between them via the HC-05's. I then cycled the power to both modules and they came back up and paired automatically, allowing transmission between them.

If you are intending to have a PIC reconfigure the modules you will need a way to cycle power under PIC control such as a transistor to switch power or such like and to also hold the Enable pin high before applying the power. If you are only interested in pairing with the one device to send/receive data then it will probably be easier to configure using a PC and issuing AT commands as above. Once you have them configured the way you want you can then pretty much consider them as a transparent plug-n-play link.

I hope the above posts help and I'm sure there are plenty of examples of using the UART to send and receive data. I've not looked at using the HC05 component yet as I haven't got access to my development board. I might get a chance later in the week.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

With reference to the attached I've quickly knocked this up to toggle an LED via UART.

It isn't complicated and there are countless ways to achieve, so just consider it as a way you could do it (and probably more elegantly too <s>)

Three components and the WiKi will give further explanation and examples of each if needed:-

LED
UART(RS232)
Circular Buffer

After initialisation we enable an interrupt on RXINT0. This interrupt causes the program to automatically jump to an Interrupt Service Routine whenever we receive incoming data on the UART. In this routine we simply collect the incoming byte and store it in the Circular Buffer.

We then enter an endless loop. In this loop we check to see if the Circular Buffer, populated by incoming data received via the UART, contains our "trigger". If the "trigger" is present we then toggle the LED On/Off.

The "trigger" phrase in the example is LED but could be anything you wish within reason.

If you set one of the HC-05's to be a slave using your PC as per a previous post, and connect to your PIC (I've used a 16F1939) via level-shifters if required, and then connect with your phone running a terminal program, whenever you send LED from the phone the LED will toggle On/Off.

In my FC7 chart the Circular Buffer component appears to be invisible but it is in the chart underneath the LED somewhere. I probably did something stupid with it.

I haven't used FC7 in a while and I don't have any hardware available to test with so........Hopefully it should work :)

Regards
Attachments
HC05_Test.fcfx
(13.48 KiB) Downloaded 175 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer
this is great and shall try in hardware although i shall use a different pic i was going to use a pic12f1840, also in the circular buffer am i right in thinking the 3,1,1 sequence that the 3 represents LED ??

cheers bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

The 16F1939 was simply the last PIC I used :)

With regards to the Circular Buffer, the chart receives incoming data via the UART and stores it in the buffer. This helps ensure we never miss any incoming data.

I have set the trigger as "LED" (ascii 76 / 69 / 68) and if these three characters appear in sequence anywhere within the buffer then we will branch and toggle the LED.

What the 3-1-1 refers to in the Circular Buffer LookForValue component macro is

Value = "LED" (this is our trigger to look for and could be anything)
NumChars = 3 (number of characters to look for)
RemoveContent = 1 (removes the content of the buffer. In this example if we didn't it would constantly toggle On/Off after first instance)
ResetFind= 1 (we start each find from the beginning)

The Wiki has good example of using the CB and I think it's a great component.

Hope this helps.

Regards

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer
i have established comms with my HC05 using "cool term" terminal software and configured the bluetooth same as you, with the HC05 connected to my laptop i can connect via my phone and using serial blue tooth app i can send text from my phone to the laptop running the cool term software so i know comms are good i then modded your flowchart using a 16f1825 using the internal oscillator at 8Mhz with RA0 as my led output and built a circuit with a 1 and 2k resistor providing a voltage divider of 3.3v for the rx line of the HC05 once connected denoted by a slower led flash on the HC05 i have typed LED but no led lit then typed led then "led" but i cant get the led to light, i have the TX from the pic going to Rx via the voltage divider on the HC05 and RX from the pic straight to the TX on the 05

bob
Attachments
HC05_Test.fcfx
(14.32 KiB) Downloaded 141 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Connections appear correct and there is nothing obvious in the chart. However I think I read in the forum that it isn't a good idea to enable Low Voltage Programming in the Configurations.

I won't get a chance to play with hardware until the weekend, but things you may want to try are

Connect your USB-TTL gadget Rx to the chip Rx too. That enables you to see what is arriving at the chip. You should get L E D arriving without any line feeds or carriage returns between characters.

If good then it may be that the interrupt isn't triggering, so try the attached. It enables the RXINT then enters an endless loop. If the interrupt is triggered it will toggle the LED.

Regards
Attachments
HC05_Test_INT.fcfx
(13.21 KiB) Downloaded 142 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Following on from previous post.

This will flash the LED a few times as a sanity check before enabling the interrupt and entering an endless loop.

If called the interrupt will grab whatever is being received and immediately send it back out on the UART. If you are sending from your phone then you should see an echo.

You could also just use your USB-Serial adapter directly to the chip too. USB Tx to Chip Rx etc and send from your PC using a terminal.

Regards
Attachments
HC05_Test_INT_2.fcfx
(13.88 KiB) Downloaded 144 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer

ive had a quick play and left all my hardware as previous and first compiled the TEST_INT then uploaded to the chip, now from my phone when typing led the led on portA0 lights, did the same with TEST_INT2 at power up the led flashes 3 times and does nothing else even when typing led so it seems that good comms is working with TEST_INT now need to try and adapt toggling the led with an added command say "led off" big thanks so far for getting me this far

bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Glad to hear your making progress and the RxINT0 and CB appears to work.

With the original file, each and every time you send LED the port should toggle. If off then sending LED should switch it on, and if on sending LED should switch it off.

With the first INT file, anything coming in on the Rx will toggle the output, so every character received will cause the output LED to change state. If you sent two characters together you wouldn't see any change as it would happen too quickly (On then Off). Each time the macro is called would cause the output to change state.

INT-2 file doesn't do anything with the output LED, it just sends back out whatever has been received, therefore if you sent "1234" from your phone you should see "1234" being returned to the phone and if so proves the RXINT0 is working.

I just used "LED" as an example to toggle. You can off course use any trigger you wish, or multiple triggers even, using differing techniques to include variables. Just change the LookForValue macro to suit your needs.

Let me know how you get on.

Regards

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer

regards TEST_INT the led does light when led typed but didnt realise if led was typed again it would toggle the port and turn the led off however when typing led for the 2nd time the led does not extinguish in the calculation box i did alter LED=NOTLED TO LED=!LED but this didnt make any difference

bob

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Sorry, I meant HC05_Test will toggle each time you send "LED" via UART

HC5_Test_INT will (or should) toggle the LED upon each and every received character and was to check the RXINT0 was working.

HC05_INT-2 will just echo back what the UART receives and again was just to test if the interrupt was working.

I should have access to hardware tomorrow so can check then for any errors.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Got my boards.

Set up with a laptop connected to the Master HC05 and the Slave to the chip.

Sent LED but nothing happened......

I could blame it on the cold weather taking blood from my brain to more important places but nobody will believe me....:)

Problem was down to incorrect LookForValue settings. I initially had the ResetFind parameter continuing (1), which was causing the problem, instead of starting from the beginning (0)

Changing this had the chart working as expected. Each time I type LED the output LED changes state. In the attached I made this change and also echo back what is being received.

Regards
Attachments
HC05_Test_Working.fcfx
(13.67 KiB) Downloaded 141 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer
ive been messing with flowcharts all after noon trying different things and getting no where looked all through this forum but couldnt find anything that met my needs there seems to be so little about with regards flowcode , then i spotted your post i changed the target chip to a 16f1825 but i could not get it to work ive checked and double checked but my brain is now frying ive added my flowchart but i cant spot any probs and and a big thanks for the help so far it is appreciated

bob
Attachments
HC05_Test_Working.fcfx
(13.79 KiB) Downloaded 129 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Strange....

I have a 1825 so will have a play with it and update.

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Initially some issues but that seemed to be because I still had the chip plugged into my Dev-Board. Once on a bit of breadboard all worked to plan.

Connections as you would imagine.

I had a cheapo logic analyser connected to the chip A0 (LED Output) and also the Rx pin. Traces D0 / D1 respectively below.

I altered the chart to run on Int Osc @8Mhz and included a flash test to verify before enabling the interrupt etc.

You can see from the traces that the chip flashed three times as planned, then when the trigger (LED) arrives on the Rx it toggles the output a few mS later
Capture.JPG
Capture.JPG (31.27 KiB) Viewed 22272 times
Traces

On.JPG
On.JPG (37.57 KiB) Viewed 22272 times
Output changing state to On



Off.JPG
Off.JPG (35.64 KiB) Viewed 22272 times
Output reverting to Off

I've attached the chart for the 1825 that works for me. Hope it helps.

Regards
Attachments
HC05_Test_1825.fcfx
(14.72 KiB) Downloaded 138 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HC05 HELP

Post by siliconchip »

hi chipfryer

thanks for the update ive tried the new flowcode but still nothing ?? my phone confirms connection and paired with the HC05 but when typing led nothing ive tried upper and lower case plus it looks like the HC05 is pinging something back to me in the form of 2 green diamond shapes on my phone every time i type led should i be seeing led pinged back to me ?? if so there must be something wrong with the connection although i could get a led on with the TEST_INIT program im confused lol

bob

ps forgot to mention the led does flash 3 times at start up

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: HC05 HELP

Post by chipfryer27 »

Hi

Not sure why that would be the case, but if you are not seeing LED being echoed back, instead receiving garbled then it may point to a comms issue.

Phone (running your terminal program), HC05 and chip must be running at 9600. Worth double checking as countless times I have overlooked this :)

It may be worth taking the HC05 out of the equation by connecting your PC direct to the PIC via a USB-Serial adapter. Connected Tx-Rx you can then send to the PIC and should get an echo back.

Regards

PS
When testing I had a PC connected direct to chip, then moved on to sending via two HC05s.

Post Reply