Help with ESPHome and passive buzzer

This is my first time working with ESPHome and I’m trying to repurpose a little lamp that has some rgbw lights and a passive piezo buzzer in it. I’ve gotten the lights to work perfectly, but the little buzzer is giving me a really hard time. Mostly I am just really confused as to how the PWM works with ESPHome and could really use some help. The code below only produces clicks. I’m using a 2 pin passive buzzer connected between D1 and GND.

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.turn_on: buzzer
        - delay: 500ms
        - output.turn_off: buzzer
        - delay: 1s
        - output.esp8266_pwm.set_frequency:
            id: buzzer
            frequency: 1000Hz
        - output.turn_on: buzzer
        - delay: 1s
        - output.turn_off: buzzer

That output.turn_on will give you a 100% duty cycle. I guess you should use a 50% or so, by using set_level

Thanks so much. I’ll try that out.

Thanks again @glmnet that worked perfectly.

Tell us more! Is the sketch in your first post the final (working) buzzing thing? :loud_sound:

If not please share your config with us! :blush:

I just had to switch output.turn_on with output.set_level, because the passive buzzers don’t work at 100% duty cycle.

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
6 Likes

hello folks, i have a question about a piezo buzzer. I am trying to integrate these into my ESPhome config. now I can get the buzeer working. but I get a long annoying note from it. and I want to generate a short beep when activating the buzzer. Can anyone tell me how to do this. Below my config.

output:

  • platform: esp8266_pwm
    pin: D8
    frequency: 300Hz
    id: pwm_output

light:

  • platform: monochromatic
    output: pwm_output
    name: “buzzer”

What do you need the monochromatic light for?

I don’t need that. what can I make of this ??

I just want a switch that I can use to control the buzzer

ESP8266 Software PWM Output

The ESP8266 Software PWM platform allows you to use a software PWM on the pins GPIO0-GPIO16 on your ESP8266. Note that this is a software PWM, so there can be some flickering during periods of high WiFi activity. Hardware PWMs like the one on the ESP32 (see ESP32 LEDC Output) are preferred.

Example configuration entry output: - platform: esp8266_pwm pin: D1 frequency: 1000 Hz id: pwm_output # Example usage in a light light: - platform: monochromatic output: pwm_output name: “Kitchen Light”

Hi,

Can please anyone tell me what to add to @slvrjuanito code to repeat these two beeps in a continuous loop.

I have looked a lot of examples and didn’t find solution that worked.

Thanks in advance.

here is a little tweak>

output:
  - platform: esp8266_pwm
    pin: GPIO0 #D3
    id: buzzer_output

rtttl:
  output: buzzer_output
  on_finished_playback:
    - switch.turn_off: buzzer_switch1
    - switch.turn_off: buzzer_switch2
    - switch.turn_off: buzzer_switch3
    - switch.turn_off: buzzer_switch4
    - switch.turn_off: buzzer_switch5

switch:
  - platform: gpio
    pin: GPIO0 #D3
    name: "BuzzerMission"
    id: buzzer_switch1
    on_turn_on:
    - rtttl.play: 'MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'   
    on_turn_off:
    - rtttl.stop
    - switch.turn_off: buzzer_switch1
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: gpio
    pin: GPIO0 #D3
    name: "BuzzerMario"    
    id: buzzer_switch2
    on_turn_on:  
    - rtttl.play: 'mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6'
    on_turn_off:
    - rtttl.stop
    - switch.turn_off: buzzer_switch2
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: gpio
    pin: GPIO0 #D3
    name: "Buzzer2"
    id: buzzer_switch3
    on_turn_on:
    - rtttl.play: 'scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b'
    on_turn_off:
    - rtttl.stop
    - switch.turn_off: buzzer_switch3
    restore_mode: RESTORE_DEFAULT_OFF
 
  - platform: gpio
    pin: GPIO0 #D3
    name: "BuzzerStarWars"
    id: buzzer_switch4
    on_turn_on:
    - rtttl.play: 'star_wars:d=16,o=5,b=100:4e,4e,4e,8c,p,g,4e,8c,p,g,4e,4p,4b,4b,4b,8c6,p,g,4d#,8c,p,g,4e,8p'
    on_turn_off:
    - rtttl.stop
    - switch.turn_off: buzzer_switch4
    restore_mode: RESTORE_DEFAULT_OFF
  
  - platform: gpio
    pin: GPIO0 #D3
    name: "Buzzer2short"
    id: buzzer_switch5    
    on_turn_on:
    - rtttl.play: 'two_short:d=4,o=5,b=100:16e6,16e6'
    on_turn_off:
    - rtttl.stop
    - switch.turn_off: buzzer_switch5
    restore_mode: RESTORE_DEFAULT_OFF
    
1 Like

great solution, I used it for a notification ringtone, thanks