Fundamentals
Export Targets
Code Export
Patcher UI
Special Topics
RNBO Raspberry Pi OSCQuery Runner
Working with Photoresistors (LDRs) on the Raspberry Pi with RNBO
In this article we'll cover how to use Photoresistors with RNBO on the Raspberry Pi target. Before attempting these, make sure you're familiar with the basics of exporting your RNBO patchers to the RPi and that your audio interface is working correctly with it.
Warning! The following tutorial is not yet supported on the RPi 5.
NOTE: This article is intended as a guide to help you extend the use of the RPi. Though the information here is accurate and correct, when working with electronics there is always the risk of damaging your device or components. While the voltage in the RPi is low enough not to pose any threat to your physical safety, the device itself could be damaged. Cycling 74 cannot be held responsible for any damages resulting from attempts to complete the following project. We also cannot provide technical support for RPi beyond basic setup of the image and loading RNBO patchers. Please proceed with caution and at your own risk.
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
- 1 x 1uf capacitor
- 1 x Light Dependent Resistor (aka Photoresistor)
- Breadboard
- Hook up wires
We'll start by exporting the following patcher to the Raspberry Pi target:
To control the cutoff parameter of this patcher with an LDR, we'll write a quick python script on the Raspberry Pi to use alongside the rnbo runner - ssh in, or connect up a keyboard. 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 python-osc.
We'll need to install some system dependencies:
$ sudo apt install pigpio python3-pigpio $ sudo systemctl enable --now pigpiod
Lets first create a working directory and create a python virtual environment:
$ mkdir RNBO-LDR $ cd RNBO-LDR $ python -m venv .venv
Now we'll enter the python venv and install some python dependencies:
$ source .venv/bin/activate $ pip install --upgrade pip gpiozero pigpio python-osc
Now create a file called RNBOPi_LDR.py by using nano or any other text editor you prefer
$ nano RNBOPi_LDR.py
Paste in the following script, then save the file.
from gpiozero import LightSensor
from gpiozero.pins.pigpio import PiGPIOFactory
from pythonosc import udp_client
import time
# send all OSC messages to port 1234 via loopback address
client = udp_client.SimpleUDPClient("127.0.0.1", 1234)
# start the transport via OSC
client.send_message("/rnbo/jack/transport/rolling", 1)
# read the sensor from GPIO pin 4
sensor = LightSensor(4, pin_factory=PiGPIOFactory())
try:
while True:
light_level = sensor.value
print(light_level)
client.send_message(
"/rnbo/inst/0/params/cutoff/normalized", light_level)
# sleep briefly to avoid unnecessary work
time.sleep(0.01)
except KeyboardInterrupt:
print("Exiting...")
sensor.close()Now sudo poweroff the RPi, and disconnect the power. Let's create our circuit:
- 3.3v (red) from the RPi connects to one side of the LDR
- GND (black) from the RPi connects to the ground bus rail
- The 1uf capacitor connects the shorter leg to the ground bus rail, the longer leg connects to the terminal strip inline with the LDR
- RPi GPIO 4 (blue) connects in between the LDR and the capacitor
The sensor.value from the LDR is already normalized - so we can use this directly to control the cutoff parameter via the normalized osc address. If you take a look in the patcher, you will see there is some logarithmic scaling of this via @fromnormalized which makes it feel more natural.
Switch the Pi back on, ssh in and enter the python virtual env we created before:
$ cd RNBO-LDR $ source .venv/bin/activate $ python RNBO-RPi-LDR.py
Now grab a flashlight and voila! You should be opening up and closing the filter with your light source.
Materials in this article