# ESP32 bluetooth connection with buzzer

**URL:** <https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917>\
**Category:** General Guidance\
**Created:** [December 27, 2022, 6:12am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917 "2022-12-27T06:12:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwintannn](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edwintannn](https://forum.arduino.cc/u/edwintannn)\
**Post date:** [December 27, 2022, 6:12am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/1 "2022-12-27T06:12:30Z")

</div>

Hi, I'm new with esp32. I want to make a project when my ESP32 bluetooth disconnected from my handphone my buzzer will be ring, but when my handphone connected to ESP32 bluetooth it stop ringing. How to make it so?

---

<div class="post-metadata">

**Author:** ![edwintannn](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edwintannn](https://forum.arduino.cc/u/edwintannn)\
**Post date:** [December 27, 2022, 6:13am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/2 "2022-12-27T06:13:17Z")

</div>

Heres my code before

#include "BluetoothSerial.h"  
#include "esp\_gap\_bt\_api.h"

BluetoothSerial ESP\_BT; //Object for Bluetooth

#if !defined(CONFIG\_BT\_ENABLED) || !defined(CONFIG\_BLUEDROID\_ENABLED)  
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it  
#endif

int incoming;  
int pinbuzzer = 18;

void setup() {  
Serial.begin(115200); //Start Serial monitor in 9600  
ESP\_BT.begin("Demensia"); //Name of your Bluetooth Signal  
Serial.println("Bluetooth Device is Ready to Pair");  
ledcSetup(0,1E5,12);  
ledcAttachPin(18,0);

// esp\_bt\_pin\_type\_t pin\_type = ESP\_BT\_PIN\_TYPE\_FIXED;  
// esp\_bt\_pin\_code\_t pin\_code;  
// pin\_code[0] = '1';  
// pin\_code[1] = '1';  
// pin\_code[2] = '1';  
// pin\_code[3] = '1';  
// esp\_bt\_gap\_set\_pin(pin\_type, 4, pin\_code);  
}

void loop() {  
if (ESP\_BT.available()) //Check if we receive anything from Bluetooth  
{  
label\_1:  
incoming = ESP\_BT.read(); //Read what we recevive  
Serial.print("Received:"); Serial.println(incoming);

```
if (incoming == 49)
    {
    ledcWriteTone(0,1000);
    ESP_BT.println("Buzzer ON");
    goto label_1;
    }
     
if (incoming == 48)
    {
    ledcWriteTone(0,0);
    ESP_BT.println("Buzzer OFF");
    goto label_1;
    }   

```

}  
else{  
ledcWriteTone(0,1000);  
}

delay(20);  
}

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [December 27, 2022, 6:34am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/3 "2022-12-27T06:34:03Z")

</div>

> [@edwintannn](#):
>
> I want to make a project when my ESP32 bluetooth disconnected from my handphone my buzzer will be ring, but when my handphone connected to ESP32 bluetooth it stop ringing. How to make it so?

First you need to read this about how to post your code on the forum  
[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)

If I understand your question correctly, you will need to add something like this to your loop().

```arduino
if (ESP_BT.connected())
  {
    //code to turn buzzer off
  }
  else
  {
    //code to turn buzzer on
  }

```

---

<div class="post-metadata">

**Author:** ![edwintannn](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edwintannn](https://forum.arduino.cc/u/edwintannn)\
**Post date:** [December 27, 2022, 6:52am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/4 "2022-12-27T06:52:20Z")

</div>

first of all im sorry i didnt know how to post correctly

when im using your code, class bluetoothserial has no member named 'connected' why is it ?

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [December 27, 2022, 7:04am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/5 "2022-12-27T07:04:13Z")

</div>

Please post the complete code you tried.

A review of the library certainly indicates that .connected() is a supported function

[https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial](https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial)

---

<div class="post-metadata">

**Author:** ![edwintannn](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edwintannn](https://forum.arduino.cc/u/edwintannn)\
**Post date:** [December 27, 2022, 8:01am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/6 "2022-12-27T08:01:00Z")

</div>

Heres my complete code i tried

```cpp
void setup() {
  Serial.begin(115200); //Start Serial monitor in 9600
  ESP_BT.begin("Demensia"); //Name of your Bluetooth Signal
  Serial.println("Bluetooth Device is Ready to Pair");
  ledcSetup(0,1E5,12);
  ledcAttachPin(18,0);
  
// esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
// esp_bt_pin_code_t pin_code;
// pin_code[0] = '1';
// pin_code[1] = '1';
// pin_code[2] = '1';
// pin_code[3] = '1';
// esp_bt_gap_set_pin(pin_type, 4, pin_code);
}

void loop() {
  if(ESP_BT.connected()){
    if (ESP_BT.available()) //Check if we receive anything from Bluetooth
    {
      incoming = ESP_BT.read(); //Read what we recevive 
      Serial.print("Received:"); Serial.println(incoming);

      if (incoming == 49)
          {
          ledcWriteTone(0,1000);
          ESP_BT.println("Buzzer ON");
          }
          
      if (incoming == 48)
          {
          ledcWriteTone(0,0);
          ESP_BT.println("Buzzer OFF");
          }   
    }
  }
  else{
    ledcWriteTone(0,1000);
  }
  
 
  delay(20);
}

```

---

<div class="post-metadata">

**Author:** ![edwintannn](https://avatars.discourse-cdn.com/v4/letter/e/f08c70/32.png) [@edwintannn](https://forum.arduino.cc/u/edwintannn)\
**Post date:** [December 27, 2022, 8:06am UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/7 "2022-12-27T08:06:08Z")

</div>

my current version of esp32 is 1.0.1

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [December 27, 2022, 5:56pm UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/8 "2022-12-27T17:56:36Z")

</div>

> Heres my complete code

You are missing he #include for the library, the object instantiation, and some variables. This is what you you had in the improperly posted code. hen I add it to what you posted, it compiles.

You need to learn how to read the error message the compiler gives you. In "Preferences" turn on the verbose output during compilation.

```arduino
#include "BluetoothSerial.h"
#include "esp_gap_bt_api.h"

BluetoothSerial ESP_BT; //Object for Bluetooth

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

int incoming;
int pinbuzzer = 18;

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 25, 2023, 5:57pm UTC](https://forum.arduino.cc/t/esp32-bluetooth-connection-with-buzzer/1069917/9 "2023-06-25T17:57:16Z")

</div>

Closed on June 25, 2023, 5:57pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
