아두이노 > Arduino with HC-05 (ZS-040) Bluetooth module – AT MODE

TODAY833 TOTAL605,462
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 아두이노 | 아두이노 | Arduino with HC-05 (ZS-040) Bluetooth module – AT MODE

페이지 정보

작성자 키트 작성일2017-09-13 15:37 조회2,359회 댓글0건

본문

 

Arduino with HC-05 (ZS-040) Bluetooth module – AT MODE

Updated 19.07.2015
Updated 26.07.2015
Updated 30.10.2015

AT mode allows you to interrogate the BT module and to change some of the settings; things like the name, the baud rate, whether or not it operates in slave mode or master mode. When used as a master device AT commands allow you to connect to other Bluetooth slave devices.

There are many slightly different HC-05 modules, the modules I have are marked ZS-040 and have an EN pin rather than a KEY pin. They also have a small button switch just above the EN pin. They are based on the EGBT-045MS Bluetooth module.

Update: I now also have boards marked fc-114. See:
HC-05 FC-114 and HC-06 FC-114. First Look
HC-05 FC-114 and HC-06 FC-114. Part 2 – Basic AT commands
HC-05 FC-114 and HC-06 FC-114. Part 3 – Master Mode and Auto Connect

On the zs-040 modules there are 2 AT modes. I do not know if this is intentional but some commands only work when pin34 is HIGH. Other commands work when pin 34 is either HIGH or LOW. This fooled me for quite a while. For this post I have called the different modes “mini” AT mode and “full” AT mode.

HC-05 zs-040

To activate AT mode on the HC-05 zs-040 modules we can:
– 1. Hold the small button switch closed while powering on the module.
– 2. Set pin 34 HIGH (3.3v) when power on.
– 3. Close the small push button switch after the HC-05 is powered.
– 4. Pull pin 34 HIGH after powering the HC-05.

Method 1.
Enters AT mode with the built in AT mode baud rate of 38400. The baud rate cannot be changed by the user.
This method allows the module to enter AT mode on start but but does not keep pin 34 HIGH and uses the “mini” AT mode.

Method 2.
Enters AT mode with the built in AT mode baud rate of 38400. The baud rate cannot be changed by the user.
If you keep pin 34 HIGH you will enable the “full” AT mode which allows all AT commands to be used.
If you let pin 34 return LOW after power on then “mini” AT mode will be enabled.

Method 3.*
Enters “mini” AT mode using the user defined communication mode baud rate.

Method 4.*
Enters “full” AT mode using the user defined communication mode baud rate.

If pin 34 is kept HIGH then the HC-05 enters the “full” AT mode. If pin 34 is brought HIGH and returned to LOW it will put the module in to “mini” AT mode.

* added 21.07.2015

Method 1 and 2 are good in that you know the baud rate – it will always be 38400. This could be useful if you have modules other people have used or if you forget what communication mode baud rate you have previously set.

Method 3 and 4 adds convenience. You can enter AT mode, make changes and return back to communication mode without switching sketches and messing around with different baud rates.

I use software serial on Arduino pins 2 and 3 to talk to the HC-05. This means I can still use the hardware serial to talk to the serial monitor on a host computer.

 

Entering AT Mode Method 1. Use the button switch

The small push button switch, when closed, connects pin 34 to vcc which allows you to enter AT mode. Close the button switch when powering on the module and you enter AT mode using 38400 baud rate. Once on you can release the button switch, however, releasing the button switch puts the module in to a mini AT mode and some commands will not work. Commands such as AT+NAME?, AT+INQ, AT+RNAME? only work when pin 34 is HIGH. As soon as you release the button switch pin 34 returns LOW. If you want access to the extended commands you can simply close the switch just before issuing the AT command and release the button switch after the command has been sent.

Make the following connections
– BT VCC to Arduiono 5V
– BT GND to Arduino GND
– BT TX to Arduino D2
– BT RX to Arduino D3 through a voltage divider (3.3V)

HC-05 AT Mode Connections to Arduino

Connect the Arduino to the host computer. The LED on the HC-05 should be blinking quickly at about 5 times a second.

With the Arduino on, do the following
– Remove the 5V connection to BT VCC
– Press and hold the button switch on the BT module
– Re-connect BT VCC to 5V (while still pressing the button switch), the LED should come on.
– Release the button switch and the LED should be blinking slowly on/off once every couple of seconds. This indicates AT mode.

The following sketch is used to talk to the BT module. Run the sketch and put the HC-05 in to AT mode.

// Basic Bluetooth sketch HC-05_AT_MODE_01// Communicate with a HC-05 using the serial monitor//// The HC-05 defaults to communication mode when first powered on you will need to manually enter AT mode// The default baud rate for AT mode is 38400// See www.martyncurrey.com for details//  #include SoftwareSerial BTserial(2, 3); // RX | TX// Connect the HC-05 TX to Arduino pin 2 RX. // Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.//  char c = ' '; void setup() {    Serial.begin(9600);    Serial.println("Arduino is ready");    Serial.println("Remember to select Both NL & CR in the serial monitor");     // HC-05 default serial speed for AT mode is 38400    BTserial.begin(38400);  } void loop(){     // Keep reading from HC-05 and send to Arduino Serial Monitor    if (BTserial.available())    {          c = BTserial.read();        Serial.write(c);    }     // Keep reading from Arduino Serial Monitor and send to HC-05    if (Serial.available())    {        c =  Serial.read();        BTserial.write(c);      } }

Here is the output on the serial monitor
HC-05 AT Mode_ SerialMonitor_01

The HC-05 expects a new line and a carriage return character at the end of each command so make sure “Both NL & CR” is selected at the bottom of the serial monitor. To confirm you are actually in AT mode, in the serial monitor type “AT” (no quotes) and hit Send. You should get an “OK”.
HC-05 AT Mode_ SerialMonitor_04

 
 

Entering AT Mode Method 2. Using the Arduino to Control the HC-05

In this example the Arduino fully controls the HC-05. The Arduino pin D4 connects to a PNP transistor which is used as a switch to control the power and D5 is connected to the HC-05 pin 34 to control AT mode. Of course you can control the HC-05 manually if you wish.

This used to be my preferred method but I have found that once I have set up a module I seldom change it and it has become more convenient to use the button switch or a temporary connection to pin 34. The benefit of this example is that the Arduino can control the process. Also, certain commands only work when pin 34 is HIGH. Using this method allows you to keep pin 34 HIGH.

The HC-05 can draw a maximum of 40mA and although Arduino pins can supply 40mA this is the maximum and not recommended. The Arduinos 5V out pin can supply up to 200mA and is a much safer option and we use this to power the HC-05. We still need a regular pin to control the transistor which we are using to switch the power on and off.

With the Arduino turned off make the following connections:
– Arduino D2 to HC-05 TX
– Arduino D3 to HC-05 RX through a voltage divider
– Arduino D4 to HC-05 pin 34 through a voltage divider
– Arduino D5 to PNP transistor base via a 2.2k resistor
– HC-05 GND to common GND
– PNP emitter to +5V
– PNP collector to HC-05 vcc

I am using a 2N3906 PNP transistor because it is what I have. Similar PNP transistors can also be used.

HC-05_02_Breadbaord_1600

HC-05-PNP

HC-05_AT_MODE_02_BreadBoard_ClipCloseup_01_1200
HC-05 with pin 34 connected by a probe clip. You can also simply hold a piece of wire to the pin. For a long term solution you would need to solder a wire to pin 34.

Compile and upload the following sketch

// Basic Bluetooth sketch HC-05_AT_MODE_02b// Connect the HC-05 module and communicate using the serial monitor// Arduino automates entering AT mode//// The default baud rate for AT mode when pin 34 is HIGH on power on is 38400// See www.martyncurrey.com for details//////  Pins//  Arduino D2 to HC-05 TX//  Arduino D3 to HC-05 RX via a voltage divider//  Arduino D4 to HX-05 pin 34 via a voltage divider//  Arduino D5 to PNP Base via a 2.2k resistor//  BT VCC to PNP Collector//  BT GND to GND//  PNP Emitter to vcc//// When a command is entered in to the serial monitor on the computer // the Arduino will relay it to the Bluetooth module and display the result.// const byte BT_POWERPIN = 5;const byte BT_PIN34_PIN = 4; const boolean ON = LOW;const boolean OFF = HIGH; boolean BT_POWER = HIGH;  boolean PIN34_STATE = LOW; boolean NL = true;char c = ' ';  #include SoftwareSerial BTserial(2, 3); // RX | TX void setup() {    pinMode(BT_POWERPIN, OUTPUT);    pinMode(BT_PIN34_PIN, OUTPUT);      digitalWrite(BT_POWERPIN, OFF);      digitalWrite(BT_PIN34_PIN, LOW);      Serial.begin(9600);     // communication with the host computer    //while (!Serial)   { ; }     Serial.println("Arduino Started");    Serial.

댓글 0

조회수 2,359

등록된 댓글이 없습니다.

아두이노HOME > 아두이노 > 전체 목록

게시물 검색

2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2017
모바일버전으로보기