I am using Sonic Pi 3.3.1 on my Windows PC and a Python script(Python3.7.2) on my raspberry pi(Raspbian Buster) that detects distance from an HC-SR04-Ultrasonic Sensor .The program then creates a tune with a pitch that ranges higher if the object is further away ,this tune is then sent over OSC to Sonic Pi. External OSC is enabled on my windows PC. I also checked the Port and ip addresses, and they are correct.

I have tested my circuit extensively and I can confidently say this isn't the source of the problem, and that I added it for documentation purposes only at the bottom of my post for anyone who is interrested ,so I will move on.

My Python code:

from gpiozero import DistanceSensor
from time import sleep

from pythonosc import osc_message_builder
from pythonosc import udp_client

sensor = DistanceSensor(echo=17, trigger=4)
sender = udp_client.SimpleUDPClient('', 4560)

while True:
    pitch = round(sensor.distance * 100 + 30)
    sender.send_message('/play_this', pitch)



The problem doesn't seem to be my python code as it logs the correct values and successfully sends the pitch so once again I will move on.

My sonic Pi code:

live_loop :listen do
  note = sync "/osc/play_this"
  play note[0]


Sonic Pi Output

Sonic Pi log's the notes correctly but doesn't play it and this so the data is being received but isn't being played so the problem is most likely how I am trying to play the note but I can't find the correct way to do this

I then Tried this, to test if sonic pi installed correctly and is able to play sound:

live_loop :foo do
  play 60
  sleep 1

Sonic Pi did play a sound so I can rule out a defective installation where sound is not installed properly

My Circuit:

The Vcc pin on the HC-SR04-Ultrasonic Sensor is connected to the 5V power supply on the raspberry pi, then the Trig pin is connected to the 4th GPIO connector on the board the echo pin is then connected to the 17th GPIO connector between a voltage divider consisting of a 330 ohm resistor and a 470 ohm resistor Wich protects the raspberry from a full 5V

My circuit looks like: circuit


you need to use the cue path as seen in the cue log. So sync /osc: This should have popped up in the autocomplete after the first message was received by Sonic Pi from your Python script.

If you want your sync to work regardless of where the message came from you can use the * sync /osc*/play_this.

Also, a great place to ask Sonic Pi specific questions is our friendly forum:

  • @SamAaronsorry I did what you said but my problem isn't receiving the data because the log is logging the correct values instead my problem is playing the sound – TERMINATOR Feb 7 at 13:26
  • In the code you wrote above, your sync path in the live loop doesn’t match the incoming cue path in the logs - it’s missing the IP address and the port number. If these are the same, it should work. If you’re not sure, then let the GUI’s autocomplete complete the sync path for you :-) – Sam Aaron Feb 7 at 13:51
  • Also, I made a typo with my * example (it’s hard to write code on a phone!) it should be sync "/osc*/play_this" – Sam Aaron Feb 7 at 13:53
  • once I inserted the sync /osc*/play_this it worked thanx so much – TERMINATOR Feb 7 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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