(Disclaimer: I'm not a native english speaker.)
A while ago, my wife bought some LED string lights to light up the garden at night. They created a very nice atmosphere. They were put around the trees, but guess what, what should happen happened, we cut the strings while cutting the trees...
What I want to show you today is how to salvage broken stuff like those LED strings and create interesting connected devices that you can control with your smartphone.
You will learn how to use a microcontroller and a transistor to drive LEDs, how to connect your device to Internet, and how to control the device from your smartphone. I just assume that you have some basic electronics knowledge like how to apply the Ohm's Law. If you ever programmed an Arduino before it is even better.
Let's start with the devices I want to build. The good thing about cut strings is that there are at least two pieces. Thus I can build at least two devices. I will start with a connected lamp that I'll put on a table and then a connected LED string that I will use to light my new bedroom. All I want is a way to switch the lights ON and OFF using my smartphone.
But first things first, we need to see how things worked to reuse the lights.
Step 1: Reverse Engineering
We have two LED strings but we don't know the voltage drop across the strings pins and the current they require. Sadly, I don't have a datasheet to get those values.
In those cases, we will need to figure out everything by ourselves. Let's take apart the enclosure.
After removing some screws with a screwdriver, we can see a very simple circuit. The interesting part is around the LED string pins, we see a voltage regulator (3 pins component), a resistor (the black box with 100 on it), and the LED string pins. Looking a little bit closer (circuit design), we see that the regulator output is connected to the LED string which is in turn connected to the ground through a 10 ohm resistor (100 means 10x10e0). Let's put some batteries and measure the voltage drop across the string pins and between the regulator output and the ground.
Using a multimeter, we can measure a voltage drop of around 3V across the string pins (at shown in the pictures). We also measure 4.5V between the regulator output and the ground. We thus deduce that there is a voltage drop of 1.5V across the 10 ohm resistor; we can actually measure it too. Using Ohm's Law (U = RI), we know that the current through the branch is 1.5V / 10 ohm = 0.150A or 150mA. Again we can measure the current but we would need to put the multimeter in series with the string which is not easy to do.
We now know how to drive the LED strings. Let's build our device.
Step 2: Materials & Tools
Here is what you will need to build the devices:
- some screwdrivers to teardown stuff, I like that kind of kit
- some LED string lights, if you want to reproduce the devices
- an ESP8266, it will be the brain of our device
- a breadboard and some wires, we will use them to build the prototype
If you want to create a permanent circuit, you will need some tools and some protoboards:
- you can buy a soldering kit quite cheaply to get started, you will find a multi-meter that can be used to reverse engineer your own stuff, just beware to not manipulate devices connected to the main or even devices using more than 30V DC
- a cutter is very useful to cut wires and component leads
- some solid wire
It may seems a lot to get started but you will build some stock for any other project you might have. If you don't mind waiting, you can order everything on Aliexpress at a much lower cost. As an alternative, if you don't want to buy those tools, you can also go to the closest hackerspace.
Finally, you will need a few hours to build everything (less if you just follow this tutorial).
Step 3: How to Use a Transistor
We know that the LED string require 150mA but it is way more than what the ESP8266 can safely deliver on its output pins. You don't want to drive more than 12mA per GPIO pins on the microcontroller. To go around this limitation, will need some kind of switch that can be controlled by the microcontroller. The most common switches are the relay and the transistor. A relay will certainly work but will be bulkier, more expensive, and most of the time you will want to use a transistor to drive a relay.
We will use transistors for both devices. To use a transistor like a switch, we must drive current through its base. The current that flows through the LED string will be proportional to the current that flows through the base.
You can play with an Arduino and a transistor on Tinkercad to get a sense of how things work. I created a basic simulation that you can tweak. If you want to learn more about Tinkercad, you can follow this amazing tutorial: How to Use Tinkercad To Test & Implement Your Hardware.
You can see that the transistor works like a closed switch when the GPIO output is high and like an open switch when the GPIO output is low. You can also play with the resistors values. The resistor in series with the LED will limit the current flow through the LED and the resistor connected to the base of the transistor will control the maximum current flowing through the LED. If you increase the base resistor you won't drive enough current for the LED and the light will be dimmer.
You can take a look at my notes to see what resistor values I choose for the devices. I could have used the 3.3V output instead of the 5V output but then I wouldn't have the corresponding resistors to build the circuit. Don't hesitate to read the transistor datasheet to look for the transistor gain.
Let's now build a prototype.
Step 4: Build a Prototype of the Circuit
We will need to prepare the LED string wire. First let's cut the first half to separate the batteries holder. Then, strip the wire, I used a terminal block to connect the LED string to the breadboard. We will also need the ESP8266, I used a D1 mini clone, two resistors, and a transistor.
I choose a p2222a for the transistor but you can choose any NPN transistor. You will just need to review the resistors values according to the transistor gain that you can find on the transistor datasheet. I choose a base resistor of 1k ohm and a LED resistor of 15 ohm. The base is driven by the GPIO5 or D1.
Keep the batteries holder as it can be useful for another project or even to power your newly created devices.
Follow a tutorial on how to upload a program on the ESP8266 with the Arduino IDE, upload the blink program replacing the LED_BUILTIN by D1, and you can now enjoy a blinking LED string.
If the circuit is not working for you, try to swap the LED wires as you need to connect the anode to the LED resistor. I always inverse the wires...
Use you multimeter to check connection and voltage drop. You should see 3.3V between D1 and the ground when the output is high. You should also see a voltage of 3V between the LED string wires.
Having a blinking LED string is good but how can we control the LED string with our smartphone?
Step 5: Using Your Smartphone to Drive the LED String Lights - Part I
You will need to install the Blynk app on your smartphone.
Once the app is installed, create a new project. Blynk will send you an email with a token (series of hex chars) that you will need for your ESP8266 program. Create a button that will act as a switch. The button should drive the GPIO5 or D1 pin of the ESP8266. You can now play your project. Note that the app will tell you that the device is offline.
You can edit the project later to add timers that will control the lights.
Step 6: Using Your Smartphone to Drive the LED String Lights - Part II
Open your Arduino IDE. You will need to install the Blynk library; for that, just follow the screenshots I made. Go to the "Tools" menu, click on "Manage Libraries", search for "Blynk", and install the latest version.
You can now open an example that will setup Blynk on the ESP8266 for you. The example is shown on the screenshots.
Ensure you selected the correct board, "D1 mini" in my case, and the correct port.
Update the code with your wifi SSID and password (usually the WPA or WEP key on the Internet box), you will also need to fill the token you received by email.
You can now upload the code to the ESP8266. Once the code is uploaded, wait a few seconds to ensure your device is connected in WiFi to your Internet router and you will be able to control the lights using the Blynk button you created.
You now have an IoT device! You can stop there if you want but don't forget to read the "Resources" section. If you want to have more fun and build a permanent circuit and an enclosure, continue reading.
Step 7: Create a Permanent Circuit (bonus)
It is time to create a permanent circuit. You can watch this and this video to learn about soldering. I used a standard proto board with some header for the ESP8266. That way if I want to reuse the microcontroller for another project, I can. You can choose to solder the microcontroller directly to your proto board. If you are not confident choose a proto board that looks like a breadboard; you will be able to reuse your breadboard connections.
I did two mistakes with my first device. I didn't use the terminal block for the LED string... and I inverted the wires. You can mark the negative or positive wire but using a terminal block is recommended. The second mistake is that I used the 3.3V to drive the LED string resulting in a dimmer light. If, like me, you make mistakes, don't worry, it is easy to remove solder and change resistors values or update the connections. You can even add more components later!
Now that you have you permanent circuit, it's time to build its enclosure.
Step 8: Build an Enclosure (bonus)
I followed a sparkfun tutorial on Tinkercad to build an enclosure for my devices. I printed the enclosure using my newly acquired Prusa i3 MK3 with some PLA filament (20% infill and 0.2 mm). It is actually a first for me and I already made two mistakes that you can see on the pictures. My first enclosure didn't have the required space for the USB plug and the holes were not aligned. I then designed a new version with a better fit that can also support a lid. You can save some time and some money only printing the required part of the enclosure to test the fit with the circuit.
You now have two IoT devices that you can control using Blynk. Sky's the limit. You can totally extend the project with a presence detector that control the lights, with a timer that switch off the lights after a certain amount of time, or even using the LED string lights as a notification system; they could blink when you receive an email for example.
Step 9: Resources
I cannot recommend enough this book: Make: Electronics: Learning Through Discovery. You can learn about transistors, capacitors, and plenty other interesting stuff about electronics. It has the required knowledge to start tinkering with electronics components. Coupled with your just acquired knowledge about the ESP8266, Blynk, and Tinkerpad, you will be able to build very interesting stuff.
You can learn a lot watching Youtube videos. I recommend the following channels:
I you are brave enough, you can acquire more knowledge following edx or coursera courses about IoT or electronics.