Introduction: Lego Mini Cooper App Controlled Lights
Is your dream job being a bomb disposal expert but you are hesitating because of the dying part? Then this is the project for you! You will spend long hours making tiny adjustments to a highly unstable device, sweat poring from your brow and hands shaking uncontrollably. A heartbeat could separate Triumph and Disaster. Either way you will not die, except perhaps of mortification. So you must be prepared to treat those two impostors just the same. Otherwise you might be better off with actual bomb disposal.
What am I on about? Implementing a full-featured set of web-based controls for your Lego Mini Cooper, all accessible from your phone! Highlights include:
- Individual control of exterior and interior lighting
- Lighting selections mirrored on the phone display
- Auto lights feature dependent on light level
- Internet-based timekeeping so your Mini can flash the hour with its lights
- User-configurable time-zone selection
- A near infinite variety of manually-selectable interior lighting colors and brightness levels
- An automatic "Groovy" mode to alternate interior lighting for that true sixties vibe. Yeah, Baby!
- Tesla-like autopilot. No, not really.
Plus convenience nerd features such as:
- Multicast DNS (no need to remember pesky IP addresses)
- OTA (over the air) firmware upgrades to easily add even more features
WiFi Manager so the Mini can be moved to new networks without re-coding SSID and passwords
Let's get started!
Step 1: What You'll Need
If you already have a Lego Mini Cooper you are in luck. This project will cost you about $10. Less if you have some or all of the consumables lying around already. If you do not have a Lego Mini, they are about $100. What I did was buy my (grown-up) daughter one for Christmas. Clever, huh?
The only other component of consequence is an ESP8266-based NodeMCU. About $6 a throw. Then all you need are a bunch of 3mm LEDs, a few resistors (including an optional LDR/photoresistor), a couple of NPN transistors, thin hook-up wire, and heat-shrink tubing.
A soldering iron is the main thing here. Plus a drill with bits from 1/8in on down. You'll also need some smallish hand tools - I found a pair of pointy tweezers especially helpful.
Step 2: Design Considerations
Just like with a real Mini Cooper, you need to decide on options. In this case, how many individual lights do you want and do you want to add other things (a horn, for example). My advice is don't get too ambitious. First off, the NodeMCU has only 9 usable GPIO pins and one analog input pin. Even if you wanted to expand the pins with a shift register there is the consideration of physical space for the wiring and controlled accessories. Here's what I ended up with:
- Fog/Driving Lamps
- Right and left blinkers/hazards (front and rear)
- Three interior lights for RGB (could have used a combined one but didn't have any -- pin use is the same either way)
- Photoresistor on the analog pin for auto on/off functionality
I used transistors on the blinkers to save a couple of pins, an approach you could also use on the headlights and fog lights (anything that will always be switched together). You need to be careful though - I ran into a problem using transistors on some of the pins, which is discussed later.
- No alteration to the external appearance of the car
- As few internal modifications as possible
- No glue, no matter how tempting to add stability
- No hard coding of key configurables (e.g., wifi network)
- Reasonable-looking GUI for the "app"
In the end, I interpreted these rules more as guidelines but I don't think I made any flagrant deviations.
Step 3: Build Your Mini
Though devious, I am not without humanity. So in the spirit of Christmas I let my daughter build the Mini. Because, well, it was hers. This she did very ably. It was awesome.
I had played with Lego as a kid but only managed to make what could loosely be described as a "house". I don't think I had any window or door pieces so some imagination was required. This Lego Mini was in another league - capturing the very essence of the real thing (an example of which my daughter also happens to own) while preserving the integrity of Lego itself.
Despite the freezing cold, we immediately went on an extended photoshoot. It was more than awesome. It was perfect!
Except, nothing is perfect is it? Or at least, it is the human condition to view what was once seen as perfect as unsatisfactory. This is why we need landfills and divorce attorneys.
Clearly, what this car needed was LEDs. My daughter was only visiting for the holidays so I had to work fast. Version 1 of this project was just sticking some LEDs in the headlights and fog lights and wiring everything up to a couple of coin cell batteries through a switch. I say "just," but it was my first encounter with the fragility of the Mini (and I would guess most other custom Lego kits). Just picking the thing up risked bits falling off with a dispiriting clatter on the floor. And I would need to drill some tiny holes with a hulking great DeWalt, pull wire through spaces where wire was not designed to go and make the structure support a battery compartment on the underside.
Some of the liberated bits were quite large and it was obvious to me where they went back. Other pieces were a complete mystery. These I laid aside on a "too difficult" pile, which only a few minutes into the project had grown alarmingly. At some point, enough pieces had fallen off that the risk of the entire thing reverting to its original condition became more than theoretical. If you ever doubted that entropy defines the universe, look no further than Lego.
In a perverse biological reaction probably familiar to bomb disposal experts, the greater the risk of catastrophe the more my hands shook. If you are of a particularly nervous disposition, other biological reactions may be triggered. On Spotify, a T.Rex song played. The Mini as Nemesis. A line from Yeats' The Second Coming came to mind.
We shall pause here because I have an overdeveloped sense of the dramatic narrative.
Step 4: Version 1
Well reader, you will doubtless be relieved to hear I managed to get the battery-powered lights installed and the Mini pretty much restored to fully assembled condition. Just looking at this picture, though, you can see the tiles making up the roof are uneven. I guess they had fallen off so many times I couldn't be bothered to press them down again for the picture. Or possibly this was a consequence of some fortifying beverages imbibed to steady hand and heart. It was Christmas, after all.
Here matters remained. My daughter had to leave for Canada and took the Mini with her. That is how presents work, apparently.
Meanwhile, I had fallen hard for Lego. I started to watch Lego Batman on repeat, and read reviews of Lego kits. One day, I came across this from an outfit called Brick Loot, a commercial version of "my" idea. Even worse, it was better than my effort because it had more lights. More. Better. I tried to console myself that the blue lights were tawdry and it was twenty times more expensive than the $1.30 I had spent on Version 1. But my heart was having none of such normally appealing logic. This could not stand, especially when I found out there were tons of other options out there to add LED lighting to the car. Clearly I had to up my game. The Mini was recalled from the Frozen North and work commenced.
It so happened that I had recently embarked on another frivolous project which involved, inter alia, LEDs and a NodeMCU. It was not a giant leap of the imagination to marry this project with the Lego Mini. I could even re-use a good few chunks of code! Using a NodeMCU meant we could have phone-based control of the lights and as much automation as the imagination allowed. Take that, Brick Loot, whoever the heck you are.
Immediately, I suspended further Lego research. It's fine if someone else has already done something similar to this project (for they surely have). Just as long as I don't know about it (commenters, please respect this willful ignorance).
I now realize I said "Let's get started" four steps ago. So, let's get started. Really.
Step 5: Wiring
The first order of business was to decide how to get power to all the lights. Each of the sets of lights (headlights, fog lights, blinkers, interior lights) needed a slightly different approach.
I'll let the pictures do the talking for how I went about it. In a nutshell, wiring to all lights except the fog lights can be installed by drilling a small hole from the center of the light straight back into the next available cavity, then routing the wire inconspicuously to the underside of the Mini. For the fog lights, the wire must enter the rear of the reflector slightly off-center. The RGB interior LEDs (not pictured) are easy to install by drilling a hole straight up through the underside so it emerges between and slightly to the rear of the front seats. I used three separate lights because I didn't have any combined ones.
The only other thing worthy of mention is that in exploring options for the rear blinkers I discovered the spare tire, located under the floor of the trunk. This was completely unexpected - it would not be an exaggeration to say I was flabbergasted. Here was a component few, if any, would see after assembly. But all the more valuable for that. Well played Lego!
When you are done, all wires should meet in the large space between frame members under the Mini. This is where we shall install the NodeMCU and terminate the wiring on the appropriate pin.
I had earlier made a bad decision by electing to include the current-limiting resistors for the LEDs, transistors and LDR in the wiring runs. I did this because I wasn't planning to mount the NodeMCU on Perfboard. In the end that is exactly what I did so I could have easily consolidated all the electronics in one place. Not doing so will make replacement of components, especially the LEDs, slightly more challenging. Oh well.
Before fixing the NodeMCU/Perfboard to the frame, consider which side you want the micro USB to face.
Step 6: The Circuit
Nothing special here. The Fritzing sketch should be pretty self-explanatory, amateurish though it is. Current limiting resistors on all LEDs are 220Ω and on the transistors 1kΩ. The transistors are 2N2222 NPNs. The fixed resistor on the LDR is 10KΩ.
That said, now might be the place to mention a couple of quirks I discovered about the NodeMCU.
First, although there is some suggestion on the interwaves that pins D9 (RX) and D10 (TX) can be used as GPIOs if there is no serial traffic I'm not sure they can -- certainly didn't work for me.
Second, I ran into an issue with one of the transistors attached to D3. Not entirely sure why -- D3 (also D4 and D8) determine boot mode but not sure why that would matter. Just a heads-up so if you do run into a problem try moving stuff around on the pins.
I've made a Mental Note to look into both these issues further but for the moment all I can say is the pin allocation shown on my sketch worked for me.
Step 7: The Code
As always, I am deeply indebted to the selfless individuals with real expertise who contribute libraries, tutorials and other resources to be used and abused by hapless individuals such as myself. For this project in particular I relied heavily on a very comprehensive introduction to all things ESP8266 available here. The WiFi Manager library also deserves a special shout-out for facilitating network portability, highly desirable given that the Lego Mini travels back and forth from Canada like a real car.
Finally, a big thank you to my daughter Emma, for letting me play with her car to an unreasonable degree.