You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino项目技术求助:蓝牙模块断开时LED闪烁/蜂鸣器触发代码

Arduino Bluetooth Connection Status Monitor (LED/Buzzer Control)

Hey there! I've got you covered with a straightforward implementation for your project. Below is the complete code, plus hardware setup details to make this work smoothly with either an LED or buzzer.

Hardware Setup

First, let's get the wiring sorted:

  • Bluetooth Module (HC-05/HC-06 example)
    • VCC → Arduino 5V (use 3.3V if your module is 3.3V-only—don't fry it!)
    • GND → Arduino GND
    • TX → Arduino Digital Pin 2 (we'll use SoftwareSerial to avoid blocking the hardware serial port)
    • RX → Arduino Digital Pin 3 (crossed with TX, standard for serial comms)
  • LED or Buzzer
    • Positive terminal → Arduino Digital Pin 13 (add a 220Ω resistor for LED, 1kΩ resistor for buzzer)
    • Negative terminal → Arduino GND

Code Implementation

This code listens for connection status messages from the Bluetooth module (most common modules like HC-05 send +CONNECT when paired, +DISCONNECT when the link drops). It then toggles your LED/buzzer accordingly:

#include <SoftwareSerial.h>

// Pin definitions
const int BT_TX_PIN = 2;
const int BT_RX_PIN = 3;
const int OUTPUT_PIN = 13; // Use this for either LED or buzzer

SoftwareSerial bluetoothSerial(BT_TX_PIN, BT_RX_PIN);
String incomingMsg = "";
bool isConnected = false;
unsigned long lastBlinkTime = 0;
const long blinkDelay = 500; // Blink/beep every 500ms

void setup() {
  pinMode(OUTPUT_PIN, OUTPUT);
  digitalWrite(OUTPUT_PIN, LOW); // Start with output off
  
  Serial.begin(9600); // Optional: for debugging via Serial Monitor
  bluetoothSerial.begin(9600); // Match your module's default baud rate
}

void loop() {
  // Read incoming data from Bluetooth module
  while (bluetoothSerial.available() > 0) {
    char c = bluetoothSerial.read();
    incomingMsg += c;
    
    // Check for connection status triggers
    if (incomingMsg.indexOf("+CONNECT") != -1) {
      isConnected = true;
      digitalWrite(OUTPUT_PIN, LOW); // Turn off LED/buzzer
      incomingMsg = ""; // Reset message buffer
    } else if (incomingMsg.indexOf("+DISCONNECT") != -1) {
      isConnected = false;
      incomingMsg = ""; // Reset message buffer
    }
  }

  // Handle blinking/buzzing when disconnected
  if (!isConnected) {
    unsigned long currentTime = millis();
    if (currentTime - lastBlinkTime >= blinkDelay) {
      lastBlinkTime = currentTime;
      // Toggle the output pin to create blink/beep effect
      digitalWrite(OUTPUT_PIN, !digitalRead(OUTPUT_PIN));
    }
  }
}

Quick Tips

  • Baud Rate Check: If your module isn't responding, double-check its default baud rate (HC-05 is often 9600, but some use 38400). Adjust bluetoothSerial.begin(XXX) to match.
  • Buzzer Swap: No code changes needed—just replace the LED with a buzzer on OUTPUT_PIN. The toggle logic will make it beep on/off when disconnected.
  • Debugging: If status messages aren't triggering, uncomment Serial.begin(9600) and use the Arduino Serial Monitor to see exactly what your Bluetooth module is sending. Some modules use different strings like "CONNECTED" instead of "+CONNECT".

内容的提问来源于stack exchange,提问作者Mohak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:20:07