Active buzzer, how to get multiple beeps

Hi guys!

As title says, I connected an active buzzer I’m planning to use for audio feedback/notification. It’s a simple 5v buzzer with just + and - connections, works fine using a 2n2222 transistor for driving it. Works fine with this script to play a single beep.

switch:
- platform: gpio
  pin: D3
  id: buzzer
  name: "${device_name} buzzer"
  on_turn_on:
  - delay: 750ms
  - switch.turn_off: buzzer

My question is, is there a way to make it play 3 short beeps? Is there a way to tell esphome to pull the pin up/down in sequence with some delays?

Just put the sequence you want in (say) a script, then trigger it from wherever you want (say a virtual button).

So turn on, delay, turn off, delay, turn on…

A bit like this led example:

script:
  - id: short_double_flash_left_red_led
    then:
      - light.turn_on: desky_light_led_left_red
      - delay: 200ms
      - light.turn_off: desky_light_led_left_red
      - delay: 200ms
      - light.turn_off: desky_light_led_left_red

Or use RTTTL buzzer…

esphome:
  # whatever your would put here

  on_boot:
    priority: -10
    then: 
      rtttl.play: 'start:d=4,o=6,b=200:c,e,2g'
      # or in any other action block

rtttl:
  output: buzzer

# speaker connection
output:
  - platform: ledc
    pin: GPIO26
    id: buzzer

Oops just read this - yes for active buzzer what @Mahko_Mahko said - or some have had success with slow PWM.

I tried it but it sounds weird :slight_smile:

Thanks I didn’t know about scripts, this looks exactly like what I need. Looks like I can even define different beep codes and call them from different automations, which is what I’m looking for. Will try and report back.

1 Like

Finally got around to test this! I did a script with short 4 beeps, and it works, but the switch looks like this when it’s running: https://files.catbox.moe/94y8ad.mov

Is there a way to avoid the switch flicker? I’m afraid not.

This is the yaml I’m using.

switch:
- platform: gpio
  pin: D3
  id: buzzer
  name: "${device_name}-buzzer"
  on_turn_on:
  - script.execute: buzzer_fast

script:
- id: buzzer_fast
  then:
    - switch.turn_on: buzzer
    - delay: 100ms
    - switch.turn_off: buzzer
    - delay: 50ms
    - switch.turn_on: buzzer
    - delay: 100ms
    - switch.turn_off: buzzer
    - delay: 50ms
    - switch.turn_on: buzzer
    - delay: 100ms
    - switch.turn_off: buzzer
    - delay: 50ms
    - switch.turn_on: buzzer
    - delay: 100ms
    - switch.turn_off: buzzer

I know this is old post. Have you tried to use different pin rather than D3 to confirm its not flickering?

Anyone trying to setup something like this, you may use simplified version of this as given below. I am using this on esp8266 d1 mini.

switch:
  - platform: gpio
    pin: GPIO14 #Your GPIO where the buzzer is connected at
    id: buzzer
    name: "Your Alarm"
    on_turn_on:
    - script.execute: buzzer_five # Name this as you want.
script: 
- id: buzzer_five # Use the same buzzer name you have given above. 
  then:
    - repeat:
          count: 5
          then:
            - switch.turn_on: buzzer
            - delay: 500ms                
            - switch.turn_off: buzzer   
            - delay: 1000ms

Hi sri4iot, the solution to the switch flickering was just to use a button instead of the switch. With newer esphome versions I have also found that there is less random delays in beeps.

button:
- platform: template
  name: "${device_name}-buzzer"
  on_press:
  - script.execute: buzzer_fast