ESPhome buzzer + switch

hello all

i am trying to connect a active piezo buzzer using ESP Home and LEDC output, see code below. I want to be able to control the buzzer from hassio using virtual switch (and also in automation).

in this page i have found what i need, kinda ( https://community.home-assistant.io/…/passive…/230912/4 ) , the buzzer even makes a sound. However in esphome logs i got an error that PIN26 is used in multiple places (obviously)

Might be super simple and am just missing something. Can someone point me right direction? If i change the PIN the the switch does not work and i dont know how to control the buzzer. The example code in https://esphome.io/components/output/ledc.html… also did not help

output:
- platform: ledc
pin: GPIO26
id: buzzer_output
frequency: 2300hz

switch:
- platform: gpio
name: "Krb Buzzer"
pin: 26

Use a template switch and the ledc output.turn_on/ output.turn_off actions.

thanks for answer. so if i get You right - something like this ?

output:
- platform: ledc
pin: GPIO26
id: buzzer_output
frequency: 2300hz

switch:
  - platform: template
    name: "switch_buzzer_virtual"
    turn_on_action:
      - output.turn_on: buzzer_output
    turn_off_action:
      - output.turn_off: buzzer_output

Looks ok, but obviously you’re in the best place to test :wink:

1 Like

today i finally tested and somehow does not work. I add the switch into hassio and when i try to turn it on, after few seconds it turns off by it self in UI. In logs of the ESPHome i see log, that I turned on the switch, nothing else.
Any idea whats wrong? :slight_smile:

one more update:
just for test, to see if it does at least something, i have added inverted:true into the template switch and it did make a constant sound
afterwards i have found in documentation that i can use optimistic: true . Now I do see the state change in UI of hassio and logs. however the buzzer stopped working. I only hear silent clicking sound when i press the switch from frontend

at last i have made it work. for anyone in future. i did follow ESP32 LEDC Output — ESPHome and updated the code a bit, commented out etc. see below

output:
 - platform: ledc
   pin: GPIO26
   id: buzzer_output  
   # frequency deleted from output as this will be set in switch 
   # frequency: 2300hz

switch:
   - platform: template
    name: "krb_switch_buzzer_virtual"
    optimistic: true
    turn_on_action:
      - output.turn_on: buzzer_output
        #in here we set the frequency, but first we had to turn the buzzer on as stated in https://esphome.io/components/output/ledc
      - output.ledc.set_frequency:
          id: buzzer_output
          frequency: "2441Hz"
       #am just a newbie, so i dont exactly why the code below needs to be used, however it does not work without it
       # in documention i have found " This action sets the float output to the given level when executed"
       #which does not tell much to me
      - output.set_level:
          id: buzzer_output
          level: "75%"
    turn_off_action:
      - output.turn_off: buzzer_output

in any case ,thanks @koying for pointing me the right direction

1 Like