ESPHome and passive buzzer continuous beep sound

Hi,

I want want to add code to my esp d1 mini for continuous beep with pasive buzzer
I have tested the code below and it makes only 2 beeps. I need continuous 2 beep sound when triggerd. Please help me with this.

Code

esphome:
  name: lamp
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "******"
  password: "******"

logger:
ota:

output:
  - platform: esp8266_pwm
    pin: D1
    id: 'buzzer'

api:
  services:
    - service: lamp_notify
      then:
        - output.esp8266_pwm.set_frequency:
            id: buzzer
            frequency: 800Hz
        - output.set_level:
            id: buzzer
            level: 50%
        - delay: 120ms
        - output.turn_off: buzzer
        - delay: 200ms
        - output.esp8266_pwm.set_frequency:
            id: buzzer
            frequency: 1000Hz
        - output.set_level:
            id: buzzer
            level: 50%
        - delay: 120ms
        - output.turn_off: buzzer

I think all the examples about continuous buzzers use ESP32 - like the example here:

You could have a play with RTTL buzzer. I have used this before and it works well. I can’t remember if it loops automatically but worst case it you set up a timer and call the play command once you expect it to finish.

It is my approach to turn on a buzzer with pauses. Basically it uses a template binary sensor to control the buzzer.
Strictly speaking, It is not necessary to create the template sensor if you put everything in the same block.

binary_sensor:
  - platform: template
    id: buzzer_repeat
    internal: True
    on_press:
      while:
        condition:
          binary_sensor.is_on: buzzer_repeat
        then:
          - output.turn_on: buzzer
          - output.ledc.set_frequency:
              id: buzzer
              frequency: "2000Hz"
          - output.set_level:
              id: buzzer
              level: "50%"
          - delay: 200ms
          - output.turn_off: buzzer
          - delay: 200ms
    on_release:
      then:
        - output.turn_off: buzzer


  - platform: gpio
    id: button1
    pin:
      number: GPIO32
      mode:
        input: true
        pulldown: true

    on_press:
      then:
        - output.turn_on: led
        - binary_sensor.template.publish:
            id: buzzer_repeat
            state: True      
    on_release:
      then:
        - output.turn_off: led
        - binary_sensor.template.publish:
            id: buzzer_repeat
            state: False

output:
  - platform: gpio
    pin: GPIO02
    id: led
  
  - platform: ledc
    pin: GPIO33
    id: buzzer

1 Like

Thanks for taking the time to share this, Randy. It solved my problem, I like it, I wasn’t on the verge of thinking of this solution myself, you saved me time, and I am grateful.