I wanted to connect a simple piezo buzzer to my 3D-printer running Klipper to make some beeps when a print starts, stops, etc.
- 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
I soldered some wires, added some heat shrink tubing to protect the leads to the speaker, then crimped on a Dupont connector:
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
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 ofS
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 %}
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