Programing/code a buzzer on esp32dev

I have tried all sorts of things from copying the buzzer ledc code and numerous code samples from various users from this forum. Every time I try to validate the code it fails to validate.

I tried copying the example code from here: ESP32 LEDC Output — ESPHome and my .yaml file will not validate with that code?

Can someone please help me configure a buzzer on my esp32dev board? I am just testing it out to see if i can do this and get it to work.

Here is my .yaml code that is working and I can see it in ESPHome.

binary_sensor:
  - platform: gpio
    name: "Test Window Sensor 0"
    pin:
      number: GPIO33
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 1"
    pin:
      number: GPIO4
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 2"
    pin:
      number: GPIO13
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 3"
    pin:
      number: GPIO26
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 4"
    pin:
      number: GPIO21
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

switch:
  - platform: gpio
    name: "relay 1"
    id: relay_1
    pin: 
      number: 16
      inverted: true
      mode: 
        output: True
        open_drain: True
    restore_mode: ALWAYS_OFF

text_sensor:
  - platform: wifi_info
    mac_address:
      name: Mac
      entity_category: "diagnostic"
    ip_address:
      name: IP Address
      address_0:
        name: IP Address
        entity_category: "diagnostic"
    ssid:
      name: SSID
      entity_category: "diagnostic"
  - platform: version
    name: "FW Version"
    hide_timestamp: true
    
sensor:
  - platform: uptime
    type: seconds
    name: Uptime Sensor
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"
button:
  - platform: restart
    name: "Restart"

Thank you to anyone that can help me get it added.

Ed

If you do not supply the code that fails, it makes it impossible to see where your mistake is.

Your code doesn’t include anything related to buzzer…
If something fails, post the log.

Sorry about not having the code I used in there.

Here is the example code from this page: ESP32 LEDC Output — ESPHome

All I did was just copy the code into my .yaml file. I also included the log file as well.

esphome:
  name: west-alarm2
  friendly_name: West Alarm Panel 2

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ej2xCokq5H9ebtL0Lbz3K4rEPCeXkb0s7WKNU58sksA="

ota:
  - platform: esphome
    password: "384c9cc895a3d5b9ac869089203c7cb3"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.1.51

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "West-Alarm2 Fallback Hotspot"
    password: "1TwaqhEPPIYb"

captive_portal:

binary_sensor:
  - platform: gpio
    name: "Test Window Sensor 0"
    pin:
      number: GPIO33
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 1"
    pin:
      number: GPIO4
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 2"
    pin:
      number: GPIO13
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 3"
    pin:
      number: GPIO26
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

  - platform: gpio
    name: "Test Window Sensor 4"
    pin:
      number: GPIO21
      inverted: false
      mode: INPUT_PULLUP
    device_class: window

switch:
  - platform: gpio
    name: "relay 1"
    id: relay_1
    pin: 
      number: 16
      inverted: true
      mode: 
        output: True
        open_drain: True
    restore_mode: ALWAYS_OFF

output:
  - platform: ledc
    pin: GPIO5
    id: buzzer

    on_press:
        then:
      - output.turn_on: buzzer
      - output.ledc.set_frequency:
        id: buzzer
        frequency: "1000Hz"
      - output.set_level:
        id: buzzer
        level: "50%"

text_sensor:
  - platform: wifi_info
    mac_address:
      name: Mac
      entity_category: "diagnostic"
    ip_address:
      name: IP Address
      address_0:
        name: IP Address
        entity_category: "diagnostic"
    ssid:
      name: SSID
      entity_category: "diagnostic"
  - platform: version
    name: "FW Version"
    hide_timestamp: true
    
sensor:
  - platform: uptime
    type: seconds
    name: Uptime Sensor
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"
button:
  - platform: restart
    name: "Restart"

Here is the output from the log file:

INFO ESPHome 2024.12.4
INFO Reading configuration /config/esphome/west-alarm2.yaml...
ERROR Error while reading config: Invalid YAML syntax:

while parsing a block collection
  in "/config/esphome/west-alarm2.yaml", line 88, column 3
expected <block end>, but found '?'
  in "/config/esphome/west-alarm2.yaml", line 92, column 3

Thank you for responding.

Output doesn’t have “on_press” option. Or did you try to press the buzzer? :slightly_smiling_face:
What are you looking for to accomplish?

Thank you for responding.

What I was trying to accomplish was having a button in HA so when I press it, it will beep for now. Later I was hoping to expand it to a beep, beep for a door. I also want a constant beep for my garage door, for lets say 10 seconds before it automatically closes.

But, at this point I would like to see a switch or button in HA to where I can make a single beep.

So, I have tried a few things to get this to go. I have tried adding a switch (component) and I failed at adding that as well? I am not too sure where that code went but tried adding it as a switch.

What I am working with is a 3.3v buzzer, on pin 5. My board supports 3.3v and 5v.

Thank for helping or trying to anyway.

Is it a active or passive buzzer?
If passive, you need another way to drive it.
If active, why you want ledc output? Need to control volume?

Again thank you for responding.

It is an active 3.3v: Amazon.com: Gikfun Active Buzzer 3V Magnetic Long Continous Beep Tone Alarm Ringer for Arduino (Pack of 10pcs) EK2148 : Electronics

You asked why ledc, I am not too sure why because I am just getting into ESPHome and want to learn it. I am a beginner at this so, if there are better ways to do what I have researched, I am up for what ever someone suggests to me.

I also have an issue with my Relay as well but was going to tackle that in another thread.

If you don’t need pwm, replace (remove) your output component with simple switch:

switch:
  - platform: gpio
    pin: GPIO5
    id: buzzer
    name: "Buzzer Switch"

Buzzer wired between GND and Gpio5

For a relay it goes the same way.

ps. valid for a small buzzer, otherwise you need additional circuit!

So, what you are saying is that I over thought or my research needs refining. Lol…

Let me try your code and will let you know if I have success.

Again, thank you!

Valid for tiny buzzer, not for some 40mm alarm…

And for the relay, post specs/link to your specific one. Many of them have tricky circuits and it’s easy to fry the Esp if you connect it wrong way.

Yes, they both work correctly now. I can hear a long beep until I turn it off. And the Relay is now working as well. I can hear the relay click on and off.

I appreciate the help.

One other thing, two or three. :slight_smile:

What if I want a beep, beep on the buzzer and have it switch off?

Second, the beep/buzzer the sound is low, how can I make it louder?

Third, for the relay, what if I want it to preform a momentary on (kind of like pushing a button.

If you do not have the time to explain these I can put it in as a question to the ESPHome forum.

Thank you very much for all the help and advice. You rock! Cheers!

You add this to switch:

on_turn_on:
- delay: 1000ms
- switch.turn_off: buzzer

Same Is valid for relay. Adjust the time for your preference.

To make buzzer louder you need to increase voltage. For that you need NPN transistor (or mosfet) to drive it at higher voltage. Do you have any?

Thank you I will try it. No I do not have a NPN transistor or mosfet. I would have to do some research on those.

At this point I am happy that it’s working.

Much appreciated! Cheers!

image

Thank you for the diagram. Do you have a link for an Amazon product that I can build/test with? I think in your diagram it’s a transistor and not a mosfet, right?

Yep, common npn transistor, pull it out from some broken circuit board :wink:

Resistor value is fine anything between 100 and 500 ohm

1 Like

Cool. Thank you again.