I would like to control Sonic Pi via the Rasberry Pi GPIO pins. For example if a pin goes high, a tone starts playing. It appears that Sonic Pi can only be controlled on the local machine using OSC commands.

  1. is this the only option?
  2. is a background Python script that polls the GPIO pins and then sends a OSC command, the most elegant way to do this?
  • Isn't this question about "Sonic Pi", and not really about "Raspberry Pi"??
    – Seamus
    Commented Nov 11, 2023 at 15:02
  • Sorry, first time i’ve ever used this forum. It is Sonic Pi running on a Raspberry Pi. Not sure if there is a more accurate subject/tag. Commented Nov 11, 2023 at 17:46

1 Answer 1


You have to install the necessary libraries for both Sonic Pi and GPIO control in Python. You can use the python-osc library for sending OSC messages from Python. You can write the following python script to send an OSC message to Sonic Pi.

import RPi.GPIO as GPIO
from pythonosc import udp_client

# Set up GPIO
GPIO.setup(<your_pin_number>, GPIO.IN)

# Set up OSC client
client = udp_client.SimpleUDPClient("", 4557)  # Sonic Pi's default OSC address

def callback(channel):
    if GPIO.input(channel):
        # GPIO pin is high, send OSC message
        client.send_message("/play", "<your_sound_command>")

# Add event listener
GPIO.add_event_detect(<your_pin_number>, GPIO.BOTH, callback=callback)

    while True:
        pass  # Keep the script running

except KeyboardInterrupt:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.