Controlling IKEA Dioder Lights with a Raspberry Pi
Ikea rocks at LED lighting. There are very few reasons nowadays not to use LED for most, if not all of your lighting needs, and Ikea is possibly the cheapest place to buy them for your home. You can get LED bulbs that just screw into your existing home sockets, but Ikea also sell the Dioder system, which consists of various white or multi-colour light spots and light strips that all plug into the same standard controller.
The lights themselves run on 12V DC and there are two styles of plug, one two-pin (basically +ve and ground) and one four-pin, which is for the multi-colour RGB lights. They've kept things simple, one pin is the red, one the green and one the blue, with the fourth one as the supply. This means that provided you can provide a 12V DC power source from somewhere, you can control the light intensity (brightness on the white lights, colour and brightness on the RGB ones) using just the pins of a Raspberry Pi.
I'm basically going to run through the procedure I used to get a couple of RGB spots working, if you want a white one, follow the instructions but just do everything once instead of three times. I'm assuming enough technical knowledge to understand that light is made up of three colours, red green and blue, and all three together at the same intensity make white. If you didn't already know that then this tutorial probably isn't for you.
First you'll need a breadboard, some wires (some male-to-male and at least four male-to-female for plugging the board into the pins of the Pi). You'll also need three MOSFETS. If you don't know what these are, think of them as like valves for electric current. Like other transistors they join two smaller circuits and a voltage on one circuit allows current to flow on the other. So we're basically hooking one side up to the Pi pins (which are only 3.3V) and the other side up to the light (which requires 12V). If we tried to control the light directly from the pins, we'd either just not have a high enough voltage to make the light work, or you'd fry your Pi. So these things are kinda important. Most MOSFETS should work if they're logic level (ie 3.3V max). Personally I used this one and it worked just fine.
Which Pin is Which?
With trial and error you can work out which pin on the Dioder's connector controls which colour. Holding the plug with the two guide rails at the top and looking into the end of the wire, the right-most hole is the supply, and the other three are green, blue and red going from right-to-left.
Get some of the male-to-male wires and plug one into each hole. It helps (but isn't essential) if they're the correct colour. Now, just while we're testing, wrap some sticky-tape round the connector to hold the wires in place. This just makes it easier when later on I say something like "plug the red pin of the Dioder into the drain pin of the MOSFET".
Identifying the MOSFETs
Like all field effect transistors, the MOSFETS have a gate, a drain and a source. If we hold the MOSFET this way up...
...the pin on the left is the gate, the middle pin is the drain and the pin on the right is the source. If your MOSFET looks different, don't worry - check the specification for which pin is which. Sometimes it's even written on the transistor itself.
Using a Breadboard
If you want to get into Pi electronics, seriously, buy yourself a load of breadboards and learn to use one. A breadboard, if you're new to electronics, is basically a board with a load of holes in it (some of which are connected to each other) that make electronics a plug-and-play deal. You could just solder the wires to the pins of the transistor and a T-cobbler for the Pi, of course, but a breadboard is so much easier. Especially when you can buy things like this.
Building the Circuit
So first things first, connect the two rails to the positive and negative outputs of a 12V DC transformer (don't turn the power on yet! Turning on the power should be the very last thing you do, once the circuit is complete. You're unlikely to hurt yourself at such low current, but you could fry your transistors or your Pi.)
All Dioder systems come with a 12V DC so you can just snip the connector off the end and poke the wires into the rails. Normally red is positive and black is negative, but in my Dioder at least it used brown and blue cables, so brown is positive and blue is negative. Also, mine were a bit chunky to poke into my tiny breadboard, so I used two screw connectors.
First plug the only wire from the light not associated with a colour into the supply (positive) rail of the breadboard.
Now plug the three (or one if you're white only) MOSFETs into the breadboard next to each other horizontally. Take the three coloured wires from the light itself and plug each one into the drain of one of the MOSFETs.
Next, connect the source pin of each of the MOSFETs to the ground (negative) rail of the breadboard.
Connecting the Raspberry Pi
Final bit of electronics coming up, then we get to programming. First connect the ground pins. The Raspberry Pi has several ground pins and you can use any of them. They are pins 6, 9, 14, 20 and 25, plus pins 30, 34 and 39 for the Pi 2 and Pi 3. Connect any of these up to the negative rail of the breadboard. Next, for each MOSFET, hook the gate pin up to one of the Pi's GPIO pins, and remember to which one each is connected. The GPIO pins are as follows:
|GPIO 17||RPi Pin 11|
|GPIO 18||RPi Pin 12|
|GPIO 27||RPi Pin 13|
|GPIO 22||RPi Pin 15|
|GPIO 23||RPi Pin 16|
|GPIO 24||RPi Pin 18|
|GPIO 25||RPi Pin 22|
It doesn't matter which you use, just remember which GPIO pin is connected to which colour. Now plug everything in, turn on the Raspberry Pi and the DC adapter. Nothing should happen to the lights just yet (if they light up now you have a problem!). Now wait for the Pi to boot.
Setting up the software
You need something to interface directly with the GPIO pins. I find a good package for this is pigpio. You can install it from the Pi's command line by typing...
sudo apt-get install pigpio
It's quite good in that it allows you to control the GPIO pins of Pis across a network, so if you have a Pi hidden in your ceiling wired to some Dioders but want to do all your coding on another Pi somewhere else in the house, you can do just that.
Control from BASH
You can control the lights directly from the command line. Make sure the daemon is running and has root (it needs this to access the hardware, at least on older Pis) Do this by typing
Now you can send values to the pins directly. Remember which pin you hooked up the red wire to (in my case GPIO17)
pigs p 17 255
The 17 is the GPIO pin number (not the Raspberry Pi pin number, these are different) and the 255 is the value to send to that channel. 255 is the maximum. Turn the lights off by sending 0 to all three colour pins, and turn them to white by sending 255 to red, green and blue. You can mix colours, so for example, sending 255 to red and blue but 0 to green will get you magenta.
Control from Python
Every Pi programmer speaks Python, apparently, so there's a Python library for pigpio
Now try making the lights magenta as before...
red_pin = 17 # obviously, set this to whichever pin is red
green_pin = 18 # ditto for green
blue_pin = 27 # and for blue
pi = pigpio.pi()
If you want to control Pis over the network, it's...
pi = pigpio.pi("192.168.0.27") # replace with the IP address of the Pi
Hopefully this is enough to get you started. You can hook multiple lights up to one Pi provided you have a MOSFET for each colour channel (so number of lights multiplied by three, basically.) You can also chain the lights so one Pi and circuit can control many Dioder strips or spots by connecting them in parallel. Have fun!