Skip to content

Lightroom Controller

May 12 2019

Intro/Purpose ​

This project was created my Hunter and Peyton to improve the speed of editing photos in lightroom. At the time both of were side-hustling photographers. We have seen other HID (human interface devices) that utilize sliders, knobs, and buttons to interact with the dials in photo/audio editing software and used those as inspiration for the design.

How it works ​

The rotary encoders are all attached to the MCP32017 I/O Expander. The interrupt pin of the MCP32017 is tied to an interrupt-capable pin of the microcontroller. When the expander detects a change of input values, it immediately alerts the microcontroller. The microcontroller decides if the interrupted pin is a button or encoder. If it is an encoder, it uses an algorithm to determine the direction it was turned. It then takes that information (button/rotation) and sends it to the PC as a MIDI signal over USB.

In summary, that data is trasmitted throught like so:

Rotary Encoder/Button > MCP32017 I/O Expander > I2C > Arduino > MIDI signal over USB > MIDI2LR Plugin > Lightroom

Because the controller sends simple MIDI to the PC, you can used this board to control other software that can interact with MIDI HIDs. Most DAWs (digital audio workstations) will accept MIDI control.

Putting it together ​

Hardware ​

Arduino Pro Micro ($4)The requirements needed to drive this software are pretty small so any microcontroller should work. We chose the ATmega32u4 since it met all of them and is what we had on hand.
Rotary Encoder (~$10) x9We went with rotary encoders instead of potentiometers because we did not want any noise in the input and we wanted the haptic feedback the encoders give
PCB ($2)Purchaced on JLCPCB
Knobs ($17)If the encoders used did not come with knobs, you can find a large variety online along with with a large variety of prices.
MCP32017 x2I/O Expander that utilizes the hight speed & expandable I2C bus

Schematics & PCB: https://oshwlab.com/phcreery/lightroom-controller

SchematicPCB

Althought the code is written for the ATMega32u4, it should work with any microcontroller that meet the following requirements

  • I2C Bus
  • At least 2 Interrupt pins
  • 8MHz clock speed. The faster, the better.

3D printed model of case:

CaseBase

Software ​

The microcontroller source code can be found on the github link listed in the sources section. This is one of the first programs I have written in C, so it is not elegant in any manner. I am writing this post years afer I wrote the original code so I will try my best to explain what is going on.

Used Libraries

I tired to make this code as versatile as possible. Modifications to the wiring can be applied throught a couple global variables. This includes, number of encoders, number of buttons, encoder pin map, button map, LED pin map, which encoder is the selector, arduino interrupt pin, expander addresses, etc.

At setup, the first thing that happens is the microcontroller interrupt pins (the ones wired to the exapnder interrupt I/O) are configured (interruptable). Then the expander is configured. It enables all interrupts, applies pull-up resister mode for input pins, and inverts polarity of the ports of the exapnder on addr0. It then does the same for the second exapnder on addr1. Next, the pins conrtolling the LEDs are setup for output use. After that, the interrupts are attached and the main loop is ready to go.

In the case of an interrupt event (when there is a signal change on the interrupt pin), the intCallBack function corresponding to the interrupted pin is immediatley called regardless of what else the microcontroller was doing. Inside those functions, a global variable awakenByInterrupt is set to True and MCP is set to the numeric value of which interrupt happened.

In the main loop, the microcontroller continually checks to see if awakenByInterrupt has been set to True. If it is not, it loops and checks again, forever. If in the case of an interrupt where awakenByInterrupt is True, the first thing it does is collect the value of all the GPIO at time of last interrupt, as hex, on the interrupted expander using expanderRead. Both sides, A and B of the expander, are stored in keyValue. Then appendtoData() is called. Inside that function, keyValue is XOR comapred to the previous value, prevkeyValue, and stored in changedkeyValue. This will represent, in binary, the pins that have changed in value. keyValue is also AND comapred to changedkeyValue and stored into comparedkeyValue. This represent the difference of values between keyValue changedkeyValue. After that, keyValueArray, changedkeyValue, and comparedkeyValue are appended each into a corresponding into a array that stores a history on the last 4 values. This is then put into a global variable, data (it doesnt get more genaric than that) as a multidimensional array (whats a struct?). The data set, in binary, will look like the following for 1 encoder and 1 rotation.

				old	...	...	new
				[0]	[1]	[2]	[3]
prevkeyValue			00	10	11	01
keyValue		data[0]	10	11	01	00
changedkeyValue		data[1]	10	01	10	01	= keyValue ^ prevkeyValue
comparedkeyValue	data[2]	10	01	00	00	= keyValue & changedkeyValue

Now the data is analyzed to determing the harware pin number that was interrupted. If that number exists in the encPins pin map, then the event was cause by an encoder, else, a button. Now this is where the history of the gpio is needed. encoderDirection() is then called to determine the rotation of the encoder. It first check to see if the encoder has done one full rotation

if ((data[2][2] == 0) && (data[2][3] == 0)){

If both first 2 values of comparedkeyValue == 0, a full rotation has occured. Then it checks the last 2 values.

if (data[2][0] < data[2][1]){

If the last value is less than the second-to-last, the encoder was rotated in the clockwise direction. Now you are probably wondering why I have a multidimensional array and not just an array of the comparedkeyValue, well, I don’t have an answer for you.

Now some more logic is done to determine button press. Then some more to handle “Banks” which give X^2 number of virtual encoders where X = number physical encoders - 1 and 1 being the “selector” encoder.

Once the Encoder and Direction are determined, it send that information over MIDI. MIDI is sent as packets called “notes” with data containing channel, pitch, and velocity. In our case, Bank, Encoder, Direction, respectively.

The MIDI signal is then received by a Lighroom plugin called MIDI2LR which decodes the MIDI signal and interracts with Adobe Lightroom.

Conclusion ​

We are very pleased with the result. After several software iterations, it worked perfectly. It actually proved very useful and sped up our work by about double. Althought great, it is not perfect. The LEDs are much too bright. This is due to them being fed 5 volts which is too much and they will experience burn-in for being left on. Another issue is a bug in the software. When you turn 2 encoders quickly at the same time, the microcontroller hangs up and needs to be reset. The PCB has lots of room for imporovement. The MCPs can be swapped for a ones with modern space efficient SMT packages. The arduino can be swapped with a standard ATMega32u4 and a couple decoupling capacitors. The trace widths are probable overkill for the voltage and signals.

Sources ​

Source Code: https://github.com/phcreery/Encoder2MIDI

Wire Arduino Library: https://www.arduino.cc/en/reference/wire

MIDIUSB Arduino Library: https://github.com/arduino-libraries/MIDIUSB

AVR Interrupts: http://www.gammon.com.au/interrupts

MCP23017: http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=10945

MIDI2LR Plugin: https://rsjaffe.github.io/MIDI2LR/

GIF of populated PCB