Nov 5 update on Christmas Light Project

Up until today I prototyped my circuit on a breadboard; today I moved it to a perf board and soldered everything up. It still works!

Here’s a shot of the perf board:

perfboard

It’s just a reimplementation of the same circuit I had already breadboarded (and described in the prior post) except that I did go ahead and update Rg to 330Ω:

mosfet-v2

The eight infrared emitters will be remotely located out by each light strand I’m controlling. The long wires between those remote IR emitters and this board will be hooked up to the terminals as shown, generally with two wires connecting to all but the outer terminals. This is because each LED has two wires but one of them needs to be connected to the next LED in the series arrangement.

The GND goes to the Arduino ground as well as the ground of the external 12.6V power supply. The plus side of that power supply connects at the lower left and the signal from Arduino pin 3 which goes (via resistor Rg) to the MOSFET gate terminal connects at “G”.

The screw terminals I’m using come in two-terminal chunks but they can also snap together to form larger banks. They are very handy to have for projects like this.

Since this whole thing fit on one half of the perf board I can implement the second copy of this circuit on the other half in the same way.

I connected everything up and was happy to learn that my amateur soldering prowess didn’t screw anything up (and especially didn’t fry the MOSFET during soldering).

SOFTWARE

From a previous project I had some code implementing a simple JSON POST interface on an Arduino (with ethernet shield) allowing remote access to digital I/O. That code is up on github.

I took that code and modified it to accept POST requests of JSON to control sending IR codes using the IRRemote library.

The JSON it accepts looks like this:

{ "codes": [ ircode-dictionaries ], "repeat": n }

An ircode-dictionary looks like:

{ "code": 16718565, "bits": 32, "protocol": "NEC" }

The thing I’m controlling takes 32-bit IR codes via the NEC protocol; as I wrote earlier I determined this using the IRRemote library and an IR receiver/decoder in the usual way. The above code will make the LED string RED for example.

You can optionally add a “delay” element to an ircode-dictionary; it specifies a delay, in microseconds, to wait after sending the code. Although the NEC protocol allows for fairly tightly-repeated IR codes, the box I’m controlling doesn’t respond reliably unless you have about 150000usec (150 milliseconds) of delay between two consecutive IR commands. I haven’t tried to refine this number; I just know that 150 milliseconds works and 50 milliseconds doesn’t, so the true limit is presumably somewhere in between. I made the server supply a 150 millisecond delay by default unless you override that with the “delay” parameter.

You can specify multiple ircode-dictionaries in one POST; the main limitation is that there isn’t much ram (a whopping 2K!) on an Arduino so the maximum POST size my code can accept isn’t much. At the moment I have it defined as 350 characters. Still, that’s enough to allow you to do some interesting things. Only the first ircode-dictionary has to have the protocol and the bits specified; those values become the default for subsequent ircode-dictionaries in the same POST. Similarly for delay, if it’s been specified.

Given all that, we can make the LEDs rotate through RED GREEN and BLUE like this:

{ "codes" : [
    { "code": 16718565, 
      "protocol": "NEC", "bits": 32 },
    { "code": 16751205 },
    { "code": 16753245 } ],
  "repeat": 10 }

You have to be careful with the “repeat” because the Arduino server is non-responsive during the execution of all this. It’s probably best to set a “repeat” value that limits the cycle to 5-10 seconds at most (if you use the “repeat” at all even) and then use looping in your script or program driving the Arduino instead.

I wrote a python script using the Requests library to send POST requests to my Arduino.  The Requests library makes it dead-simple to do stuff like this; here’s the key portions of my python code for driving my Arduino (beware of / apologies for wordpress messing up indentation and line breaks):

def sendcodes(url, code_dicts, repn=1):
    topdict = {"codes": code_dicts}
    if repn > 1:
        topdict["repeat"] = repn
    js = json.dumps(topdict, 
                  separators=(',', ':'))
    result = requests.post(url, data=js)
    return result


# send NEC codes. Each argument should either be
# a simple code or a tuple: (code, delay)
def sendNEC(url, *codes, repn=1):
    a = []
    for x in codes:
        try:
            a.append({'code': x[0], 
                     'delay': x[1]})
        except TypeError:
            a.append({'code': x})
    return sendcodes(url, [{'protocol': 'NEC', 'bits': 32, **a[0]}] + a[1:], repn=repn)

I squeezed out extra spaces using the “separators” argument in the JSON conversion, just because of space limitations on the Arduino and thus not wanting to waste any of the limitation on unnecessary white space. For the same reason I only send the “repeat” element if the repeat is more than just one.

Here are some simple examples showing the LED colors changing under control of all this magic.

For the last one (“seizure”) I backed away far enough so the R/G/B LEDs could blend as they will when seen up on my house. The result looks a little bluish but I think that’s an artifact of the video. In any case I think my neighbors will enjoy that one.

INSTALLATION

While I’ve been working on electronics and software, we’ve also been installing the lights on the house. Don’t worry, I won’t actually light them up until after Thanksgiving, respecting proper tradition — but this year I need to prototype and check things out early.

Here’s a picture of some the LEDs running around one part of my roof line:

img_6581

The LED strand is attached to the roof simply by magnets, as the fascia is metal and magnetic. This is going to make it easy to put up and take down each year, though storing it requires some finesse to prevent it from become a magnetic mess. Each magnet has a zip-tie attachment stuck onto it and then is zip-tied to the LED strand. At night this seems pretty bright, I’m looking forward to getting the whole project up and running soon!

Leave a Reply

Your email address will not be published. Required fields are marked *