2017년 3월 30일 목요일

Arduino and HC-06 (ZS-040)


The HC-06 is a slave only BT module that is fairly easy to use with the Arduino using serial communication. Once it is connected it simply relays what it receives by bluetooth to the Arduino and whatever it receives from the Arduino it sends to the connected device. There are several slightly different versions of the HC-06, however, all seem to use the same firmware and have the same AT commands. The ones I have are labelled as zs-040. I also have some HC-05s which share the same PCB and are also labelled as zs-040.
HC-06 zs-040
The HC-06 defaults to AT mode at power on. This is indicated by a rapidly flashing LED. After the HC-06 is connected to another device the LED stops flashing and is constant on.

Connections

The Bluetooth module the ZS-040 is based on, the EGBT-046S, is a 3.3V device. The HC-06 breakout board has a 3.3v regulator that allows a larger input voltage to be used, in the range of 3.6 to 6 volts. The RX pin can still only accept 3.3V though. This means a voltage divider is required to connect to a 5V Arduino. A simple voltage divider can be created using 2 resistors. I am using a 1K ohm resistor and a 2K ohm resistor. The Arduino will read 3.3V as a HIGH so the HC-06 TX pin can be connected directly to the Arduino.
HC-06
HC-06 Vin to 5V (can be from the +5V out from the Arduino)
HC-06 GND to common GND
HC-06 RX to Arduino pin D3 (TX) via a voltage divider
HC-06 TX to Arduino pin D2 (RX) connect directly
Arduino HC-06

Test Communication With The HC-06

After connecting everything we need to talk to the HC-06. We can do this by using software serial on the Arduino. I use software serial to talk to Bluetooth modules and use the hardware serial for debugging.
The following sketch takes whatever is entered in to the serial monitor on a host computer and relays it to the HC-06. The sketch also takes whatever the HC-06 outputs and forwards it to the serial monitor. The Arduino is acting like a relay station between the serial monitor and the BT module.
The HC-06s I have have a default baud rate of 9600. Other modules have a different baud rate. If 9600 doesn’t work try other speeds. 38400 is also very common. Once you have communication working you can change the baud rate to suit your needs.
// Basic Bluetooth sketch HC-06_01
// Connect the Hc-06 module and communicate using the serial monitor
//
// The HC-06 defaults to AT mode when first powered on.
// The default baud rate is 9600
// The Hc-06 requires all AT commands to be in uppercase. NL+CR should not be added to the command string
//
 
 
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-06 TX to the Arduino RX on pin 2. 
// Connect the HC-06 RX to the Arduino TX on pin 3 through a voltage divider.
// 
 
 
void setup() 
{
    Serial.begin(9600);
    Serial.println("Enter AT commands:");
 
    // HC-06 default serial speed is 9600
    BTserial.begin(9600);  
}
 
void loop()
{
 
    // Keep reading from HC-06 and send to Arduino Serial Monitor
    if (BTserial.available())
    {  
        Serial.write(BTserial.read());
    }
 
    // Keep reading from Arduino Serial Monitor and send to HC-06
    if (Serial.available())
    {
        BTserial.write(Serial.read());
    }
 
}
The HC-06 zs-040 expects commands to be in upper case and does not require carriage return and new line (\r\n) characters.
Open the serial monitor and select a baud rate of 9600 and ensure “No line ending” is selected from the drop down list at the bottom of the window.
Enter “AT” (no quotes) into the top text box and hit Send. If the HC-06 likes you it will say OK. AT is a basic communications test command that allows you to check the HC-06 is connected and communicating.
HC-06 Commincate with Arduino

AT Commands

The HC-06 has a limited number of commands. You can rename the device, change the baud rate, and change the PIN/password. That’s about it.
CommandReplyComment
ATOKCommunications test
AT+VERSIONOKlinvorV1.8Firmware version.
AT+NAMEmyBTmoduleOKsetnameSets the modules name to “myBTmodule”
AT+PIN6789OKsetPINSet the PIN to 6789
AT+BAUD1OK1200Sets the baud rate to 1200
AT+BAUD2OK2400Sets the baud rate to 2400
AT+BAUD3OK4800Sets the baud rate to 4800
AT+BAUD4OK9600Sets the baud rate to 9600
AT+BAUD5OK19200Sets the baud rate to 19200
AT+BAUD6OK38400Sets the baud rate to 38400
AT+BAUD7OK57600Sets the baud rate to 57600
AT+BAUD8OK115200Sets the baud rate to 115200
AT+BAUD9OK230400Sets the baud rate to 230400
AT+BAUDAOK460800Sets the baud rate to 460800
AT+BAUDBOK921600Sets the baud rate to 921600
AT+BAUDCOK1382400Sets the baud rate to 1382400
To get the modules firmware version enter “AT+VERSION” (no quotes):
HC-06 zs-040 AT+VERSION
The modules I have report they are using version linvorV1.8 which seems to be common for many HC-06s.
Note: Windows cannot use baud rates above 115200. If you are using Windows and you accidentally set the baud rate higher than 115200 you’re screwed!
After confirming that the HC-06 is working and communicating we can try to connect to an Android device.

HC-06 Connecting to an Android Device

Since the HC-06 is a slave only device, the connection must be started by another device. Below are the steps to pair and then connect with an Android device.
I am using an app called Bluetooth Terminal which is available for free on google play.
Before you can connect to the HC-06 you need to pair it.
Power on the HC-06. The LED will flash rapidly.
Open Settings on the Android device and select Bluetooth.
If your device does not auto-scan you will need to manually scan for available bluetooth devices. The HC-06 should appear in the list.
Select the HC-06. You will be asked for the pin. The default pin is “1234”.
The modules name may include the mac address; a series of hexadecimal numbers.
HC-06 Pair with Android device
After the HC-06 is paired you need to communicate with it somehow. To test things are working you can use a BT terminal program such as Bluetooth Terminal available on Google Play
Install and open Bluetooth Terminal.
Open the menu, icon at the top of the screen.
Select “Connect a device – Insecure”. This brings up a list of available devices.
Select the HC-06
Once connected, “connected: HC-06″ is displayed at the top of the screen.
HC-06 connecting to Android device
Make sure the Arduino serial monitor is open and everything you enter into the Android Bluetooth Terminal will be echoed in the serial monitor.
HC-06 communicating with Andriod device
In the Arduino serial monitor, select “Both NL & CR” at the bottom of the window and whatever you type in the serial monitor will be sent to the Android Bluetooth Terminal.
HC-06 communicating with an Android device
If NL&CR are not selected, the Arduino will still send the data but the Bluetooth Terminal program will not display it until it receives a carriage return / newline.
Next step. Turning an LED on and off.

Turning a LED On and Off by Bluetooth

I have updated and moved the Turning an LED on and off guide to its own post

Update

I have new HC-06 zs-040 modules that have the hc01.comV2.0 firmware. See here.

Need help with Bluetooth app


Have you done any Bluetooth programming in App Inventor2?
I am creating an app that will allow my Samsung Galaxy S5 Android phone to talk to the BLE Sheild on an Arduino.
I can't find any tutorials on how to create an app in App Inventor2 that makes use of the various Bluetooth functions.  Can you point me to one?
Do you have any .aia files that show how to use Bluetooth that I can look at for ideas on how to implement this?
Also, what Bluetooth board did you use? (I had little success so far with 1Sheeld and BLE Shield 2.1 by RedBear, so I might get one that worked for you)

--
For a very basic introduction on sending data from an Android device to the Arduino take a look at Arduino and HC-06 (ZS-040)
The Arduino sketch and the aia file can be downloaded at the bottom of the page.

For a slightly more advanced version take a look at arduinoBTcontrol. The download includes the Arduino sketch and the app only. If you are interested in the aia file let me know and I can post it.

--
Great Arduino and Bluetooth tutorials.  FAQ-worthy!

--

How to configure a HC-06 Bluetooth adapter for MultiWii


https://www.youtube.com/watch?v=jSxcEZHsV0A

In this video I show how to easily configure your HC06 Bluetooth adapter to work with your Multiwii board.

This board may come set to the wrong baud speed for the Multiwii and in this video I show you how to change it using kit you probably already have in your toolkit.

Commands I used are:

AT+NAMEXXXXX to change the name of the board
AT+BAUD8 to change the baud rate to 115,200 for the MWC
AT+PIN2134 to change the pin number for pairing

Thanks for watching, please comment and subscribe.

Happy flying!










i dont get "OK" for sending "AT" 

what troubleshooting have you already tried?

Mine won't change Baud rate

Happy to help but please watch https://www.youtube.com/watch?v=olIqTiSJHIY and come back to me with more detail of what you've tried..
I have looked at every video that relates to my device, gone through many forums, didn't get much from the supplier, I have done my best to research but nothing has the solution. 

that a point, maybe I have to upgrade the frimware... mine is a keynote HM-10 bluetooth 4.0 v2. I have looked at forums specificly for HM-10 but the methods are no diffrent; the codes that is. it replys to 'AT+BAUD8' with 'Ok+Set:8' and yet I goto 115200 and it doesn't work, while 9600 does, its the same with all rates 

I'd look at using the ones we've played with here. There were a lot of changes to the specs of the later adapter and we've not used them. Sounds like you are doing the right things

What module would you recommend for Bluetooth for naze32 

We setup one in out Nase32 series - the answer is in there. Google 'Naze32 Bluetooth painless360' to find it.. Hope it helps.

will the Bluetooth M connect up with a ipad

sorted thanks

got my new bluetooth adaptors tonight i already have two but i decided i really need to start renaming them now...!!!! watched the clip and will be doing it in a minute.. thanks let you know how i get on...

Hi PL. I don't seem to be able to get my FTDI to work in Arduino. So far: - Running win 10, have tried also in win 8 compatibility mode. - Checked device manager. FTDI recognised, running fine baud rate 9600. - Solid green and flashing red led on BT module when wired up to FTDI. When I get to "Serial monitor" and type "AT" nothing happens? No "OK" Have tried all baud rates (You never know your luck) to no avail. Any ideas?

Swapped rx and tx? 

Have you tried all the baud rates in the program? +Painless360 Yes :'( Bugger. I notice that the FTDI is different from ours, best of luck.. 

thanks for the suggestion. Just tried with no luck. 

Did you find the problem ? I can't connect my HC-06 with ARDUINO. No "OK" when I type "AT" 

“Coco Racer” Pacaud Sorry mate no dice for me. Hope you can figure it out for the both of us 

I tried Arduino and putty, both didn't work, I'm gone :( If you are not using the 'real' FTDI chips then you are in for a world of pain. See the many many many answers already on the videos to see your options.. I have this one. And I read the comments but didn't find the thing about "real ftdi" : http://www.banggood.com/FT232RL-FTDI-USB-To-TTL-Serial-Converter-Adapter-Module-For-Arduino-p-917226.html 

“Coco Racer” Pacaud Ey mate. PL means not a Chinese clone. You and I my friend have a Chinese clone (Not a 'real' FTDI chip) Just means we have to open our wallets a little more. Clones can work but it is definitely hit and miss. Predominantly miss. 

Couldn't have said it better..   

Tada.

Hi PL Will this be OK? https://nicegear.co.nz/cables-connectors/adafruit-ftdi-friend/ 

Maybe - we always use 'FTDI Basic' boards here.. 

Voila? https://nicegear.co.nz/electronics-gear/sparkfun-ftdi-breakout-5v/ 

That looks like a few we use here - best of luck with it and I hope that one isn't a fake. Happy flying! 

Thanks. For the price it better not be

ok:/ but 25€ the FTDI.... 

“Coco Racer” Pacaud Nope. That's $25 nzd. About €14. But I'm sure you'll get it cheaper than that. NZ tends to pay more for most things as we are at the "bottom of the earth"

This is so frustrating. I have followed your every step and can't even get the first command to work, pleas watch my 17sec vid and help.
https://youtu.be/QMWiwt6uf_s 

I know you will ask what troubleshooting have I done, I have tried all Baud rates and all board types, the only reason I got the BT for the Naze32 was because of your videos, if it all works it would be great! 

Swapped Rx and Rx cables round? If that Durand work then the FTDI or BT module need checking.. 

yep, don that, I think I might have a duff Bluetooth board.

superuuu. maacha

I get no choice of port when I connect, the border is seen but no drivers are there. 

+Brian Tapp Happy to help but please watch https://www.youtube.com/watch?v=olIqTiSJHIY and come back to me with more detail of what you've tried..

Very good video, very easy to understand even for those of us who are not pros :) Thank you for fixing this problem for me!

what processor what board and what programmer do i use i cant find info anywhere 

You don't need to set that, the board should come with the software on it you need. The board and CPU selection has no affect on how the Serial Monitor works - have you tried it? 

I see that it picked it up automatically. The problem is none of the AT commands are working. it's set at 5600 and is working but I cannot configure it using Arduino

Hi Painless, I'm trying to connect HC-06 Blutooth adapter to Multiwii, initially to changes buad rate, I connected via FTDi adapter to my PC. My PC detects the FTDI as COM6 and HC-06 as COM7, there was a blinking light in HC-06, Once I open Arduino and select COM7 and open serial monitor, the blinking light becomes solid light in HC-06 with buadrate 9600 and "no end line". If I send AT, there is no response HC-06. My connectioned HC-06 to FTDI is GND to GND, 5V to VCC, RX to TX, TX to RX. I tried even different buad to connect but no response. I'm able to detect the blutooth signal in my mobile. Please help me. 

Disable the BT module on the PC and use the FTDI com port.... Best of luck! 

Did the same and used COM6 and no response after sending"AT" , the bluetooth module just blinks. Tried with 9600 and different buad rate too. 

I'd go back to the vendor then, best of luck!

THANKS!

Hi, I'm running into the same problem as others... My FTDI is working fine (I can use it to configure a minimOSD board, so that proves it's working), and I can connect to the HC-06 fine via BT from my PC, phone, etc, but no serial response of any sort back from the HC-06. Yes I've done all the basic troubleshooting... checked drivers, swapped lines, etc, etc. Any new thoughts on this? This is making me feel like some sort of noob!!! 

Connecting to the BT adapter using the phone proves very little. In my experience it's the connection from the BT to FC that causes the problems. You need to confirm that the baud rate is correct for the FC and then make sure you have the tx/rx lines connected.Best of luck.

Good work! thank you so much ;)

Hello. First i'd like to say that your videos are well organized and easy to follow, thank you. I'm having trouble communicating to my HC-06. The one i have doesnt need an adapter like yours. It connects directly to the my MW328p. It pairs with my galaxy phone but it does not show any Parameters. Ez Gui App tells me "no data received". I've follwed this video ( https://www.youtube.com/watch?v=Uwn5DGf1bko)  but nothing works thus far. Can you help me? 

What troubleshooting have you done? Did you connect it to the PC to configure it? 

I have tries different apps on my phone. Switched the tx and rx cables round on the FC and module. I've tried communicating with serial port in arduino type all the commands but no response from Hc06. Nothing is working except it pairs with my galaxy phone.  

And have you setup the baud speed? 

Should I have done so in the PC or in arduino?  

In Arduino software - see 1:17 in the video for the answer to your question.

Best of luck.

The first tutorial i can follow congradultations! :)

Hi ! i'll do the same wiring diagram than you and the AT Commands not responding with my HC-06 BT module .... CAN YOU HELP ME ??? 

Why are you shouting? What troubleshooting have you done? 

i wiring like you show in the video but i have only a red led blinking when i plug it with the FTDI... No response with AT commands... 

So have you swapped the rx and tx cable? Tried every baud rate? Checked that the FTDI driver is loaded properly and you can see it in device manager? ? 

My FTDI module works fine, i already load Multiwii 2.4 in my MultiWii v2.5 card (and my QuadX works fine). 

My Wiring Diagram: 
FTDI RX
BT TX FTDI TX
BT RX FTDI +5v
VCC BT FTDI GND
GND BT 
it's OK ? 

With Arduino Software : Settings for card must be in 3.3v or 5v ? 

I will trying every baudrate this evening... 

Thanks for all ;) 

Try a different version of the Arduino program - if that fails then it maybe that the BT module is broken.. 

Best of luck

Got up and running with walk through. Cheers!

HEY, i connect every tink and i dont get the at ok, can u help me? 

What troubleshooting have you already tried? 

i try to put rx to rx and tx to tx, i try in reverse and nothing, the bord is on but no signal from the pc 

What settings do you have in the Arduino serial monitor program?

Do I need to use an FTDI... or can I use a USBasp? .. or can I talk thru a raspberry pi.. or arduino board to change the BAUD? That is All I think I need to do???? 

https://www.youtube.com/watch?v=qLaH4qkJNt0 

You can get them from lots of places, search for 'FTDI Basic adapter'. You can use other boards but I don't. 

http://www.amazon.co.uk/gp/product/B00P46DTGM?psc=1&redirect=true&ref_=oh_aui_detailpage_o05_s00 

works 

I guess I could have used the serial port on my system and powered from another source... DUH But I got the FTDI it should be a useful tool anyway

What if you don't get any feedback when type AT and send 

What troubleshooting have you tried so far? 

It worked in the Arduino software with an ftdi and 9600 8 n 1 no cr/ln. I tried another ttl usb adapter  and that didnt work on windows and mac.  

On MAC you can get a tool called Serial Tools that works too. There is also Putty on 

Have you tried all of the baud rates? 

Who me? I got it working.  

i got one but it won't respond any thing  

What are your settings? Baud etc What exactly did you get? Model? What OS? What kind of serial software?

Great vid worked like a charm.
just worked for me thanks great informative clip

I have the HobbyKing ATMega2650 board and I have been struggling to connect the bluetooth module for days now. 

You say the default baud rate for MultiWii is 115,200.  But, I have MegapirateNG (3.1.5-R2) loaded on this board...   

Have you ever set up a BT board with Megapirate?  Do you know what the default baud rate is for MP?  I cannot even get my computer to handshake with the BT board.  I was considering trying multiple baud rates, but that could take a while--I was hoping you might have the answer! 

Thank you for your videos.  They are a ton of help for those of us who are getting into this great hobby but have little experience. 

No worries, I got it to work!  The key change was me powering the bluetooth off of the BEC instead of the main board.  I may have had too many peripherals plugged into the board (GPS, Rx, etc).  I got Mission Planner up and running over bluetooth.

HI! I'm trying to configure my BT module by AT COMMANDS whit Arduino. I dont have a FTDI board as we seen in the video, but I have a USB to UART board (http://www.ebay.es/itm/130683943875?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649&clk_rvr_id=683556886088). I connect my BT module with this board (Tx to RX, RX to TX, Vcc to Vcc and Gnd to Gnd). Then I connect de USB to UART board to the PC and i try to send AT commands with Arduino but i have no response...:( Any idea? 

It only worked with ftdi for me. Another adapter didn't work.  

Yep, go FTDI. It's not worth the hassle..

Hi, Do you also have a video about how to set up and connect the MinimOSD to the Crius MultiWii V2.5? I am dealing around with it now for several days, but I have not got it working so far. Thank you very much for all your other videos! Very helpful for me, very good explained... 

Hi Thomas, my MinimOSD is on it's way and once I've got it sorted I'll post a video. Looks like there are a few steps to get it running and I'm not clear on any code changes needed on the main MWC to get it running. Watch this space. 

Yes, I will let you know or will post a video on how I did it. Thanks...  

Awesome! Did you have to change the code in the main board? Which connector on the Cirus board did it go into? Very interested in your method and results. Thanks for posting.. 

No it is not working yet. I have the video signal from my cam but no OSD overlay. I am playing around with this code https://code.google.com/p/rush-osd-development/ but i was not successful. Maybe you can give me a hint how to set it up and how to connect the MinimOSD to a MultiWii. When i connect the MinimOSD to the PC everything looks fine,  but i cant get an overlay. :(

Hi Thomas, my understanding is that the minimOSD is expecting to see the MAVLink data coming out of the telemetry port on the APM flight controller. 

From what I can read (haven't got mine yet) you only get the OSD display active when the board gets this input. I'm not sure if the 'basic' Cirus SE 2.0/2.5 board send out this MAVLink data to fire up the OSD.. I need to get mine to play with.. 

That's possible, looking forward to your experiences ;-)  

I've got it working - finally! Tricky little bugger but once you've got a few things figured out it works a treat. I'm uploading a video as I type that should cover what you need. Awesome OSD for less than £10!Happy flying! 

I just saw and followed your step by step guide... Great thank you!!

Thank you very much for the video, it helped me configured the module. I still have issue with Windows 7 connection. The OS discovers the BT device, installed the drivers of the uart but under “Device control” it indicates “Unknown Device Operation” and doesn't let to connect to it. Any idea? 

Thank you so much for this. I ended up getting luck with mine. I used the Ublox software and it eventually recognized it and I was able to get it working. I didn't know you could use the Arduino IDE.