Use beeper from MKS SBC - which PIN?

Hi,
I would like to use the beeper that is part of the SBC (I am talking about the physical beeper that is on the MKS board behind the LCD).

Does anybody know, which pin of the rpi I have to address or how I can find it out by myself?
I tried:

  • ar37: not a valid pin
  • gpio17: a valid pin, but I can get no sound, neither with, nor without PWM

In printer.cfg I have

[output_pin beeper_pin]
pin: rpi:gpio17
#   Beeper pin. This parameter must be provided.
#   ar37 is the default RAMPS/MKS pin.
pwm: True
#   A piezo beeper needs a PWM signal, a DC buzzer doesn't.
value: 0
#   Silent at power on, set to 1 if active low.
shutdown_value: 0
#   Disable at emergency shutdown (no PWM would be available anyway).
cycle_time: 0.001
#   Default PWM frequency : 0.001 = 1ms will give a tone of 1kHz
#   Although not pitch perfect.


[gcode_macro BEEP]
gcode:
    # Parameters
    {% set i = params.I|default(1)|int %}           ; Iterations (number of times to beep).
    {% set dur = params.DUR|default(100)|int %}     ; Duration/wait of each beep in ms. Default 100ms.
    {% set freq = params.FREQ|default(2000)|int %}  ; Frequency in Hz. Default 2kHz.

    {% for iteration in range(i|int) %}
        SET_PIN PIN=beeper_pin VALUE=0.8 CYCLE_TIME={ 1.0/freq if freq > 0 else 1 }
        G4 P{dur}
        SET_PIN PIN=beeper_pin VALUE=0
        G4 P{dur}
    {% endfor %}

Any help would be greatly appreciated - cheers
Bastian

im now trying to work this out too, im wondering, why did you try pin 17?

ok, i have worked out some stuff with this. from reading stuff about the chip itself i found commands to turn pins on and off from the command line and found that this command echo 1 > /sys/class/gpio/gpio82/value turns the buzzer on and this echo 0 > /sys/class/gpio/gpio82/value turns it off. however these only work as root (sudo su)

i beleive the gpio82 maps to the name GPIO2_C2 that should be used in the config but i have a print going right now so i cant check

also, this did not need pwm so it is a DC buzzer (unfortunatly i think that means we have no controller over pitch)

here is what i was looking at to get the command: RockpiE/hardware/gpio - Radxa Wiki

Hey @TomW1605 - thank you very much for this information!
I feel really stupid, that I did not pick up on your answer earlier :frowning:

I can confirm, I can get the beep through the command line as you describe.
In the config though, I found no way to use that pin:

pin: rpi:GPIO2_C2 in printer.cfg gives me:
“Pin ‘GPIO2_C2’ is not a valid pin name on mcu ‘rpi’”

Same kind of error with pin: rpi:GPIO82

I can use a shell script and trigger that from a macro - I would like it more (as it is more direct and elegant) if I was able to address the pin via the correct name.
If you have any more information - it would be greatly appreciated.
Cheers

Getting the beeper on the SV07 to work by using a shell-script takes two files:

  1. macro_beep-beep.sh
#!/bin/bash
# usage: beep.sh [BEEPCOUNT] [BEEPDURATION] [PAUSEDURATION]

# Output raw passed parameters
echo "Raw parameters: $@"

# Default values
BEEPCOUNT=${1:-3}
BEEPDURATION=${2:-0.1}
PAUSEDURATION=${3:-0.5}

# Output all passed parameters
echo "Beep count: $BEEPCOUNT, beep duration: $BEEPDURATION, pause duration: $PAUSEDURATION"


# Function to play a beep
play_beep() {
    echo 1 > /sys/class/gpio/gpio82/value
    sleep $BEEPDURATION
    echo 0 > /sys/class/gpio/gpio82/value
}

# Play the beep for the specified count
for (( i=0; i<BEEPCOUNT; i++ )); do
    play_beep
    sleep $PAUSEDURATION
done
  1. macro_beep.cfg
[gcode_macro BEEP]
gcode:
  {% set beep_count = params.BC|default("3") %}
  {% set beep_duration = params.BD|default("0.2") %}
  {% set pause_duration = params.PD|default("1") %}
  RUN_SHELL_COMMAND CMD=beep PARAMS='{beep_count} {beep_duration} {pause_duration}'

[gcode_shell_command beep]
command: bash /home/mks/printer_data/config/macro/macro_beep-beep.sh
timeout: 10
verbose: False
  • adapt the path to the script-file in macro_beep.cfg (third line from the bottom)
  • include macro_beep.cfg in your printer.cfg
  • then, you may use e.g. BEEP BC=5 BD=0.1 PD=0.5 in other macros (PAUSE, PRINT_END, …)

ive done all this and i keep getting a response in the website gui that you need root privilages. when i tell the shell script macro to run the script with sudo -s bash i get no password was provided. the script works, as i can trigger it from putty manually. any insight? @Bastian

@pgain88 : hm, let’s see if wee can figure it out.
With which user are you accessing the terminal (if you run $ whoami you’ll know).
And what are the owner and permissions on the script file ($ ls -la macro_beep-beep.sh)?

In my setup, the file belongs to user mks and permissions are set as -rwxr-xr-x:
-rwxr-xr-x 1 mks netdev 610 Jan 11 22:26 macro_beep-beep.sh

To change permission: $ sudo chmod 755 macro_beep-beep.sh
To change ownership: $ sudo chown mks:netdev macro_beep-beep.sh

Does any of that fix it?

unfortunately it does not help. my original file permissions were not the same as yours, but even after fixing them with chmod, it does not work.

my file properties pull up as -rwxr-xr-x 1 mks netdev 489 Jan 16 21:37 beeper.sh whoami pulls as mks.

After turning on logging and trying to trigger the command from mainsail i get this.
“/home/mks/printer_data/config/beeper.sh: line 20: /sys/class/gpio/gpio82/value: Permission denied”
“Raw parameters:”
“Beep count: 3, beep duration: 0.1, pause duration: 0.5”

if i try to sudo bash the command i get this error “[sudo] password for mks:
sudo: no password was provided”

however if from a real command line i sudo bash the script, input the password then it will work as expected.

ive used visudo to modify to sudoers.tmp and made sure mks password required was set to no.

any other advice @Bastian

I believe I can see the difference in behaviour clearer now:
With my setup running $ echo 1 > /sys/class/gpio/gpio82/value from the terminal as mks works (you have to echo 0 ... to stop the beep…) - without su or sudo.

Is this the same for you? I believe not.
Also TomW1605 wrote you had to use sudo, but I don’t think that was ever necessary for me.

$ groups mks gives me:
mks : netdev root tty disk dialout sudo audio video plugdev games users systemd-journal input ssh

Is there any group, that my mks has, which your’s doesn’t?

$ ls -la /sys/class/gpio/gpio82/value gives me:
-rw-rw---- 1 root dialout 4096 Jan 23 17:15 /sys/class/gpio/gpio82/value

It looks like it’s the dialout group, which allows mks to access that value.
Who are ownership and permissions for that file on your side?

Thanks for the quick reply!

I can indeed call the gpio beep without sudo yes. Also for anyone else, prepare the following command quickly, the beep is loud lol.

Trying “groups mks” gives me the same results you have.

However “ls -la /sys/class/gpio/gpio82/value” gives me something different, i get;
-rw-r–r-- 1 root root 4096 Jan 24 02:26 /sys/class/gpio/gpio82/value

I have since gone over kill and resolved the issue by doing the worst,
" sudo chmod 777 /sys/class/gpio/gpio82/value"

everything does in fact now work. thanks for pointing me in the right direction.

1 Like

I don’d understand why it is working with the user mks, but not from the macro. I thought, that scripts, which are called from macros would be using exactly that user…

You could try to align the file-ownership and -permissions, so that they are the same as on my setup:

  • $ sudo chown root:dialout /sys/class/gpio/gpio82/value to set dialout as the group, which owns that value.
  • $ sudo chmod g+w /sys/class/gpio/gpio82/value to add “write”-permissions for the owning group (echoing into that file is like writing).

If it works after that - hurray (but I don’t understand why this is the solution…).

1 Like

I got it to work by using a new command shell with super powers in macro_beep-beep.sh and execute the whole command in that:

sudo sh -c “echo 1 > /sys/class/gpio/gpio82/value”

Then I put mks at the end of /etc/sudoers.tmp with sudo visudo:

mks ALL=(ALL) NOPASSWD: /home/mks/printer_data/config/macros/

and changed the macro_beep.cfg to say

command: sudo /home/mks/printer_data/config/macros/macro_beep-beep.sh

1 Like

this crashes my entire firmware when i try it.

i did find that my method of " sudo chmod 777 /sys/class/gpio/gpio82/value" does not work after restart.
must login as root to make these changes stick.

I got it to work from within klipper, without using gcode_shell_command.
/sys/class/gpio/gpio82 inside klipper is rpi:gpiochip2/gpio18. But to make the pin available in klipper, we have to take it away from sysfs

These are the steps to get there:

1. take the pin away from sysfs, so it is available in klipper
To free the pin each time before klipper starts, add a service override file:
$ sudo systemctl edit klipper.service

There are just two lines of content, which go into the file that opens after running the command above:

[Service]
ExecStartPre=/bin/sh -c 'echo 82 > /sys/class/gpio/unexport'

 

2. set up the pin in klipper

In printer.cfg, configure the pin:

[output_pin beeper_pin]
pin: rpi:gpiochip2/gpio18

Add a macro that uses this pin to emit a beep, e.g.:

[gcode_macro BEEP]
gcode:  
  SET_PIN PIN=beeper_pin VALUE=1
  G4 P500
  SET_PIN PIN=beeper_pin VALUE=0

3. reboot the printer

 
 

If you want to reverse the canges to the service, you have to remove the override file - you can remove the whole directory, that contains the file:
$ sudo rm -r /etc/systemd/system/klipper.service.d/
and don’t forget to reboot.

1 Like

thank you for this, i spent so long banging my head against a wall having no idea why the pin wasnt working in klipper

Every time i try this new method i get this error and the system will not start.

Moonraker can’t connect to Klipper!
Please check if the Klipper service is running and klippy_uds_address is correctly configured in the moonraker.conf.

any thought’s?

@pgain88 : no, sorry - no idea why this is happening.

Thanks @Bastian for having investigated this. My two cents:

Directly accessing the gpio from Klipper gets rid of the gcode-shell-command dependency (what is nice), but it does not play together with the makerbase-beep-service, which makes the screen beep when it is touched.
The reason is, that the makerbase service works by writing to gpio82 via sysfs, but it can’t, if the gpio gets unexported.

To get both kinds of beeps working, the gcode-shell-command approach is the better choice (both gcode-shell-command and makerbase-beep-service are installed in the original Sovol image).
But if one does not want the screen beeps, then the direct gpio access from Klipper is a nice approach.

(Btw., I included your description on my klipperscreen page)