Introduction: MIDI Piano Lighting

MIDI controlled light bulb fixture using vintage style "edison" light bulbs.

The control box serves a few purposes, but mainly allows you to change modes:

  • CLASSIC - MIDI notes are mapped to bulbs Cn-Bn mapped to bulbs 1-12, intensity of bulbs is fully on, sustain pedal holds notes of same time, clears all bulbs when released.
  • VELOCITY - same as Classic, but intensity of bulb is controlled by the velocity of the note played.
  • SCROLLING (shown in video) - in this mode, bulbs are lit up sequentially as more notes are played, with intensity of the bulb based on velocity of the note. Releasing the sustain pedal clears the bulbs.
  • AUTOMATIC - Slow moving algorithm that creates a changing visual display in random patterns.

I divided the steps up into three parts :

  1. Hardware (1-3) - Light bulbs, sockets, mounting, etc.
  2. Electronics (4-7) - Explanation of circuitry, how to control light bulb intensity (dimming), midi communication
  3. Software (8-10) - Details into the Arduino firmware, using timers, interrupts and PWM to control the light bulb intensity.

Step 1: Hardware : Light Bulbs and Sockets

Sockets

I chose to use the smaller socket light bulbs (candelabra base, E12 socket) mostly because I had a bunch laying around from another project. However, the smaller base is less of an eye sore in my opinion, and typically is much cheaper than the standard E-26 base light bulb socket (standard light bulb size). I was able to get these sockets on ebay for about 50 cents a piece, which is a lot cheaper than paying $3-4 for a standard light bulb socket.

When soldering the wires to the sockets, you'll notice the solder joint is covered by shrink tubing. That's very important, especially for the live side of the wire so that you can't accidentally shock yourself when it's plugged in. The shrink tubing also provides some mechanical support so that not all of the weight is on the solder joint. I'll talk about it more in the electrical section, but it's very important that the neutral wires are soldered to the outer shell of the socket, not the other way around. This makes it significantly safer for people to be around, in the case that you used exposed metal sockets like I have. The safest solution would be to use insulated sockets, although they will likely be bulkier and more expensive.

Light Bulbs:

The light bulbs come from 1000bulbs.com , although, edison bulbs can be obtained other places as well, including home depot. The important thing is to get a bulb with low wattage. You don't want 12 60 watt bulbs lighting up in your face, even at 25 watts, it can be a little strong sometimes. In addition, the lower wattage makes the electronics and wiring easier because of the lower current requirement.

Step 2: Hardware: Hanging Assembly

The rod that the sockets hang from is just your standard electrical pipe (conduit), you can get this in a wide variety of lengths and diameters at home depot. It's also very easy to cut yourself with a pipe cutter. The best part about it, is that it's dirt cheap. I used a 5 foot pipe that I cut in half, then placed a Tee in the middle to join them, total cost for all the hardware was less than $10.

One nice thing about electrical conduit, is that it's very soft, yet sturdy. So it's easy to cut into, it's easy to drill into, it's easy to shape, but it's also very strong. (And cheap!)

To hang the bulbs, I simply drilled holes in the pipe along one side, about 2 inches apart, then fed the wires through the holes out to the top to the DB25 connector. I decided to cut the wires all the same length and solder them to the DB25 connector first, so that when I went to attach the sockets, I could choose my wire lengths at that time, and have a more visually accurate representation of how it would look.

Step 3: Hardware: DB25 Wiring

Although the light bulbs all share the same neutral wire, I decided to give each bulb it's own pair of wires, all the way back to the electronics box. I could have used a connector with 13 pins on it (12 for the switched line voltage, 1 for the neutral), like a DB15, but that would mean the neutral wire would have to be significantly thicker than the other wires.

At 25 Watts per bulb, each bulb draws ( Watts / Voltage = Current ) -> (25/120) ~= 0.20833 Amps, or about 208mA of current. You can put 200 mA of current through some very small wire gauges without any problem. However, if you were to share the neutral wire, you would have 208mA * 12 = 2.5 Amps of current. Still not a terrible amount, but you would have to use a wire gauge that was thicker for the neutral wire.

Also, because of the way I am hanging my lights, I needed a pretty long cable going from the hanging assembly to the control box, at least about 15 feet. I could obviously make a cable, but that's kind of a pain, instead I figured, if I don't have to worry about the wire gauge, I could just use a DB25 cable and eliminate some very tedious wiring. To keep the wiring simple, I put all the neutral side connections on the bottom row of the DB25 connector, and the LINE side on the top.

Step 4: Electronics : Background

The easiest way to turn a light bulb on and off from a microcontroller is using a solid state relay (SSR). Sharp makes these chips specifically, that can actually handle a good amount of current, and have just about every component inside. Just takes one resistor and you can drive these from a micro-controller pin. That's all well and good for turning the light bulb on and off, but that's where the simplicity ends. Since driving the solid state relay, is really like driving an opto-coupler, which is really just driving an LED, it's logical to think you could control the light bulb intensity by using PWM on the relay, just as you would with an LED. Unfortunately, you can't do this, at least not without doing some other things first.

The difference between a solid state relay, and a basic optocoupler is in the output stage. We need the "opto" part for isolation, so we're not connecting our light bulb circuit directly to our digital circuit for basic safety reasons. However, the output stage of an optocoupler is typically a transistor of some kind (BJT, MOSFET, etc). That will only allow us to pass current in one direction, and a voltage in the other direction would likely damage the part instantly. To control AC devices we need to use a triac. A triac is a type of thyristor which can pass current in both directions, but the important thing to know about triacs is that once they're on, they don't turn off until the current goes below a certain level, close to zero. It's because of this fact that we can't arbitrarily pulse width modulate the LED side of the SSR. In addition, we must be careful to choose a SSR that does NOT have zero-cross detection built in. Some of the SSR's have this feature, to protect the AC device on the other side, but in our case, that will make it impossible to control the triac the way we want. So we need a SSR that has no zero-cross detection, and can handle the current of our light bulb. That last part is pretty easy. The next step is what this is all leading to, in order to control the intensity of the bulb, we have to turn the SSR on and then back off again in one half cycle of the AC waveform. Since the line voltage come in at 60Hz, that means we have 1/120Hz =~ 8.3ms to do this. That's plenty of time for a microcontroller.

The video above demonstrates this happening, the yellow trace is the AC Line voltage, the blue is the PWM signal coming from the microcontroller.

(The box is just a re-purposed switch box that I found at a thrift store. I made cut outs in the back, and use the front panel knobs to change the display modes of the bulbs.)

Step 5: Electronics: Microcontroller - Relays

I had a couple arduinos laying around as well as a custom shield I had made for a different light bulb project, so that's what I ended up using. The shield is really only useful in that it has the optocoupler circuit on it for triggering an interrupt on the arduino when the AC voltage crosses zero. (Zero Cross Detection Circuit). There are quite a few ways to do this, and I happened to have an arduino Mega available, but you could easily do this with a standard arduino as well. You really only need 12 output pins to get it working. If you want the intensity control, you can still do this with standard output pins, but to get the highest resolution intensity, with the quickest code, you need 12 PWM outputs, which the Arduino Uno doesn't have. I have code in the software section for doing this without PWM as well, although you could also implement software PWM and probably be okay on the Uno.

I also happened to have a board from a different project with 12 solid state relays on it, and the necessary resistors. I had to make some slight modifications to it to connect my ground and neutral wires, but the circuit is pretty simple. Each one of the solid state relays needs to be connected to the microcontroller on the LED side, in series with a resistor. Essentially, you're just driving 12 leds as far as you should be concerned. Just make sure that you're connecting to PWM output pins to make your life simpler.

The relays I'm using are Sharp PR22MA11NXPF. They are great because the package is small, they can pass AC current up to 400V! and currents of 150 mA. They are many variations of this package, some can pass currents of over 1000mA if you need higher current applications. I chose to use the low current version as an additional safety concern. If I plugged in anything that could pull over 150mA of current, I may damage my wiring, or myself somewhere else down the line. At least this way, the relay will fry before anything else... hopefully.

(Note: The PR22MA series seems to be discontinued, but the PR33 series is still in production, it's essentially the same part but can have loads of 300mA at up to 600V!)

Step 6: Electronics: Zero-Cross Detection

To do the zero-cross detection, I use an awesome chip made by Vishay, the SFH6206. The two-LED side turns on a transistor on the opposite side, really it's an AC controlled optocoupler. There are many ways to do zero-cross detection, but this way I find to be the simplest and cheapest. You simply buy the smallest 115->12.6V transformer, or something in that range, a 9V or lower would also work. Then, you plug your transformer into the SFH6206 with a resistor to limit the current.

The triac side (AC from transformer) can handle up to +-60mA, but you don't need that much. However, the more current you pass through, the more current is passed on the digital side (transistor sink output), and consequently, the faster response you will have. I chose a 470 ohm resistor in series with my 12.6VAC, which gives me ~ (12.6/470) ~= 27 mA of current. So my transformer only needs to be rated for greater than 27mA, which lets me use a much smaller transformer.

An alternative would be to just plug the AC directly into the SFH6206 and use a bigger resistor. However, if I did this, and I wanted 20 mA of current through the LEDs, I would need 120-1.5(LED forward voltage) = 118.5 / 20mA ~= 6Kohm resistor. However, a 6K resistor, with 20mA going through it is dissipating ~2.5 Watts of power, that's actually kind of a lot, and will generate a substantial amount of heat, while also being very energy inconsiderate :D. I'd much rather not have to worry about leaving my controller box on all the time and use more efficient electronics.

On the output side (transistor sink, open collector) I tie the emitter to ground, and add my own 1K pullup resistor. The reason I don't use the internal pull-up resistor on the microcontroller is that it is much higher, close to 30K. The result is a much slower pulse when the AC near zero volts. As a result, the timing isn't as accurate, and it's an easy fix with only one part to get the zero-cross interrupt to line up better with the AC voltage.

Step 7: Electronics: MIDI

The MIDI interface is very simple. For a detailed explanation of how it works, refer to the midi electrical specification.

Essentially, you're just isolating the UART (serial) transmission. In my case I'm only listening to incoming midi messages, but I really wish I added a MIDI THRU port so that I could still use MIDI out of my keyboard while connected to the lights. If your keyboard doesn't generate any sounds, this will probably be necessary.

The MIDI transmission signal needs to be connected to the UART RX pin of the micronctroller.

In addition, again just because I had one laying around, I added an ethernet shield to the box so that I could transmit my midi messages over the network, or control the light bulbs remotely. The easiest way to do this is to utilize the IpMidi software that's already out there. All you have to do is configure your ethernet shield as a multicast device on the correct port, and now your keyboard will also have a MIDI network bridge. I have quite a few synthesizers hooked up to a different keyboard, and typically only use my piano to play the piano. But I thought it would be nice to be able to have the option to control other equipment if I wanted to. Step 10 goes over how to setup ipMIDI, but it's really a separate thing altogether, so skip over it if you're not interested.

Step 8: Software: Timers and Interrupts

I don't know what pictures to post for explaining the software, so, obviously, probably better to just post a picture with a cat. Also, the full arduino code is available to download above. (lbdOrganPWM.ino)

Timers and Interrupts

In order to use the hardware PWM in sequence with the ac line triggering, all we have to do is make sure the counter that is used to output the PWM value is reset at the zero-crossing of the AC line voltage. With the triggering circuit explained in step 7, we attach a hardware interrupt to this pin to reset the counters when it triggers.

Each timer is initialized at the beginning of the program to the waveform generation mode of Fast PWM mode with ICRn as the top value. This timer mode is what makes using the hardware PWM possible.

Since we have 7-bit resolution in the velocity of the MIDI note, we want to get pretty close to this resolution for the light bulb intensity. Honestly, anything above 16 steps is pretty negligible, but since we can we might as well. So if we set the prescaler of the timers to 1024 (pg. 161 of Atmega2560 Datasheet), then we have 16Mhz / 1024 for our timer clock = 15625Hz. Our goal frequency of our total PWM waveform is 120Hz, to match the half cycle of the 60Hz line voltage. So, we divide 15626/120 to get ~130. That means if we set our ICRn register to130, we will get a frequency matching the line frequency with ~130 steps. That's perfect since we have 128 steps in the MIDI velocity!

After trying this out, there's some variation in the line frequency, and so I ended up using a top value (ICRn) of 122. This seemed like a safe enough value where I would make sure I would turn off the triac at the zero-cross. If you go too far past the zero-cross threshold, you miss it and the light never turns off or dims.

Last thing, about the Timer config is the PWM polarity. Because once the timer gets to it's end point we want to make sure the triac turns off (this means we're close to the zero-cross), we have to use the inverted mode of the PWM pin, so that when the Timer = 0, the pin is low. That just means when we get a velocity value of 100, we really want to set the PWM register to 127-100 = 27.

For handling the zero-cross, the arduino library has a built in function for this using the hardware interrupt:

attachInterrupt(5,zeroCrossDetect,FALLING);

Where 5 is the interrupt number (pin 18 on the Mega), FALLING indicates it is only triggered on the falling edge (1->0, not 0->1) of the interrupt pin, and zeroCrossDetect is the function below.

It works by setting all the counters used by the specific PWM pins I have chosen to zero at the zero cross. It also sets up the prescaler and wave-form generation mode for each timer, the reason for this is so that the timer starts on the AC cycle at the beginning of the program, ensuring there's no offset from the start.

void zeroCrossDetect() { 

  TCNT1 = 0;
  TCNT3 = 0;
  TCNT4 = 0;
  TCNT5 = 0;
  
  TCCR1B = (1<<WGM12) | (1<<WGM13) | (1<<CS10) | (1<<CS12);
  TCCR3B = (1<<WGM32) | (1<<WGM33) | (1<<CS30) | (1<<CS32);
  TCCR4B = (1<<WGM42) | (1<<WGM43) | (1<<CS40) | (1<<CS42);
  TCCR5B = (1<<WGM52) | (1<<WGM53) | (1<<CS50) | (1<<CS52);
}

Step 9: Software: MIDI

I won't spend much time with how the MIDI works, there's plenty of tutorials on that. However, I highly recommend the Arduino MIDI Library found here.

For my needs, I really only need a few of the functions provided by this library. Specifically Note On, Note Off, and Control Change (for recognizing the sustain pedal).

The way I implemented the sustain pedal with the bulbs depends on the mode that the controller is in.

For the "CLASSIC" and "VELOCITY" modes, the sustain pedal keeps any light bulb on of the the same note value until it is released. When it's released, all the bulbs are cleared.

In the "SCROLLING" mode, the sustain pedal is ignored.

Also, when using IpMIDI, program changes and bank changes are recognized, although I haven't completely figured out if I'll use that at all.

Step 10: Software: IpMIDI

This section is really specific to my personal use of the control box. It has very little to do with the light bulbs, but does allow for automation over WIFI.

ipMIDI is a great software module that lets you use your MIDI devices of your local network (LAN).

All you need to do to set this up is configure your ethernet board for sending multicast messages on address 225.0.0.37, on port : 21928.

Then, when you receive midi messages of any kind, just retransmit them as UDP messages, and as long as you have ipMIDI installed on a network computer somewhere, you should start seeing midi notes coming in. Similarly, if you send out messages to the ipMIDI device on your computer, you can control the light bulbs using standard midi sequencing.

Step 11: Glamour Shots

That's it! Here's some more photos :D

Comments

author
TiffanyS1 (author)2015-02-10

Would it be possible to use this with a microphone rather than mapped to specific piano?

author
parth_bhat (author)2014-07-06

Felt Like I was In heaven. Beautiful I enjoyed It A lot.

I'll Also Make This. But I dont Have A midi Piano I have A keyboard ROLAND JUNO-GI Don't Know Weather Itll Work With That Or Not.

But What You Made Is really Ossum

author
JohnnyLightning (author)2014-07-02

Awesome Instructable and awesome piano playing!! What is the name of the first tune in your video please?

author

Thanks! It's called Consolation No.3 by Liszt, but it's just a shortened section of it, the full piece is much better. I didn't really plan out what I was going to play for the video, was a little pressed for time, but glad you liked it!

author
Alderin (author)2014-06-15

Very nicely done!

For the plain English explanation (the oscilloscope explained it fine for me, but not everyone gets those):

The Triac will only shut down when the power going through it drops below a certain level. Using a Triac to control an A/C source means that it won't shut off until the A/C progresses past zero volts in its normal course. What capricorn1 has done is invert the timing of the PWM so that it doesn't turn ON the A/C source until enough time has gone by, then the rest of the wave goes to zero, the Triac can switch off, and the PWM counter resets. Unlike most PWM systems that follow a basic ON then OFF pattern, the inversion makes it an OFF then ON pattern that is compatible with the Triac and the A/C source.

Just in case anyone missed the Inverted PWM signal point.

author
capricorn1 (author)Alderin2014-06-16

Thanks Alderin!

I tried to get that across by talking about the PWM polarity, but your explanation is probably better, and the more the better! The real key with this technique is to make sure that the "OFF" happens just before the next zero-cross, so the period of the PWM has to match half the AC period, minus a little bit of time to be safe, and then also, like you mentioned, has to be inverted. Thanks again!

author
Alderin (author)capricorn12014-06-16

In fact, technically, you don't strictly have to keep the PWM on after the Triac engages, it could be just an enabling pulse. The Triac will remain on regardless of the PWM signal after that, with the possible minor exception of a little bit of the tail end of the drop to zero below the Triac's hold power level.

Not sure if this fact can be used to improve anything, but it is an interesting thought.

Thanks for posting your project!

author
capricorn1 (author)Alderin2014-06-16

I'm glad you brought that up! I wasn't going to go into that detail, didn't think anyone would care, but what you're describing is the way I originally wrote my code. It wasn't until later that I realized I could use the hardware PWM instead. The problem with sending the enabling pulse, is you still have to keep track of the counter to know when to turn it on, for 12 light bulbs, which requires a ton of overhead code. And the best I could was about 16 steps in between cycles, so 4 bit resolution. (still plenty to be honest for a light bulb). However, by using the hardware PWM, changing the intensity is as simple as writing a byte to the OCRn register, then no more code is necessary, and you get almost 7 bits of resolution! So it's a win-win, even though you're right, the signal doesn't need to be in the form of a full duty cycle, it just makes the code simpler, lighter, and faster. Thanks again!

author
tinyweasel (author)2014-06-16

This is incredible!

author
sai613 (author)2014-06-15

Amazing! I'd like to make and give it to my friend playing keyboard =)

(And your piano is nice too!)

author
lindarose92 (author)2014-06-15

Wow, this is amazing! Beautiful work!!

author
gai_man_de (author)2014-06-15

It's very impress :x

author
doodlecraft (author)2014-06-14

Totally amazing! Every piano needs this! :)

author
domdomgin (author)2014-06-13

can i use a arduino duemilanove..the wiring is a bit confusing..can you have a pict of what goes where altogether..does it work on any piano..thanks..

author
capricorn1 (author)domdomgin2014-06-13

the wiring is defined in the arduino sketch if you're interested, but it's only important if you're using the mega. If you were to use an Uno, or duemilanove, you could choose any 12 output pins (other than you're RX) which will be needed for the MIDI connection, and the external interrupt pin for zero-cross detection. That leaves you with no other pins! Then you'll have to use software PWM on those pins with a similarly modified timing algorithm to synchronize with the AC line. So it's kind of maxing out the capabilities of the Atmega328 chips, but it can be done.

As for the piano, you can use any ELECTRIC piano that has MIDI OUT.

Alternatively, if you just want to turn the light bulbs on and off without the intensity control, it's easy to do this on a deumilanove or uno.

author
craftclarity (author)2014-06-11

Seriously, this is fantastic. Beautiful work.

author
jkimball (author)2014-06-11

That is very nice looking.

author
Matt Carl (author)2014-06-10

This is so awesome! Totally trying this project this summer, thanks for sharing!

author
jessyratfink (author)2014-06-10

This is absolutely stunning - very nice work! :D

About This Instructable

28,178views

274favorites

License:

Bio: instagram - @capricorn_one
More by capricorn1:Sound Reactive Light Bulb Displays + Stranger Things...MIDI Piano LightingAntique Light Bulb Organ - MIDI/OSC Controlled
Add instructable to: