You are on page 1of 19

6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

MartynCurrey
MostlyArduinostuff

ArduinoandHC06(ZS040)
PostedonOctober8,2014

TheHC06isaslaveonlyBTmodulethatisfairlyeasytousewiththeArduinousingserial
communication.OnceitisconnecteditsimplyrelayswhatitreceivesbybluetoothtotheArduinoand
whateveritreceivesfromtheArduinoitsendstotheconnecteddevice.Thereareseveralslightly
differentversionsoftheHC06,however,allseemtousethesamefirmwareandhavethesameAT
commands.TheonesIhavearelabelledaszs040.IalsohavesomeHC05swhichsharethesame
PCBandarealsolabelledaszs040.

http://www.martyncurrey.com/arduinoandhc06zs040/ 1/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

TheHC06defaultstoATmodeatpoweron.ThisisindicatedbyarapidlyflashingLED.AftertheHC
06isconnectedtoanotherdevicetheLEDstopsflashingandisconstanton.

Connections
TheBluetoothmoduletheZS040isbasedon,theEGBT046S,isa3.3Vdevice.TheHC06breakout
boardhasa3.3vregulatorthatallowsalargerinputvoltagetobeused,intherangeof3.6to6volts.
TheRXpincanstillonlyaccept3.3Vthough.Thismeansavoltagedividerisrequiredtoconnecttoa
5VArduino.Asimplevoltagedividercanbecreatedusing2resistors.Iamusinga1Kohmresistor
anda2Kohmresistor.TheArduinowillread3.3VasaHIGHsotheHC06TXpincanbeconnected

http://www.martyncurrey.com/arduinoandhc06zs040/ 2/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

directlytotheArduino.

HC06Vinto5V(canbefromthe+5VoutfromtheArduino)
HC06GNDtocommonGND
HC06RXtoArduinopinD3(TX)viaavoltagedivider
HC06TXtoArduinopinD2(RX)connectdirectly

TestCommunicationWithTheHC06
AfterconnectingeverythingweneedtotalktotheHC06.Wecandothisbyusingsoftwareserialon
theArduino.IusesoftwareserialtotalktoBluetoothmodulesandusethehardwareserialfor
debugging.

Thefollowingsketchtakeswhateverisenteredintotheserialmonitoronahostcomputerandrelays
ittotheHC06.ThesketchalsotakeswhatevertheHC06outputsandforwardsittotheserial
monitor.TheArduinoisactinglikearelaystationbetweentheserialmonitorandtheBTmodule.

http://www.martyncurrey.com/arduinoandhc06zs040/ 3/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

TheHC06sIhavehaveadefaultbaudrateof9600.Othermoduleshaveadifferentbaudrate.If9600
doesntworktryotherspeeds.38400isalsoverycommon.Onceyouhavecommunicationworking
youcanchangethebaudratetosuityourneeds.

//BasicBluetoothsketchHC06_01
//ConnecttheHc06moduleandcommunicateusingtheserialmonitor
//
//TheHC06defaultstoATmodewhenfirstpoweredon.
//Thedefaultbaudrateis9600
//TheHc06requiresallATcommandstobeinuppercase.NL+CRshouldnotbeaddedtothecommandstring
//


#include<SoftwareSerial.h>
SoftwareSerialBTserial(2,3);//RX|TX
//ConnecttheHC06TXtotheArduinoRXonpin2.
//ConnecttheHC06RXtotheArduinoTXonpin3throughavoltagedivider.
//


voidsetup()
{
Serial.begin(9600);
Serial.println("EnterATcommands:");

//HC06defaultserialspeedis9600
BTserial.begin(9600);
}

voidloop()
{

//KeepreadingfromHC06andsendtoArduinoSerialMonitor
if(BTserial.available())
{
Serial.write(BTserial.read());
}

//KeepreadingfromArduinoSerialMonitorandsendtoHC06
if(Serial.available())
{
BTserial.write(Serial.read());
}

}

TheHC06zs040expectscommandstobeinuppercaseanddoesnotrequirecarriagereturnand
newline(\r\n)characters.
Opentheserialmonitorandselectabaudrateof9600andensureNolineendingisselectedfrom
thedropdownlistatthebottomofthewindow.
EnterAT(noquotes)intothetoptextboxandhitSend.IftheHC06likesyouitwillsayOK.ATisa
basiccommunicationstestcommandthatallowsyoutochecktheHC06isconnectedand

http://www.martyncurrey.com/arduinoandhc06zs040/ 4/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

communicating.

ATCommands
TheHC06hasalimitednumberofcommands.Youcanrenamethedevice,changethebaudrate,and
changethePIN/password.Thatsaboutit.

Command Reply Comment

AT OK Communicationstest

AT+VERSION OKlinvorV1.8 Firmwareversion.

AT+NAMEmyBTmodule OKsetname SetsthemodulesnametomyBTmodule

AT+PIN6789 OKsetPIN SetthePINto6789

AT+BAUD1 OK1200 Setsthebaudrateto1200

AT+BAUD2 OK2400 Setsthebaudrateto2400

AT+BAUD3 OK4800 Setsthebaudrateto4800

AT+BAUD4 OK9600 Setsthebaudrateto9600

AT+BAUD5 OK19200 Setsthebaudrateto19200

AT+BAUD6 OK38400 Setsthebaudrateto38400

AT+BAUD7 OK57600 Setsthebaudrateto57600

AT+BAUD8 OK115200 Setsthebaudrateto115200

AT+BAUD9 OK230400 Setsthebaudrateto230400

AT+BAUDA OK460800 Setsthebaudrateto460800

AT+BAUDB OK921600 Setsthebaudrateto921600

AT+BAUDC OK1382400 Setsthebaudrateto1382400

http://www.martyncurrey.com/arduinoandhc06zs040/ 5/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

TogetthemodulesfirmwareversionenterAT+VERSION(noquotes):


ThemodulesIhavereporttheyareusingversionlinvorV1.8whichseemstobecommonformany
HC06s.

Note:Windowscannotusebaudratesabove115200.IfyouareusingWindowsandyouaccidentally
setthebaudratehigherthan115200yourescrewed!

AfterconfirmingthattheHC06isworkingandcommunicatingwecantrytoconnecttoanAndroid
device.

HC06ConnectingtoanAndroidDevice
SincetheHC06isaslaveonlydevice,theconnectionmustbestartedbyanotherdevice.Beloware
thestepstopairandthenconnectwithanAndroiddevice.
IamusinganappcalledBluetoothTerminalwhichisavailableforfreeongoogleplay.

BeforeyoucanconnecttotheHC06youneedtopairit.
PowerontheHC06.TheLEDwillflashrapidly.
OpenSettingsontheAndroiddeviceandselectBluetooth.
Ifyourdevicedoesnotautoscanyouwillneedtomanuallyscanforavailablebluetoothdevices.The
HC06shouldappearinthelist.
SelecttheHC06.Youwillbeaskedforthepin.Thedefaultpinis1234.
Themodulesnamemayincludethemacaddressaseriesofhexadecimalnumbers.

http://www.martyncurrey.com/arduinoandhc06zs040/ 6/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

AftertheHC06ispairedyouneedtocommunicatewithitsomehow.Totestthingsareworkingyou
canuseaBTterminalprogramsuchasBluetoothTerminalavailableonGooglePlay

InstallandopenBluetoothTerminal.
Openthemenu,iconatthetopofthescreen.
SelectConnectadeviceInsecure.Thisbringsupalistofavailabledevices.
SelecttheHC06
Onceconnected,connected:HC06isdisplayedatthetopofthescreen.

MakesuretheArduinoserialmonitorisopenandeverythingyouenterintotheAndroidBluetooth
Terminalwillbeechoedintheserialmonitor.

http://www.martyncurrey.com/arduinoandhc06zs040/ 7/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

IntheArduinoserialmonitor,selectBothNL&CRatthebottomofthewindowandwhateveryou
typeintheserialmonitorwillbesenttotheAndroidBluetoothTerminal.

IfNL&CRarenotselected,theArduinowillstillsendthedatabuttheBluetoothTerminalprogramwill
notdisplayituntilitreceivesacarriagereturn/newline.

Nextstep.TurninganLEDonandoff.

TurningaLEDOnandOffbyBluetooth
IhaveupdatedandmovedtheTurninganLEDonandoffguidetoitsownpost

Update
IhavenewHC06zs040modulesthathavethehc01.comV2.0firmware.Seehere.

http://www.martyncurrey.com/arduinoandhc06zs040/ 8/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

ThisentrywaspostedinArduino,BluetoothbyMartyn.Bookmarkthepermalink
[http://www.martyncurrey.com/arduinoandhc06zs040/].

36THOUGHTSONARDUINOANDHC06(ZS040)

StevePotter
onApril13,2015at7:16pmsaid:

Thisdownloadzipseemstobemissingthe.aiafile.Ionlyseethe.inoandcompiled
.apk.
Canyoupleasepostthe.aiafilesowehavetheblockstoworkwith?
Thanks!

Martyn
onApril14,2015at12:50pmsaid:

HiSteve,

haveupdatedthedownload.Nowincludestheappinventoraiafileandalso
thecorrectArduinosketch.

rabchit
onMay9,2016at7:20pmsaid:

whereisthedownloadlink?

JamesHeires
onApril14,2015at10:59pmsaid:

Martyn,
Thanksforposting.
SmallissuetheArduinosketchmakesuseofArduinopins2&3forRx&Tx,
respectivelyusingSoftwareSerial,butyourdescriptionstatesHC06RXtoArduino

http://www.martyncurrey.com/arduinoandhc06zs040/ 9/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

TXviaavoltagedivider,andHC06TXtoArduinoRX(connectdirectly).So,Itried
bothways,butsuspectthatpins2&3arecorrect?
Inanyevent,ImunabletogetanyATcommandresponsesfrommyHC06module
allIseeintheserialmonitorwindowistheprompt:EnterATcommands:
Triedwithmultiplebaudratesandlineendings,tonoavail.
IknowthisBTmoduleisworking,becauseItestedwiththeechosketchandanLED
controllersketch(foundelsewhere)withoutissue.Bothoftheseexamplesusedthe
hardwareTx/Rxpins(0&1onmyArduinoUno).
Anythoughts?

Martyn
onApril15,2015at7:35amsaid:

HC06RXtoArduinoTXviaavoltagedivider,andHC06TXtoArduinoRX
(connectdirectly)refersthesoftwareserialonpins2and3,however,other
pinscanbeusedsuchas10and11.

IfyouarenotusingtheserialmonitoronthehostcomputerYoucanconnect
theBTmoduletothehardwareserialbutIusethisforcommunicationwiththe
serialmonitoronthehostcomputerfordebugging.

Checkyourconnections:Arduinopin2toHC06TXandArduinopin3toHC
06RX(withavoltagedivider)
RememberthattheHC06expectscommandstobeuppercaseanddonot
addCR+NLwhentypingATcommands.

WhensendingcommandsfromtheserialmonitorcheckthattheLED
connectedtothehardwareserialisflashing.

YoucanalsoaddanLEDtoshowwhendataisbeingsentthroughthe
softwareserial

Pingback:TurningaLEDonandoffwithanArduino,aHC05andAndroid|Martyn
Currey

Pingback:Connecting2ArduinosbyBluetoothusingaHC05andaHC06:Pair,
Bind,andLink|MartynCurrey

Pingback:QuickLinks|MartynCurrey

http://www.martyncurrey.com/arduinoandhc06zs040/ 10/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

Tom
onJanuary25,2016at7:23pmsaid:

Thisisagreattutorial,butImunabletosendATcommandsfromtheserialterminal.
WhenIattempttoconnectfromTeraTerm,thesameresults.Thehc06ledis
flashingandnotsteady.HowevertheAndroidconnectstotheHC06,asindicatedby
steadyledandtheabilitytocontrolledsandmotors.Anyhelpisappreciated.

Martyn
onJanuary26,2016at1:21pmsaid:

Tom,
doyouhavecommunicationbetweentheArduinoandtheHC06withAt
commandsworking?
Areyouadding\r\ntotheendofthetext?
SomeonereportedtheyhadissueswiththelatestIDE.Whentheytriedan
earlierversioneverythingworkedfine.

Ifyoucannotgetitworkingafterdoublecheckingeverythingtryadifferent
terminalapp.

AreyouusingtheexactsameBTmodule?Therearenownewerversions(JY
MCUBT_BOARDV1.06)thatrequireapullupresistorontheArduinoRXline.
IfyouthinkyoumayhavetheJYMCUV1.06boardsee
http://mcuoneclipse.com/2014/03/30/gettingbluetoothworkingwithjymcu
bt_boardv106/fordetails.

Pingback:ArduinoProjects:JYMCU/HC06|Engineertravellingtheworld.

cosmos
onApril21,2016at2:29amsaid:

Thisisverygoodtutorial.
thankyou.

http://www.martyncurrey.com/arduinoandhc06zs040/ 11/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

alexis
onApril23,2016at7:13amsaid:

Verygoodtutorial.

Mike
onApril26,2016at3:44amsaid:

HeyMartyn,
Thispostwasveryhelpful,Iaskedyouaboutconnectingtwohc06sandinstructed
metouseahc05andanhc06.withalittlebitofworkiwasabletofigureoutfrom
yourposthowtogetthehc05toreceiveATcommandsthroughaserialportonmy
pc.Iamunabletoachievethiswiththehc06,Ihavedoublecheckedmywiringandit
isallinitsproperplaceandevenattemptedusingsomeofthesketchesyouhave
postedonyourblogtogetitintoacommandmodetoreceivetheATcommands,
someblogsmentionofputtingahightokey26idobelieveonthehc=06togetitinto
commandmode,andthatthecommandshavetobeenteredwithinatimelimitorthe
deviceignoresit.anyhelpwiththiswillbegreatlyappreciated.

thanksagain

Martyn
onApril26,2016at7:26amsaid:

UnliketheHC05whichyouneedtomanuallyenter,theHC06defaultstoAT
mode.TheHC06isinATmodewhenfirststarted.

AcoupleifthingstonotetheHC06requiresATcommandstobeuppercase
anddonotaddcarriagereturnandnewlinecharacters(\r\n).

Rereadtheaboveguideandifyouhavefollowedtheaboveandarestill
havingproblemsyouneedtocheckthebaudrate.MostHC06saresetto
9600butsomediffer.Firsttry38400andthenalltheothers.

SandeepRaj
onDecember3,2016at11:28pmsaid:

http://www.martyncurrey.com/arduinoandhc06zs040/ 12/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

TheATmodeworkedat9600baud.Ichangedthebaudto1200and
nowIamunabletoreturnbackto9600baudusingATcommands.

ThebluetoothmoduleonlyrespondstothecommandATbutnotto
anyothercommandslikeAT+NAMEorAT+VERSIONoranyother
forthatmatter.Thisbeingthecase,themoduleisntrespondingto
AT+BAUD4tochangeitbacktoitsdefaultbaudrate.

SandeepRaj
onDecember4,2016at12:19amsaid:

Gotitbackto9600baudbydoingahardresetofthemodule.I
didthisbypullingthePin11(RESETpin)tothegroundand
powercyclingit.ThenIwenttoATmodeandgavethe
commandAT+BAUD4.Itworked.

Aryaman
onMay4,2016at3:08amsaid:

heyMartyn
iveBluetoothmodulenamedZS040andithas6pinsinsteadof4.2extraforstate
andoneforen
Itriedconnectingonly4ofthembutmymoduleisntlightingup
whereshouldIconnectthosetwopinsinmyarduinouno

pleasehelp!!
Regards:)

Martyn
onMay4,2016at4:24pmsaid:

The6pinversionistheHC05.See

http://www.martyncurrey.com/arduinowithhc05bluetoothmoduleinslave
mode/

http://www.martyncurrey.com/arduinoandhc06zs040/ 13/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

http://www.martyncurrey.com/arduinowithhc05bluetoothmoduleatmode/

PsyMan
onJune7,2016at7:56pmsaid:

Thankyou,afteracoupleofhoursheadscratchingIhavefinallychangedthesettings
onmyHC06withouthavingtowaitforanFTDIAdapter,Isimplywired4pins
directlytothearduinounoastheBTadaptersays3.36vanditsallgoodtofittothe
Naze32now.Goodworkandthanksagain.

Lucas
onJune10,2016at5:05pmsaid:

HelloMartyn,oneofthefewplaceswheretofindinfoonHC05
CanyouconfirmthatwithoutthevoltagedividerontheINline(forsignalsat5V),the
HC05doesntreadthesignal?
Thanks

Martyn
onJune11,2016at1:50amsaid:

TheHC05willworkwithoutthevoltagedividerandmanypeopleconnect
directlytoa5Vsignalwithoutaproblem.However,theBluetoothmodule(the
smallmodulenottheHC05breakout)isa3.3vdeviceandotherpeoplehave
reporteddamagingtheboardswhenusedwith5V.Thedamagemaynot
happenstraightawayandsoitmaynotbeobvious.Forthesakeof2cheap
resistorswhytakethechance.

http://www.martyncurrey.com/arduinoandhc06zs040/ 14/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

NECROSUMBRA
onSeptember7,2016at1:44amsaid:

Ihave4oftheZS040adapters.IamtryingtolearnthecodebuteverytimeItryto
connecttothebluetoothitjustsaysconnectionrejected.WhenIputintheAT
commandsIgetnothingback.
AnyIdeas?

Imlookingtodosomemastertomultipleslavestomultipuleslaves.Iknowtheycan
nottalkallatoncewhichisfinebutIreallyneedtofigurethisout.anysiteIcanlook
atforadvancedbluetoothcode?

Martyn
onSeptember7,2016at2:56pmsaid:

Therearenowafewdifferentmodulesthatusethezs040breakoutboardand
youneedtodeterminewhichonesyouhave.

IftheyarevisibleinAndroidSettings=>scanfordeviceswhatisthename
theyshow?

ArethetheyBLEdevices?IfyestheymaynotpairinSettingsandyouwill
needaBTv4compatibleapptoseethemproperly.

DotheyhaveablueLEDattopleftonthesmalldaughterboard?Ifyessee
thebottomofthehttp://www.martyncurrey.com/bluetoothmodules/post.

IfyouaresuretheyareBluetoothV2.0/2.1thendoublechecktheconnections
totheArduino.Makesureyouhavethentherightwayaround(RXtoTXand
TXDtoRX)andthatyouhavethecorrectvalueresistors.

Idonthaveanexampletoshowhowtosetupanetworkbutbasicallyyou
needtoconnecttoeachslavedeviceoneatatime.
Connecttomodule1,getdata,closeconnection.
Connecttomodule2,getdata,closeconnection.
Connecttomodule3,getdata,closeconnection,etc.

Ihavea4nodenetwork(1master,3slaves)runningathomecontrollinga
turtleaquarium.TheconnectingandreconnectingisnotasquickasIwould
likebutIdontreallyneedaninstantconnectionsoIconnecttoeachnode
every10secondsorso.IusetheSTATEpinontheHC05toshowIhavea
connectionandthenIuseaverybasichandshakesystem(aHELLOits
module1/2/3heremessage)toshowwhichnodeisconnected.

http://www.martyncurrey.com/arduinoandhc06zs040/ 15/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

Idontconnecttoeachmoduleinturn,Iconnecttothewaterheaterevery
secondconnection.Thisallowstomecheckthewatertempmorefrequently
justincaseIamtryingtoboiltheturtle.

CharlieT
onSeptember12,2016at6:04pmsaid:

IhavemanagedtoaltertheserialparityonmyHC06andnowcantuseitduetothe
factthatArduinosoftwareserialwontletmechangetheparity.

Myquestionissimple.HowtoIrestorethefactorydefaultstomyHC06?
SomespecialpowerupsequenceIhope!:)

Thanksinadvance,

CT

Martyn
onSeptember13,2016at3:05amsaid:

Youcannotrestorefactorydefaultsthroughhardwareonlysoftware.

Youwillneedausbserialadaptorandafullterminalprogramlikeputtywhichallow
youtochangetheserialcomsproperties.

CharlieT
onSeptember14,2016at8:07pmsaid:

OKgotit.Iknowwhattheparitywaschangedto.Ididmanagetofigurethat
out.Iwilluseaterminalprogramandgetitbackto9600,N,8,1.

Thanks!

http://www.martyncurrey.com/arduinoandhc06zs040/ 16/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

Mike
onOctober6,2016at5:12amsaid:

Hi,Martyn
IfIdontuseSoftwareSerial,IcantcommandtheBT,right?
Serial.read&writeonlyfortalkbetweenserialmonitor&arduino?

Mike
onOctober6,2016at6:15amsaid:

UnlessusetheMEGA.

Martyn
onOctober6,2016at8:39amsaid:

Inalltheexamplesiusehardwareserialtocommunicatewithahost
computerandtheArduinoserialmonitor.

OnATmega328basedArduinosthereisonlyonehardwareserialsoI
uesoftwareSerialorAltSoftSerialtotalktotheBluetoothmodule.

Ifyoudonotneedtousetheserialmonitorthenyoucanusthe
hardwareserialtotalktotheBluetoothmodule.

Ifyouneedhardwareserialforboththen,yes,youneedanArduino
suchastheMegawhichhasmorethanonehardwareserial.

WhenIdevelopnewprojectsformyselfIusehardwareserialtotalkto
theBTmoduleandausbserialadapterandsoftwareserialtotalkto
theserialmonitor.WheneverythingisworkingIremovetheserial
monitorpartsfromthecode.

http://www.martyncurrey.com/arduinoandhc06zs040/ 17/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

PhilippeLoutrel
onNovember26,2016at11:04pmsaid:

Thankssomuch,yourcodereallyworks!
Thereissomuchwronginformationaround,likeconnectpin26toVccforATmode
onHC06!!!

Ted
onMarch3,2017at8:45pmsaid:

HiMartyn

Thanksfortheinsightfulpost.Outofcuriosity,Imeasuredthevoltagearrivingatthe
RXpinoftheEGBT046S(withoutusingavoltagedivider).Itmeasured3.28vsoit
seemsasifthebreakoutboardisdoingsomevoltageregulation.

Doesthisseemplausible?

Warmregards,
Ted

Ted
onMarch3,2017at8:56pmsaid:

Woops!ItturnsoutIwasmeasuringthewrongpin.TheRXpinoftheEGBT
046Sisindeed~4.7vsoavoltagedividerismostdefinitelyneeded.

Ted

krittibasbairagi
onApril16,2017at4:48amsaid:

sirihavehc06bluetoothmodule.afterpowerupledisblinkingwithinterval2sec
approx.buticantfindbluetoothsignalfrommymobile.italsodoesnotacceptanyat

http://www.martyncurrey.com/arduinoandhc06zs040/ 18/19
6/20/2017 ArduinoandHC06(ZS040)|MartynCurrey

command.helpme

Richard
onMay21,2017at3:53amsaid:

HiMartyn

IdontwanttoconnectittoArduino,butratherastandardRS232device.
Inadvanceofconnectingthingsup,doyouknowwiththeZS040(HC06)boardwill
itdrivetheRxportofastandardRS232deviceOK?
IdoplantousetheresisitivesplitontheRxportoftheZS040moduleofcourse.

RegardsandThanks

Martyn
onMay21,2017at6:38amsaid:

ThesignalsshouldbeOKbutifyoutry,becarefulwiththevoltages.RS232
signalvoltagescanbeanythingupto25v.Checkthevoltagesyourdevice
uses.

http://www.martyncurrey.com/arduinoandhc06zs040/ 19/19