HC-05蓝牙模块接Arduino:AT模式、透传与手机互发

2026-12-03 20:22:18 13阅读

想给Arduino加个无线通道,HC-05依然是最省事的选择:一个串口透传模块,配对之后手机、电脑都能跟板子互发数据。这篇讲清它的接线、AT指令模式和一对透传的实际用法。

模块本身:透明串口桥

HC-05的核心是Bluetooth 2.0 SPP协议桥,对它来说什么都不用管,串口收到的字节原样从蓝牙发出去,蓝牙收到的字节原样从TX脚吐出来。模块标注Power 3.6V-6V,板载稳压,但RX脚逻辑电平还是3.3V,接5V主控的TX时最好串一个1k和2k的分压,保守做法是一分压电阻落到3.3V区间。

HC-05蓝牙模块实物正反面

引脚六根:EN(有些印成KEY)、VCC、GND、TXD、RXD、STATE。EN脚决定进入AT模式,STATE脚在连接建立后拉高,可以拿来点个灯当连接指示。

接线与两种工作模式

接线只有四根:VCC接5V,GND共地,模块TXD接主控RX,模块RXD接主控TX。注意是交叉接,TX对RX。

Arduino与HC-05接线原理图

HC-05有两种模式。普通透传模式下波特率9600,EN脚悬空即可;按住EN脚上电(或EN拉高),模块进入AT指令模式,波特率是38400,这时串口发的都是配置命令而不是数据。改名字、改配对码、改波特率、指定主从模式,都在AT模式下完成。

AT指令速查

用USB转TTL或者Arduino的软串口都能进AT模式,几个最常用的命令如下。发AT回OK说明链路通了;AT+NAME改广播名;AT+PSWD改配对码,默认1234;AT+UART改串口波特率;AT+ROLE=0是从机等着被连,AT+ROLE=1是主机主动连对方。改完记得AT+RESET,否则部分参数不生效。

两个容易卡住的点:一是AT模式下必须用38400波特率,用9600发AT只会没反应;二是EN脚是拉高进AT,不是接地,接反了永远进不去。

透传例程:手机控LED

配对连接之后,HC-05对Arduino来说就是Serial。下面例程收一个字符控制7号脚LED,收'1'开灯、'0'关灯,并把状态回传给手机,代码搬运自HowToMechatronics教程:

#define ledPin 7
int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }

 if (state == '0') {
  digitalWrite(ledPin, LOW); // Turn LED OFF
  Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
  state = 0;
 }
 else if (state == '1') {
  digitalWrite(ledPin, HIGH);
  Serial.println("LED: ON");;
  state = 0;
 } 
}

注意这段例程用的是38400,因为源教程里模块进了AT模式后把波特率设成了38400。如果你的模块还是默认透传9600,把Serial.begin改成9600就行。

手机端装一个串口蓝牙终端App,扫描配对(配对码默认1234),选中HC-05建立连接,发送字符就能看到LED动作,同时收到模块回传的状态字符串。

手机蓝牙配对设置界面

蓝牙串口App控制界面

电脑端也能连:虚拟串口

带蓝牙的笔记本配对后会出现两个 outgoing 和 incoming 的COM口,用 outgoing 口(一般编号大的那个)以38400波特率打开,就能和Arduino互发数据。下面这段是Processing写的一个简易控制界面,两个按钮对应开关灯:

import processing.serial.*;
Serial myPort;
String ledStatus="LED: OFF";

void setup(){
  size(450, 500);
  myPort = new Serial(this, "COM5", 38400); // Starts the serial communication
  myPort.bufferUntil('\n'); // Defines up to which character the data from the serial port will be read. The character '\n' or 'New Line'
}
void serialEvent (Serial myPort){ // Checks for available data in the Serial Port
  ledStatus = myPort.readStringUntil('\n'); //Reads the data sent from the Arduino (the String "LED: OFF/ON) and it puts into the "ledStatus" variable
}

void draw(){
  background(237, 240, 241);
  fill(20, 160, 133); // Green Color
  stroke(33);
  strokeWeight(1);
  rect(50, 100, 150, 50, 10);  // Turn ON Button
  rect(250, 100, 150, 50, 10); // Turn OFF Button
  fill(255);
  
  textSize(32);
  text("Turn ON",60, 135);
  text("Turn OFF", 255, 135);
  textSize(24);
  fill(33);
  text("Status:", 180, 200);
  textSize(30);
  textSize(16);
  text("Program made by Dejan Nedelkovski,\n     www.HowToMechatronics.com", 80, 320);

  text(ledStatus, 155, 240); // Prints the string comming from the Arduino
  
  // If the button "Turn ON" is pressed
  if(mousePressed && mouseX>50 && mouseX<200 && mouseY>100 && mouseY<150){
    myPort.write('1'); // Sends the character '1' and that will turn on the LED
    // Highlighs the buttons in red color when pressed
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(50, 100, 150, 50, 10);
  }
  // If the button "Turn OFF" is pressed
  if(mousePressed && mouseX>250 && mouseX<400 && mouseY>100 && mouseY<150){
    myPort.write('0'); // Sends the character '0' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(250, 100, 150, 50, 10);
  }
}

笔记本与Arduino蓝牙通信界面

选型与边界

HC-05只支持蓝牙2.0 SPP,手机端现在是主流,Android兼容良好,iPhone不认SPP,苹果用户要么换HC-08/HM-10这类BLE模块,要么走WiFi方案。距离标称10米,隔一堵墙衰减明显;另外透传时主控的硬件串口被占用,调试打印和蓝牙通信会打架,建议像例程那样用软串口或干脆分开处理。

总结一下:透传接线四根、AT模式38400、改配置要RESET。这三条记住,HC-05基本没有使用门槛,做一个遥控小车或者无线传感器节点,它都够用。


作者:固件烧录仔
链接:https://www.aiddithome.com/p/12b3591da57b6e.html
本作版权归作者所有,未经允许请勿转载。若存在违规行为,您可以点击 “举报”。


登录 后发表评论
4条评论
老张装机
1楼 · 58分钟前

分压那步千万别省,我见过直接5V怼RX把模块烧了的,3.3V逻辑真扛不住。

触底反弹的Tony
2楼 · 58分钟前

iPhone不认SPP这条很关键,省得有人买回去才发现连不上。

拆机党
3楼 · 58分钟前

正好给遥控小车加个手柄通道,照这个透传例程改改就能用。

阿伟不emo
4楼 · 58分钟前

HC-05经典款了,第一次玩蓝牙就是它,EN脚拉高进AT这个坑我也踩过,接了地半天没反应。