This project uses light-dependent resistors to build a pianola roll by detecting light and dark on a piece of paper to play a MIDI note. This one is a bit fiddly, but in terms of building the circuit and understanding the code, I’ve kept it in the “beginner” category.
The issue is that it is just a bit sensitive to actually get the thing working – you will have to mess around with the physical layout quite a bit and adjust the circuit to get it working well.
These are the key Arduino tutorials for the main concepts used in this project:
- Arduino Analog Input
- Using an LDR as a digital input from ‘Tweaking4All’.
- Previous tutorials from the Arduino Nano MIDI Keyboard.
If you are new to Arduino, see the Getting Started pages.
Parts list
- Arduino Uno
- 2x Light dependent resistors (LDRs)
- 2x 10k “trimmer” potentiometers
- White LED
- 180Ω resistor
- 2x Red LEDs (optional)
- 2x 220Ω resistors (optional)
- 5 pin DIN socket or plug
- 2x 220Ω resistors for the MIDI link
- Breadboard and jumper wires
- Housing for the LDRs and LED
The Circuit
I’ve included the schematic as this is another one of those circuits that is easier to show in schematic form than on a breadboard. And as you’ll see my own breadboard implementation was slightly different again, having been built on a prototyping shield.
The basic idea is explained very well in the LDR tutorial linked above, but the general idea is that the variable resistors are adjusted until the Arduino can distinguish between what you are considering light and what you are considering dark.
I was finding that it was just too sensitive to ambient light conditions, making it very unreliable, so I added an always-on white LED to shine on the LDRs. I also constructed a simple housing for the two LDRs and the white LED that allows me to feed paper through it easily. This was made out of Lego and you can see it below.
Other simplifications involved tying the anode of the white LED and two of the leads of the LDRs together so I only needed a single +5V connection wire. And of course my now obligatory “solder the two resistors for the MIDI connection inside the DIN plug” move.
The Code
Most tutorials for using light dependent resistors with an Arduino assume you will be reading analogue value that is dependent on the intensity of light falling on the sensor. But I wanted to leave the option open to use a large number of sensors – well at least enough for a full octave anyway, so wanted to use the digital pins.
The tutorial referenced at the start of the page shows how this can be possible using a trim variable resistor to adjust the voltage until it is just right to recognize the difference between what you are calling “light” and “dark”. But the complication is that you need a means to be able to set that trim pot as part of a calibration phase.
Initially I had a different calibration mode that printed out the digital reading from the sensor – 1 for light present, 0 for no light present. This worked fine, but means changing the code when you are ready to do it for real.
Next, I just tried with the MIDI connected up itself, but this was very awkward to actually do when generating sounds.
Finally I decide to add some visual indication of MIDI note on or off. The prototype shield I’m using has two built-in LEDs that you can connect anywhere, so I linked them up to pins 12 and 13.
So the code has four lists of values to keep track of:
- The list of pins to use as inputs for the “keys” – the LDRs.
- A list of MIDI notes to play for each key.
- A “last key status” indication for each key, so that I can detect the on to off and off to on changes and trigger the MIDI events appropriately.
- And finally a list of pins to use for option LED outputs.
Given all that, the actual code is fairly straight forward. I wanted keys to trigger on black – i.e. dark, so I’ve reversed the logic. The most complicated part of the code then is that for each key ( ldr), it does the following:
- If the key is pressed (ldr is dark), then if it wasn’t pressed before, trigger the MIDI note on message for that key and turn on the optional LED.
- If the key isn’t pressed (ldr is light), but it was pressed before, trigger the MIDI note off message for that key and turn off the optional LED.
- Otherwise – i.e. if it is pressed, but was already pressed, or it if isn’t pressed, but wasn’t pressed before – then do nothing.
Having LEDs, although optional, makes calibrating it a lot easier.
Closing Thoughts
This demonstrates the basics ok, but it is only two notes, so is a little limited. I’d like to try to build a fully chromatic octave, but will need some way of gaining better separation between the LDRs in the “sensor”.
To get up to that many notes though, I think it will be off to some stripboard and soldering again though.
Kevin