Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Last active February 20, 2018 18:07
Show Gist options
  • Save dropmeaword/7c42a90e72e9ad56d3ee69ff5a7930fb to your computer and use it in GitHub Desktop.
Save dropmeaword/7c42a90e72e9ad56d3ee69ff5a7930fb to your computer and use it in GitHub Desktop.
Using a simple LDR sensor and an ARDUINO as a MIDI control signal

Setting up

You will need an Arduino library, this one will work and the page behind this link also explains how to install it in your Arduino programming environment.

  1. Open your Arduino programming environment (IDE).
  2. In the menu go to Sketch > Include Library > Library Manager...
  3. Search for a library called Arduino MIDI and install it

library manager

Here's the link to the library: Arduino MIDI library

Load this code into your Arduino:

#include <MIDI.h>

const int DEFAULT_MIDI_CHANNEL = 1;

// created and binds the MIDI interface to the default hardware Serial port
MIDI_CREATE_DEFAULT_INSTANCE();
 
 void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to all incoming messages
  pinMode(A5, INPUT);
}

void loop() {
  int reading = analogRead(A5);
  Serial.println(reading);
  delay(sampleRate);

  // midi only supports values from 0 to 127 so we map our reading from
  // the full range to the midi range
  int midiValue = map(reading, 0, 1024, 0, 127);

  // send midi cc message
  MIDI.sendControlChange(1, midiValue, DEFAULT_MIDI_CHANNEL);
}

If your Arduino is connected to your LDR sensor circuit it should start pumping MIDI control values back to your computer. To route the MIDI signals from our computer's serial input to our other MIDI-aware software you will need a virtual MIDI connector.

Get a virtual MIDI connector

I use Hairless MIDI. It's simple, it's free and does exactly what it says and nothing more.

Open Hairless MIDI, select the serial port where your Arduino is connected as your input and route the MIDI signal to your DAW on the Hairless output.

@Uberdome
Copy link

Hey dude I really like this project, I've had an idea to have a kind of light based MIDI controller/"theremin" for a while.

Slight problem with the code: apparently the "SampleRate" needed to be declared globally (if so, how? I just do "int sampleRate;").

Even when I do this and the code verify's it will still not upload. I'm using an Uno, is the code it meant to be compatible with one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment