Light up some LEDs with RNBO on the Raspberry Pi

Light up some LEDs with RNBO on the Raspberry Pi

You've just made your own desktop synthesizer with RNBO and the Raspberry Pi, you're ready to switch the lights off and jam out - but there's no visual feedback... If there's a bleep in a dark studio but there's no pulses of twinkling light - did it even happen?

⚙️
Before attempting this tutorial, make sure you're already familiar with the basics of exporting your RNBO patchers to the RPi and that your audio interface is working correctly with it.

Things you'll need:

  • 8x through-hole LEDs
  • 8x 220 ohm resistors*
  • 1x 12mm push button momentary switch (optional)
  • Breadboard
  • Hook up wires

*this is a general approximate resistor value for these current limiting resistors (without them, you can burn out your pins on your RPi or blow the LEDs) if you really want to be precise, this tool from digikey is really handy.


We'll start by exporting the following patcher to the Raspberry Pi target:​

We're going to use the LEDs to light up the current step of the sequence. If you've also got a momentary push button handy we'll add that as a little transport control start/stop button.

We'll write a quick python script on the Raspberry Pi to use alongside the rnbo runner that's running the sequencer patcher.

ssh in, or connect up a keyboard and monitor. We'll use a library that should already be on your RPi image called gpiozero and another for communicating with the runner via OSC. You can use any OSC library you like, this example will use pyliblo3. We've used this library before - so if you've done this in a previous tutorial - you can skip this step.

To install pyliblo3 run the following two commands from the terminal on your RPi.

$ sudo apt install liblo-dev
$ pip install pyliblo3

Now create a file called RNBOPi_LEDSeq.py by using nano or any other text editor you prefer.

$ nano RNBOPi_LEDSeq.py

Paste in the following script, then save the file.

from gpiozero import LEDBoard, Button
import liblo as OSC
import sys

# set up OSC client - send all messages to port 1234 on the local machine (rnbo runner)
try:
    target = OSC.Address(1234)
except OSC.AddressError as err:
    print(err)
    sys.exit()
    
# set up OSC server - listening on port 4321
try:
    server = OSC.Server(4321)
except OSC.ServerError as err:
    print(err)

def update_transport_state(path, args):
    i = args[0]
    global transport_running
    transport_running = bool(i)

def handle_step(path, args):
    i = args[0]
    global transport_running
    print("current step:", i)
    led_vals = [0] * len(leds)
    led_vals[i] = 1
    leds.value = tuple(led_vals)

def fallback(path, args, types, src):
    print("got unknown message '%s' from '%s'" % (path, src.url))
    print("don't panic - probably just the runner echoing back your changes :)")
    for a, t in zip(args, types):
        print("argument of type '%s': %s" % (t, a))

# register callback methods for server routes
server.add_method("/rnbo/jack/transport/rolling", None, update_transport_state)
server.add_method("/rnbo/inst/0/messages/out/step", 'i', handle_step)

# Finally add fallback method for unhandled OSC addrs
server.add_method(None, None, fallback)

# Set up RNBO OSC listener
OSC.send(target, "/rnbo/listeners/add", f"127.0.0.1:4321")

# create a button object
button = Button(21)

# create an LEDBoard object representing our array of LEDs
leds = LEDBoard(17, 27, 22, 5, 6, 13, 19, 26)

def toggle_transport():
    global transport_running
    transport_running = not transport_running
    OSC.send(target, "/rnbo/jack/transport/rolling", transport_running)

button.when_pressed = toggle_transport

transport_running = True
OSC.send(target, "/rnbo/jack/transport/rolling", transport_running)

try:
    while True:
        server.recv(100)        
        
except KeyboardInterrupt:
    print("exiting cleanly...")              
RNBOPi_2Pots.py

Now sudo poweroff the RPi, and disconnect the power. Let's create our circuit:

Running the script

Power up the Pi again and run the script

$ python RNBOPi_LEDSeq.py

The script will start the transport rolling, you should be able to turn the transport on and off using the button. Try using rnbo.remote to toggle the transport also - you should see the state reflected in the attrui whether you use the button or the toggle.

If you want to control the step values, you can open the Raspberry Pi Debug Interface to control the parameters.
lys.//

lys.//

I'm an audio programmer, data scientist, grad researcher and media artist from Melbourne, Australia. I specialise in DSP and interactive coding for sound design across a wide array of platforms.
Australia