Tuesday, November 06, 2007

Connecting an LDR to Arduino

A friend asked me to make a quick tutorial on how to connect light dependent resistors to an Arduino board and convert the data to MIDI.

We will start off by simply connecting a single light dependent resistor to the Arduino. The Arduino sketch and Max/MSP patch that are shown below can be downloaded here: http://www.milkcrate.com.au/arduino/ldr01/.

Arduino Hardware
You will need:
  • 1 x Arduino board
  • 1 x light dependent resistor
  • 1 x 100kΩ resistor
  • 1 x breadboard
  • 3 x jumper wires
Connect 5V from the Arduino board to one leg of the light dependent resistor.
Connect the other leg of the light dependent resistor to analog input pin 0 on the Arduino board.
Connect the junction of the light dependent resistor and analog input pin 0 on the Arduino board to one of the 100kΩ resistor.
Connect the other leg of the 100kΩ resistor to ground on the Arduino board.


Arduino Software
Make sure the Arduino board is plugged into a USB port on the host computer. Launch the Arduino software. Go to File > Open. Navigate to the LDR01 sketch file (called LDR01.pde located inside the LDR01 folder).
Select Tools > Microcontroller (MCU) > atmega 168.
Select the correct serial port from Tools > Serial Port >. The correct serial port is the name that takes the form of /dev/tty.usbserial-A....... where ....... is an ID number assigned to the Arduino board connected to the computer.
Select File > Upload to I/O board. The Arduino board will momentarily flash three of its small, yellow LED's indicating that information is being transferred.


Max/MSP Patch
The accompanying Max/MSP patch takes the data from the LDR via the Arduino board and changes it into MIDI data. This section presents an overview covering the functionality of this patch.
MIDI Device: Sets the MIDI device / port to which the incoming data from the LDR will be sent.

Channel: Sets the MIDI channel to which the incoming data from the LDR will be sent.

CC#: Sets the continuous controller number to which the incoming data from the LDR will be sent.

Data: Shows a graphical representation of the continuous controller MIDI data that is being sent to the device, channel and cc# specified.

Scale to min and max: If this is checked, then the patch will find the lowest and the highest number within the data stream and scale the output data so that the minimum will be 0 and the maximum will be 127. This is useful if the range of data is not large enough to generate the changes one expects in the controlled sound events. The idea is to check this box, then present the LDR with the maximum and minimum amounts of light that you think it should recieve, then uncheck the box.

Reset data scaling: Sets the data back to the default.

12 comments:

Anonymous said...

I found this blog post and hoped you might be able to help. I have used a similar circuit to create an optical theremin, however the pitch changes in the sound are only audible within a very concentrated range of the LDR... do you know how to tell the arduino to effectively widen that range?

Here is my source code:

int LDR = 1;
int speaker = 10;
int val = 0;
int tones[] = {3830, 3400, 3038, 2864, 2550, 2272, 2028, 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};

void setup() {
pinMode(LDR, INPUT); // declare the LDR as an INPUT
pinMode(speaker, OUTPUT); // declare the ledPin as an OUTPUT
}

void loop() {
val = analogRead(LDR);
digitalWrite(speaker, HIGH);
delayMicroseconds(tones[val]);
digitalWrite(speaker, LOW);
delayMicroseconds(tones[val]);
}

Sebastian Tomczak said...

I suppose one thing you could try is use a pot with the LDR as the other half of the voltage divider, so that you can set the output range to some degree manually. I would recommend using a pot that is maybe 500K to 1M in resistance or so.

Anonymous said...

Hi there, I tried to download the patch you have loaded up on this page : http://little-scale.blogspot.com/2007/11/connecting-ldr-to-arduino.html but ti doesnt work. Could you please tell me if you still have it, if so I would be most greatful if you could email it to me.

amean_n@hotmail.com

Thanks :)

Anonymous said...

Hi there, I tried to download the patch you have put up for max msp and the info for the arduino board.

http://little-scale.blogspot.com/2007/11/connecting-ldr-to-arduino.html

Unfortunatly it doesnt work.

If you still have the patch would you mind sending it to me by email?

That would be most appreciated :)

Thanks --- Amean

amean_n@hotmail.com

Anonymous said...

Could you also send me this patch? Thanks in advance, Robbie! farragh@iolfree.ie

Robin said...

Since the link to the above code is down, I've shared mine below:


byte data;

void setup() {
Serial.begin(9600);
}

void loop() {
data = analogRead(0);
Serial.print("read: ");
Serial.println(data, HEX);
delay(10);
}

Anonymous said...

Great blog you got here. It would be great to read a bit more about this matter. Thanks for giving this material.
Sexy Lady
Female escort

Unknown said...

Hi, nice project you've got going here! I'm working on something similar and was wondering if you'd be willing to email me a copy of your max patch as the link is dead? I'm not too hot on max unfortunately! Hopefully you get this message, you'd be doing me a big favor! Regards, Matt.

MONO said...

Hello World!

i also need the patch for Max :) as i am very close to the deadline...i will be very greatful! here is my email

mono.stereo@gmail.com

Victor said...

Hi,

As a beginner, this is an interesting post for me. I believe I understand how the circuit works, but I don't understand why you are using a pull-up resistor. This basically inverts the signal, right? With no light, instead of having high resistance, you have low resistance. Is there anything else to it? why bother?

Thanks.

Sebastian Tomczak said...

Victor: The resistor in conjunction with the LDR creates a voltage divider. If it was *just* the LDR, then the output would not register as a change in voltage.

Mark said...

Hi there. I have been searching high and low for exactly this Arduino LDR biz! Thanks for posting it. I am trying to control faders and FX levels in Ableton Live using light and this is perfect! As mentioned about unfortunateley the link to download Arduino sketch and Max Patch is brocken. Any chance you could email / DropBox me the sketch and patch? This would be totally apreciated. Thanks. My email is: mark.brown@mca.com.au

Best

Mark