การเชื่อมต่อโมดูล Bluetooth HC-05 กับ Arduino Nano


 HC-05 เป็นโมดูล Bluetooth ที่ใช้ในการสื่อสารระยะสั้นในโครงการต่าง ๆ ของอิเล็กทรอนิกส์ 

โดยทั่วไปจะใช้ในระบบ IoT (Internet of Things) หรือโปรเจคที่ต้องการการสื่อสารแบบไร้สาย

เช่น การควบคุมอุปกรณ์จากระยะไกล หรือ การส่งข้อมูลระหว่างอุปกรณ์ต่าง ๆ 

โมดูลนี้สามารถทำงานในโหมด Master และ Slave ทำให้สามารถเชื่อมต่อกับอุปกรณ์อื่น ๆ 


HC-05 Datasheets:

http://kio4.com/arduino/imagenes/banggood-hc-05-manual-optimized.pdf


คุณลักษณะของ HC-05

โปรโตคอล: Bluetooth Classic (RFCOMM)

ความถี่: 2.4 GHz ระยะการสื่อสาร: ประมาณ 10 เมตร (ขึ้นอยู่กับสภาพแวดล้อม)

การควบคุม: สามารถใช้งานได้โดยการสื่อสารผ่าน Serial (UART)

การกำหนดค่า: สามารถตั้งค่าผ่าน AT Commands

https://s3-sa-east-1.amazonaws.com/robocore-lojavirtual/709/HC-05_ATCommandSet.pdf


การใช้งาน 

เชื่อมต่อกับไมโครคอนโทรลเลอร์ (เช่น Arduino) เพื่อรับส่งข้อมูล

ใช้ควบคุมอุปกรณ์ไฟฟ้าผ่านสมาร์ทโฟนหรือแท็บเล็ต สามารถใช้ในโครงการหุ่นยนต์เพื่อควบคุมการเคลื่อนที่จากระยะไกล

HC-05 เป็นโมดูลที่ได้รับความนิยมมากในหมู่นักพัฒนา ใช้งานง่าย และ เป็นทางเลือกที่คุ้มค่าสำหรับการพัฒนาระบบไร้สาย.

การเชื่อมต่อโมดูล Bluetooth HC-05 กับ Arduino Nano 

โมดูล Bluetooth HC-05 สามารถเชื่อมต่อกับบอร์ด Arduino Nano 

ได้โดยใช้พอร์ต Serial หรือ SoftwareSerial 

โดยการเชื่อมต่อที่สำคัญคือการเชื่อมระหว่างพินของโมดูลกับพินของ Arduino 


อุปกรณ์ที่ต้องใช้:

Arduino Nano

โมดูล Bluetooth HC-05

สาย Jumper

แหล่งจ่ายไฟ (ถ้าไม่ใช้จาก Arduino)


การเชื่อมต่อ:

เชื่อมต่อพินของ HC-05 เข้ากับ Arduino Nano:

VCC ของ HC-05 → 5V ของ Arduino Nano

GND ของ HC-05 → GND ของ Arduino Nano

TX ของ HC-05 → RX (D0) ของ Arduino (หรือต่อผ่าน SoftwareSerial ใช้พินอื่น)

RX ของ HC-05 → TX (D1) ของ Arduino (ใช้ตัวต้านทานแบ่งแรงดันถ้าจำเป็น 5V → 3.3V)

EN ของ HC-05 → 3.3V  ของ Arduino


Diagram Connection

HC-05         Arduino Nano  

-----------------------------  

VCC   ----->   5V  

GND   ----->   GND  

TX    ----->   RX (D0)  

RX    ----->   TX (D1) ← (ใส่ตัวต้านทานแบ่งแรงดันถ้าจำเป็น)  


การตั้งค่าโปรแกรมใน Arduino IDE:

เปิด Arduino IDE ขึ้นมา ใช้โค้ดตัวอย่างเพื่อทดสอบการเชื่อมต่อ:

AT Mode:

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

//  Sketc: basicSerialWithNL_001

// 

//  Uses hardware serial to talk to the host computer and software serial 

//  for communication with the Bluetooth module

//  Intended for Bluetooth devices that require line end characters "\r\n"

//

//  Pins

//  Arduino 5V out TO BT VCC

//  Arduino GND to BT GND

//  Arduino D9 to BT RX through a voltage divider

//  Arduino D8 BT TX (no need voltage divider)

//

//  When a command is entered in the serial monitor on the computer 

//  the Arduino will relay it to the bluetooth module and display the result.

//

 

 

#include <SoftwareSerial.h>

SoftwareSerial BTserial(8, 9); // RX | TX

// int = -32768 to 0,+32767 (2 ยกกำลัง 15 )

// 00000000 - 01111111 = 0->+32767

// 10000000 - 11111111 = -32768


const long baudRate = 38400; 

char c=' ';

boolean NL = true;

 

void setup() 

{

    Serial.begin(9600);

    Serial.print("Sketch:   ");   Serial.println(__FILE__);

    Serial.print("Uploaded: ");   Serial.println(__DATE__);

    Serial.println(" ");

 

    BTserial.begin(baudRate);  

    Serial.print("BTserial started at "); Serial.println(baudRate);

    Serial.println(" ");

}

 

void loop()

{

 

    // Read from the Bluetooth module and send to the Arduino Serial Monitor

    if (BTserial.available())

    {

        c = BTserial.read();

        Serial.write(c);

    }

 

 

    // Read from the Serial Monitor and send to the Bluetooth module

    if (Serial.available())

    {

        c = Serial.read();

        BTserial.write(c);   

 

        // Echo the user input to the main window. The ">" character indicates the user entered text.

        if (NL) { Serial.print(">");  NL = false; }

        Serial.write(c);

        if (c==10) { NL = true; }

    }

 

}




////////////////////////////////////////////////////////////////////////////////////////////////////

การเชื่อมต่อแบบ CMode:

https://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-easy-method-using-cmode/

// Basic Bluetooth sketch HC-05_AT_MODE_01

// Connect the HC-05 module and communicate using the serial monitor

//

// The HC-05 defaults to commincation 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.h>

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);  

    }

 

}

ขั้นตอนในการอัปโหลดโค้ด:

เชื่อมต่อ Arduino Nano กับคอมพิวเตอร์ผ่าน USB

เลือกบอร์ดและพอร์ตใน Arduino IDE

อัปโหลดโค้ดไปยัง Arduino

ทดสอบการเชื่อมต่อ:

ใช้แอป Bluetooth บนสมาร์ทโฟน (เช่น Bluetooth Terminal) เชื่อมต่อกับ HC-05

พยายามส่งข้อความระหว่างโทรศัพท์และ Arduino ซึ่งจะแสดงผลใน Serial Monitor ของ Arduino IDE

ด้วยขั้นตอนนี้ คุณจะสามารถเชื่อมต่อและสื่อสารกับโมดูล HC-05 ผ่าน Arduino Nano ได้อย่างรวดเร็วและง่ายดาย!

Contact:

https://www.facebook.com/chaiyutpong


ความคิดเห็น