An internet-connected Lego minifig

Who doesn’t love a cute little Lego minifig? Who doesn’t smile when they see brightly coloured LEDs in interesting places? Who doesn’t get excited about being able to control stuff via the internet? Nobody. How about combining all three?

Here’s how to build your very own IoT, LED-illuminated, minifig.

Step 1 is to acquire a suitable fig. Mine is, of course, a Citrix-branded one acquired from this year’s Citrix Synergy conference. (Other brands are available 🙂 ) As we’re going to be shining LED light through the minifig’s body, it’s best to use one with a white torso.

Step 2, which really only applies if you’ve got a Citrix minifig and you wish to taunt the Citrix branding people who are still trying to rid the world of the red dots that used to form part of the logo, is to use a small drill (I used a 0.8mm bit with a Dremel 3000) to drill holes through the front of the minifig’s torso where the two dots in the logo are located. Be careful not to drill through the back too. (You might want to make sure other minifigs nearby don’t witness this drastic surgery!)

Drilling holes in the minifig's torso

The LED I recommend is a single segment from a flexible strip of PCB-mounted WS2812B LEDs manufactured by Alitove (this item at Amazon UK). You could use pretty much any 5V WS2811/2812 LED that will physically fit, however this particular model fits the minifig torso well and being a surface-mount device, in the orientation it assumes inside the minifig, shines its light towards the front of the figure. I used scissors to cut a single segment from this strip.

Strip of Alitove 5V WS2812B LEDs

Step 3 is to create space inside the minifig’s torso to house the LED. Use a sharp knife to cut away the two diagonal protruding plastic veins on the back of the torso. You may need to use a small pair of pliers to twist and pull out the plastic. You may also wish to cut out the front veins as well, to give light from the LEDs a less obstructed path to the minifig’s front.

Inside the minifig torso, showing the rear plastic veins cut away

Make sure the LED fits inside the torso, with the LED’s lens pointing towards the minifig’s front. You may need to slightly trim the sides of the PCB.

Inside the minifig's torso, showing the LED in situ

Step 4 is to solder small wires to the LED PCB’s positive, negative and signal pads. For the signal pad make sure it’s the input one. Usually the arrow on the PCB points away from the input and towards the output; these LEDs have the signal in the middle, with the positive and negative towards the edges of the PCB. It’s best to direct the wires sideways, along the PCB, rather than perpendicularly away from it – this will allow the torso to fit snugly against the legs later. In the photo below the positive is red, negative blue, and signal white. After soldering, check the LED still fits inside the minifig’s torso.

Soldering wires to the LED PCB.

Because we’re using the space inside the torso for the LED, the usual manner of attaching the minifig’s legs won’t work. Therefore, step 5 is to cut off the studs at the top of legs, making the top of the leg unit as flat as possible.

Cutting off the studs from the top of the mnifig's legs.

Step 6 is to create a route for the wires to exit the minifig. The wires will route from the torso through the top of the legs and then out the existing holes on the back of the legs. Ensure that the leg joint it straight (i.e. as if the minifig was standing up) and drill a hole through the top of the hinge to create a hole from above the legs into the hollow inside of the leg. This should be done on the outside of the leg to avoid the hinge itself. I used a 1.6mm drill bit which created a hole big enough for two wires. Do this for both legs. You could of course also have the wires exit from the back of the torso using holes drilled there, which would allow the legs to bend, whereas in my case the legs are fixed due to the wires fouling the hinge.

Drilling a hole through the minifig's legs to allow wire egress.

Step 7 is to install the LED: ensure the LED is facing forwards and thread the three wires through the two holes drilled in the top of the legs and then out the existing holes in the back of the legs, as in the photo below.

LED wires routed through the minifig's legs

Ensure that wires are positioned such that the LED can be pushed down against the top of the legs.

LED sitting on top of the minifig's legs.

Step 8 is to attach the torso and legs. Because we’ve removed the studs this will requiring glue or poly cement (I used the latter). First, before applying any glue or cement, check everything fits by pushing the torso over the LED and ensuring the torso fits snugly against the legs. You may need to trim plastic, adjust wires, etc. as necessary. Apply the glue/cement according to manufacturer’s instructions and hold the two pieces together until the bond is made. You can then place the head on the minifig in the normal way.

Using poly cement to attach the legs and torso.

Step 9 is to connect the wires to a suitable device. In this case I used an Arduino Uno, wired in the same manner as in my Controlling my IoT Christmas Jumper with CheerLights hack. The positive and negative wires connect to the Arduino’s 5V and GND respectively, and the signal wire connects to digital I/O pin 8. I used crimp connectors to make this connection. Additionally, and this is optional, I added a second 0.1″ crimped plug and second connection in the wires to allow me to more easily detach the minifig from the Arduino. In the photo below this connection is out of view at the back but you can see how the wiring from this to the Arduino itself is a separate 3-core cable with red (5V), blue (GND), and green (signal, D8) wires.

Completed minifig connected to the Arduino.

Step 10 is the software on the Arduino. Firstly, if you don’t have it already, install the Adafruit NeoPixel library according to these instructions. Using the Arduino IDE, create a sketch using the same code as in my Christmas Jumper hack, but modify the WS2811_LED_COUNT variable to be 1 instead of 20. Use the IDE and a USB cable to upload the sketch to the Arduino. To test it, open a serial monitor (from the Tools menu in the Arduino IDE) and enter commands such as “COLOR #FF0000” (ensuring that the serial monitor is configured to use newline line ending and 9600 baud) and “COLOR #00FF00” to turn the LED red and green respectively.

Step 11 is to connect this to the internet. There are many possibilities here: all you need is a script/program on a computer or Raspberry Pi that sends COLOR commands to the Arduino. An example from the Christmas Jumper hack is this node.js program which runs on a Raspberry Pi, to which the Arduino is connected via USB. The program polls the CheerLights API and changes the colour of the minifig’s LED to match the CheerLights colour – this makes your minifig glow the same colour as thousands of other internet-connected lights across the world. To use this on a Raspberry Pi:

  1. Ensure you have an up-to-date node.js and npm on the Pi (see here for how)
  2. Create a directory, e.g. /home/pi/minifig and download my code to a file in this directory, e.g. leds.js
  3. Change directory to /home/pi/minifig and install the required libraries using the command “npm install serialport tinycolor2 request”
  4. Run the program: “node leds.js” – you should see the minifig show a colour within a few seconds.
  5. Test the change by tweeting a new colour to CheerLights

Whether you’ve got a Citrix minifig like mine, a custom minifig that looks a little like you, or just one stolen from your kids, you can now have it be an internet-connected, LED-illuminated minifig!

Advertisements

#IoTree

I decided, right then, that the Christmas tree in the Citrix office in Cambridge, UK would soon be Internet-connected.

cmiotreetweetI’ve been a fan and user of Octoblu for a while. So, when I saw Chris Matthieu, Octoblu Supremo, tweet a photo of the Octoblu office Christmas tree with a comment of “Who’s up for adding some #IoT to the tree?!” I couldn’t resist the challenge.

 

I decided, right then, that the Christmas tree in the Citrix office in Cambridge, UK would soon be Internet-connected.

My first step was to find a suitable “thing” to be connected. The criteria included (i) must do something interesting, such as show different colours, and (ii) must be office-safe, so home-hacked mains power was a definite “NO!”

I decided on a cheap, battery powered tree-topper decoration with red, green and blue LEDs with a variety of flashing patterns. I didn’t care about the flashing, really, but the fact that it could told me that the LEDs would probably be wired in a way that I could drive them individually from an Arduino.

Building The StarThe next step was to hack the tree-topper itself. That meant removing the battery unit and the chip that does the flashing, figuring out the wiring of the LEDs (turned out that they were all wired common anode with 6 rings of 5 LEDs with a common cathode per single-colour ring).

My tool of choice for interfacing with LEDs, switches, etc. is the fantastic Arduino family. In this case, I used an Arduino Uno, which happens to have 6 PWM outputs that match nicely to the six LED rings, allowing me to control the brightness of each LED ring individually. A back-of-the-envelope calculation suggested a 150R resistor would give me sufficient brightness, but keep with the Uno’s maximum current sinking limits. I put together a quick bit of firmware for the Arduino which takes commands over the USB serial interface to set the brightness for each LED colour.

Arduino and Raspberry PiNext step was to get this Internet-connected. I decided to use a Raspberry Pi that was already set up with node.js and other bits and pieces for connecting to Octoblu. I adapted an existing node.js script that I’ve been using for controlling a blink(1).

Although there is already a blink(1) plug-in for Gateblu, I prefer to run standalone daemons that talk directly to Meshblu. In this case, I simply replaced the code that sends colour commands to the blink(1) via its library with commands that send colour commands to my Arduino via the serial port.

  conn.on('message', function(data){
    console.log('message received');
    console.log(data);
    console.log(conn.options);

    var color;
    color = parseColor(data.on, data.color);
    var rgb = color.toRgb()

    var m =  "RED " + rgb.r + "\n";
    console.log("SERIAL: " + m);
    serialPort.write(m);

    m =  "GREEN " + rgb.g + "\n";
    console.log("SERIAL: " + m);
    serialPort.write(m);

    m =  "BLUE " + rgb.b + "\n";
    console.log("SERIAL: " + m);
    serialPort.write(m);

  });

Putting this all together gave me a Christmas tree decoration that can take colour commands via Meshblu. The final step was to put this into an Octoblu flow so that it did something interesting.

I decided to start with the tree showing the outside temperature (something that might actually make this project a useful tool for my colleagues)! The flow–pictured below–used the scheduler node with a per-minute schedule. This triggers the weather node to look up the current temperature in Cambridge. The output from this goes to a generic function node that has a simple JavaScript function that chooses a colour based on the temperature – colder temperatures being more blue and warmer ones more red. The colour name is sent to my Raspberry Pi and Ardunio via the generic Meshblu device node.

Octoblu Flow

Illuminated tree topperThis worked fine, but the temperature isn’t changing very quickly at the moment, so I decided to force a periodic colour change just to make things a little more interesting. I added another few nodes to the flow to change the colour every few minutes, 30 seconds offset from the main weather flow to ensure that there would be a period of a different colour.

One nice thing about Octoblu is that it’s very quick and easy to change things, so what I make the LEDs do today isn’t necessarily what they have to do tomorrow.

I now have a “software-defined Christmas tree!” Maybe later I’ll change the flow so that the tree shows a colour representing days remaining to Christmas or does some sentiment analysis of Twitter activity …

https://blogsprod.s3.amazonaws.com/blogs/wp-content/uploads/2015/12/20151216_184544.jpg