Les Instruments

Random Gates

last updated: February 15, 2025

Random Gates

This module is meant to output a random MIDI note (0-60) control voltage, and a pulse output as a gate signal. The length between notes played is random, as well as the notes themselves. Can use a quantizer downstream to fit it into a scale. Quick easy way to get some random notes playing. never got it into a PCB -- just a breadboard prototype.

Hookup Diagram

random gates breadboard

Code

The code is written in JavaScript, and uses the Kalumajs runtime. (https://kalumajs.org/).

const { PWM } = require("pwm");
const { getRandomInt, notes, createDutyScaler } = require("../lib/utils");

const GATE_PIN = 19;
const CV_PIN = 11;
const PWM_FREQUENCY = 50000;
const PWM_INITIAL_DUTY = 0;

// adjust this if you're not amplifying the 3.3, or if your op amp configuration is set to something else.
// 4.72V is the output of my non-inverting op amp configuration -- those are just the resistors i had (22k, 10k)
// if you're not amplifying the filtered signal, you'll probably want 3.3v
const MAX_OUTPUT_VOLTAGE = 4.72;
// Create a function that takes in a note number and outputs a duty cycle between 0-1, scaled for the max voltage.
const getDuty = createDutyScaler(MAX_OUTPUT_VOLTAGE);

// Set up the Gate/trigger out pin
pinMode(GATE_PIN, OUTPUT);
digitalToggle(GATE_PIN);

// Set up the pseudo analog PWM CV pin
const CV_PWM = new PWM(CV_PIN, PWM_FREQUENCY, PWM_INITIAL_DUTY);
CV_PWM.start();

let on = false;
let index = 0;

function loop() {
  digitalToggle(GATE_PIN);

  // if we're triggering a note
  if (on) {
    CV_PWM.setDuty(getDuty(notes[index]));
    index++;
    if (index >= 24) index = 0;
    on = false;
  }

  // just taking a rest
  else {
    on = true;
  }

  setTimeout(loop, getRandomInt(1000));
}

loop();