Rotary encoders on BELA

12 August 2016

How to connect a rotary encoder to a BELA-enabled BeagleBone Black.

Hardware used in this post:

Connect rotary encoder

The rotary encoder is connected to the digital pins on the BELA as shown in the figure below. The capacitors are used to do assure a minimum of debouncing.


I captured the data with SuperCollider running on my laptop. Encoding is done via UGens within the synthdef itself.


Start the server remotely, either by ssh’ing into the board or directly from within your SuperCollider IDE.

$ ssh root@
bela$ scsynth bash -c 'scsynth -u 57110 -z 16 -J 8 -K 8 -G 16 -i 2 -o 2'
"ssh root@ \"screen -dm -S scsynth bash -c 'scsynth -u 57110 -z 16 -J 8 -K 8 -G 16 -i 2 -o 2 > scsynth.log'\"".unixCmd

Initialise the server

Server.default = s = Server("belaServer", NetAddr("", 57110));

First steps

First, listen to how the raw values coming from the pins can be used directly as amplitude envelopes for oscillators:

// simply listen to the pulses
Ndef(\a, {
  var trigs =[0, 1, 2]); // A, B, button[100, 800, 1900], 0, amps))

The pulses of the pins can be interpreted as a 2bit gray-encoded number.

// convert gray code into binary values
Ndef(\a, {
  var pA =;
  var pB =;
  var gray = [pA, (pB * 2)].sum;
  // convert to standard binary encoding
  var bin =,[0, 1, 3, 2]));

  // for every change in the signal, create a trigger
  var changed = +;

  // poll whenever something changed  
  bin.poll(changed); //poll changes (output on bela!) * (bin + 1)) *, 0.2).tanh!2;


If only one update per click is required, the code can be simplified. Additionally, we compute the direction of turning and feed this into an integrator to get absolute value changes.

Ndef(\a, {
  var pA =;
  var pB =;

  var trig = * pB) > 0;
  var sign = - pB);

  // sign.poll(trig, "sign");
  // trig.poll(trig, "trig");
  var note = * sign).poll(trig, "int");!2;