Go Down

Topic: LDR to Arduino to MIDI, URGENT! (Read 986 times) previous topic - next topic


Hi, we're new to the forum, sorry our first post is under such urgent circumstances!
We're trying to use photo-resistors to trigger midi notes however we've stumbled upon a few problems.

Ideally we need the LDR (photo-resistor) circuit to act more like a switch (simply triggering note-on values) rather than churning out continuous controller messages.  
Having set up the LDR to divide the voltage between the +5v pin and ground (giving a continuous input value at analog pin 0), we want to create a statement which essentially says:
When the value at analog-0 goes above a set threshold, we want to trigger  a single note on message.
We have studied every similar Arduino sketch we could find, however nothing seems to quite work out right for us.
We're getting very little in the way of note-on messages, lots of changes in midi channel, nothing consistent. basically... it's a complete mess.
We can be relatively flexible in terms of what the messages being transmitted are, as long as we get 2 clearly defined messages (which we can later transform into what we need in logic).

Internet findings that come 'closest' to success.

Circuit in use:

Arduino code in use:
// by Tubedogg 12/2006; fabian at tubedogg.de


//This program reads up to 6 analog sensors / potentiometers ans converts the data to Midi-Controller messages
// define Switchpin:
#define switchPin 10
//  define LED:
#define LEDpin 13

// Variables

// define variables for the controller data
int AnalogValue[6] = {0,0,0,0,0,0};    

// define the "lastValue" variables
int lastAnalogValue[6] = {0,0,0,0,0,0};

// select the midi Controller Number for each input
int midiCCselect[6] = {1,2,3,4,5,6};

// select threshold for each analog input  
int thresh[6] = {1,1,1,1,1,1};  

// select number of desired analog inputs (max 6)
int input_no = 6;

void setup() {
//  set the states of the I/O pins:
pinMode(switchPin, INPUT);
pinMode(LEDpin, OUTPUT);
//  Set MIDI baud rate:


// main program loop
void loop() {

for(int i=0;i<input_no;i++){

       AnalogValue = (analogRead(i))/8;    // read the value from the analog input and divide by 8 for range 0-127
   // check if value is greater than defined threshold (good for resistive touchpads etc)
   if ( AnalogValue >thresh ) {
         // check if analog input has changed
         if ( AnalogValue != lastAnalogValue ) {

         //send control change on cc#i
         midiCC(0xB0, midiCCselect, AnalogValue);
         // update lastAnalogValue variable
         lastAnalogValue = AnalogValue;

         //End if
     //End if

//End for

//End Loop

// This function sends a Midi CC.
void midiCC(char CC_data, char c_num, char c_val){
Serial.print(CC_data, BYTE);
Serial.print(c_num, BYTE);
Serial.print(c_val, BYTE);


We realise this code is for continuous controllers, however we're pretty sure it should be a simple case of setting up a threshold which splits the continuous data into an on/off message (of some kind, doesnt really matter if it's note data)

Apologies again for the urgency of this post, we're trying to get a piece complete for exhibition very soon and we cant seem to get past this point!
Any 'light' you can shed on your LDR problems would be greatly appreciated.

All the best,
HalGG Cry


Jun 24, 2010, 01:28 am Last Edit: Jun 24, 2010, 01:28 am by thedude1977 Reason: 1
I am also building an LDR-based midi controller.

Mine uses one LDR to determine pitch based on the amount of light falling on the LDR, and a button that sends note on/note off messages.

It sounds like your project involves several LDRs, each of which triggers a distinct midi note when a threshold has passed.

If this is correct, try checking out any midi arduino project that uses FSRs, or any other analog input.

This beatsneaks project helped me out a lot -- try recreating this but replace the FSRs  with LDRs.


The thing I am struggling with is forcing my LDR to calibrate to different environments. Mine works great in the dark, but is really iffy in a bright room.

Good luck!

Martin Gerken

Your threshold is 1 - that seems low to me. What's the range of valued you get from the LDR? (the rest of the code seems fine to me)


Go Up