Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active April 20, 2025 18:52
Show Gist options
  • Save andersevenrud/ee59fa06c086c69037693b7f9cee0ae7 to your computer and use it in GitHub Desktop.
Save andersevenrud/ee59fa06c086c69037693b7f9cee0ae7 to your computer and use it in GitHub Desktop.
Klipper piezo buzzer configuration (M300)

I wanted to connect a simple piezo buzzer to my 3D-printer running Klipper to make some beeps when a print starts, stops, etc.

Parts required

  • A passive buzzer (I bought these cheap ones from Ali)
    • Note that active buzzers have an internal oscillator and cannot emit tones of different frequencies.
  • Some wires and a connector

Assembly

I soldered some wires, added some heat shrink tubing to protect the leads to the speaker, then crimped on a Dupont connector:

433039059-7fc21cea-b05c-4a43-9fe0-bccdc9eeab30

Buzzer pinout

Find a PWM capable pin on your printer controller board, in my case EXP1_1 (PE8 on a BTT Octopus Pro v1.1) and configure a pwm_cycle_time pin in Klipper:

[pwm_cycle_time beeper]
pin: EXP1_1
value: 0
shutdown_value: 0
cycle_time: 0.001

G-Code macro

Then create a new G-Code macro with the following parameters:

NOTE: I'm not sure why, but I had to define the parameters so that it mirrors the "standard" M300 (like in Marlin). Using for example FREQ instead of S did not work and just emitted a single tone.

  • I: Iterations (number of beeps)
  • P: Duration (length of beeps)
  • S: Frequency (tone of beep)
[gcode_macro M300]
description: Makes buzzer sounds
gcode:
    {% set i = params.I|default(1)|int %}
    {% set dur = params.P|default(100)|int %}
    {% set freq = params.S|default(2000)|int %}
    {% set cycle_time = 1.0 / freq if freq > 0 else 0.001 %}
    {% for _ in range(i) %}
        SET_PIN PIN=beeper VALUE=0.8 CYCLE_TIME={cycle_time}
        G4 P{dur}
        SET_PIN PIN=beeper VALUE=0
        G4 P{dur}
    {% endfor %}

Testing

In your Klipper console, you can now make some sweet tunes, i.e.:

M300 S1000 P200
M300 S1500 P200
M300 S2000 P200

You can find a collection of tunes in the following Github repo: https://github.com/majarspeed/Profiles-Gcode-Macros/tree/main/Beeper%20tunes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment