Introduction: DIY IoT Lamp for Home Automation || ESP8266 Tutorial
In this tutorial we're going to make an internet connected smart lamp. This will go deep into internet of things and opens up a world of home automation!
The lamp is WiFi connected and built to have an open message protocol. This means you can select whatever control mode you want! It can be controlled through a web browser, home automation apps, smart assistants like Alexa or Google Assistant, and so much more!
As a bonus this lamp goes along with an app to control the project. Here you can select different color modes, fade between RGB colors, and set timers.
The lamp consits of an LED board and a control board. The LED board uses three different types of LEDs for a total of five LED channels! This is RGB along with both warm and cold white. Because all of these channels can be set individually, you have a total of 112.3 peta combinations!
Let's get started!
Step 1: Parts and Tools
- Wemos D1 Mini
- 15 x Warm white 5050 LEDs
- 15 x Cold white 5050 LEDs
- 18 x RGB 5050 LEDs
- 6 x 300 ohm 1206 resistors
- 42 x 150 ohm 1206 resistors
- 5 x 1k ohm resistors
- 5 x NTR4501NT1G
- Linear voltage regulator, 5V
- Download the gerber files in the circuit step to make your own PCBs
- PSU 12V 2A
- Soldering iron
- Soldering tin
- Liquid soldering flux
Step 2: The Plan
The complete project consists of four main parts:
- The circuit is made on a PCB. The completed circuit will consists of more than 100 individual components. It's a huge relief not to wire all those by hand on a perfboard!
- Arduino Code
- I'm using the Wemos D1 Mini which uses an ESP8266 as a WiFi connected microcontroller. The code will start a server on the D1. When you visit the address of this server the D1 will interpret this as different commands. The microcontroller then acts on this command to set the lights accordingly
- Remote Control
I made an app just for this project to make it as easy as possible to control the lamp to your liking
The smart lamp can really be controlled by anything capable of sending an http GET request. This means the lamp accepts commands from nearly a limitless array of devices
- 3D Printing
- This smart lamp deserves a cool looking case. And like so many projects were you need a cool case, 3D printing comes to the rescue!
Step 3: Circuit
I ordered my PCBs from jlcpcb.com. Full disclosure time: they also sponsored this project.
The PCB consists of two parts. It has the LED board and the control board. The PCB can be snapped apart to later connect these two parts by flexible wire. This is necessary to both keep the 3D printed lamp slim, and to angle the LED board to spread the light evenly through the hole room.
The control board houses the D1 microcontroller along with five MOSFETs for dimming the LEDs, and a voltage regulator to give the microcontroller a smooth 5V.
The LED board has five LED channels in three differetn types of LEDs. Because we use a 12V power source the LEDs are configured as three LEDs in series with a resistor and then repeated 16 times in parallel.
A regular white LED usually draws 3.3 V. On a segment of the board, three of these LEDs are in series which means the voltage drop is aggregated in the circuit. Three LEDs that draws 3.3 V each means one segment of LEDs draws 9.9 V. The circuit is powered by 12 V so that leaves 2.1 V.
If the segment only consisted of the three LEDs they would get more voltage than they dissipate. This is not good for the LEDs and can quickly damage them. This is why each segment also has a resistor in series with all three LEDs. This resistor is there to drop the remaining 2.1 V in the series junction.
So if each segment accounts for 12 V that means each of the segments are connected to each other in parallel. When circuits are connected in parallel they all get the same voltage and the current is aggregated. The current in a series connection is always the same.
A regular LED draws 20 mA in current. This means a segment, which is three LEDs and a resistor in series still will draw 20 mA. When we connect several segments in parallel, we add the current. If you cut six LEDs from the strip, you have two of these segments in parallel. Which means your total circuit still draws 12 V, but they draw 40 mA in current.
Step 4: Soldering LEDs
From trying a few things I've found simple masking tape is just the most effective and flexible for keeping the PCB from moving around.
For parts with multiple pins, like the 6-pins on a 5050 LED, i start by placing down solder on one of the PCB pads. Then it's just a matter of keeping this solder molten with the soldering iron while sliding the component into its place with a pair of tweezers.
Now the other pads can be easily tacked on with some solder. However, to speed up this work I suggest picking up some liquid solder flux. I really cant recommend this stuff enough.
Apply some of the flux to the solder pads, then melt some solder on the tip of your soldering iron. Now it's just a matter of bringing the molten solder onto the pads and everything flows into place. Nice and simple.
When it comes to the resistors and other two-pad components no solder flux is really needed. Apply solder to one of the pads and bring the resistor into place. Now just melt some solder onto pad number two. Easy peasy.
Take a look at the fifth picture in this step. Pay attention to the orientation of the LEDs. The warm and cold white LEDs have their notch oriented in the upper right corner. The RGB LEDs has their notch in the bottom left corner. This is a design error from my part, because I couldn't find the datasheet for the RGB LEDs used in this project. Oh well, live and learn and all that!
Step 5: Soldering Control Board
After finishing the marathon of the LED board, the control board is a breeze to solder. I placed down the five MOSFETs and matching gate-source resistors, before moving onto the voltage regulator.
The voltage regulator has optional spaces for smoothing capacitors. While I soldered them in this picture I ended up removing them because they were not really necessary.
The trick to getting a slim control board is to put the pin headers poking out the top through the bottom. After the pins are in place, the unused length can be snipped from the backside along with the black plastic. This makes the bottom side completely smooth.
With all the components in place it's time to bring the two boards together. I just snipped and stripped six small 2.5 inch (7 cm) wires and connected the two PCBs.
Step 6: WiFi Setup
There are six simple lines in the code you need to change.
- ssid, line 3
- Your router name. Make sure you get the letter case correct when writing this
- wifiPass, line 4
- Your router password. Again, pay attention to casing!
- ip, line 8
- The static ip address of your smart lamp. I chose a random ip address on my network and tried to ping it in the command window. If there's no reply from the address you can assume its available
- gateway, line 9
- This will be the gateway on your router. Open a command window and type "ipconfig". The gateway and subnet are circled in red in the picture
- subnet, line 10
- As with the gateway, this information is circled in the picture for this step
- timeZone, line 15
- The timezone you're in. Change this if you want to use the built in timer functions to turn on and off lights at specific times. The variable is a simple pluss or minus GMT
Step 7: Microcontroller Code
After changing all relevant settings in the previous step it's finally time to upload the code to the Wemos D1 Mini!
The arduino code requires a few libraries and dependencies. First follow this guide from sparkfun if you have never uploaded code from arduino IDE to an ESP8266.
Now download the Time library and the TimeAlarms library. Unzip these and copy to the arduino library folder on your computer. Just like installing any other arduino libraries.
Pay attention to the upload settings in the picture on this step. Select the same configuration, except the com port. This will be whatever com port you have your microcontroller connected to on your computer.
When the code is uploaded open the serial terminal to a message of a, hopefully, successful connection! You can now open your browser and visit the static ip address you saved to the microcontroller. Congratulations, you've just built your own server and is hosting a web page on it!
Step 8: Open Message Protocol
When you control the smart lamp with the app all the messages will be handled for you automatically. Here is a list of the messages the lamp accepts, if you want to build your own remote control. I've used an example ip address to illustrate how to use the commands.
- Sets Red lights to max value, green lights to half value, and blue lights to 34. Cold and warm white are barely on
- When entering values, you may choose between 0 and 1023. Always write the light values as four digits in the URL
- Sets blue lights to the value 800 while simultaneously turning off all the other lights
- Turns all lights completely off
- Starts slowly fading between all possible RGB colors. Perfect for ambience!
- Flashes the given color two times to indicate incoming notification. Perfect if you want to, say, create a program on your computer to flash the lamp red whenever you receive a new email
- Adjusts clock to daylight savings time. Adds one hour to the clock
- /DST=0 use this to go back from DST, removes one hour from the clock if DST is active
- Saves the state for timer 1. This timer will turn on the given RGB values at 06:30 in the morning
- Set the timer hour to 99 to deactivate the timer. The RGB values are still stored, but the timer will not turn on the lights when the hour is set to 99
These are the currently built in commands. Leave a comment if you have any cool ideas for new commands to build either in the arduino code or the remote app!
Step 9: Remote Control
Click here to download the app. The setup is made super ease, just enter the ip address of your smart lamp and select whether you want to control just RGB LEDs or RGB + warm and cold white LEDs.
As explained in the previous step, you now know what message protocol the app is using. It's sending an http GET request with the URLs. This means you can also create your own microcontroller circuit, and still use this app to control the functions you develop on your own.
Because we've really looked deep into the message protocol you can also control the smart lamp by anything capable of sending an http GET request. This means any browser on a phone or computer, or smart home devices or assistants like Alexa or the Google Assistant.
Tasker is an app that basically lets you create conditions to control close to anything. I used that to flash the smart lamp with the color of a notification when I receive it on my phone. I also set up tasker to turn on the lights at full white, when the phone connects to my home WiFi after 16:00 on a weekday. That means the lights turn on automatically when I get home from school. It's really cool to come home with lights automatically on!
Step 10: 3D Printing
The lamp case itself can be printed almost completely without supports. The only parts that really need support are the pegs meant for mating with the PCB. Therefor I made the stl available both with and without a tiny support structure for just these pegs. The advantage of using this custom support is that the print is much faster! And we only get printing support on the parts that really need it.
You can download the .stl files here
Step 11: Bring It All Together
After 3D printing start by removing the printing support. The power cables goes into separate channels and are tied together. This knot will create strain relief preventing the cables from being ripped of the PCB. Solder the power cables onto the backside of the PCB and make sure you get the polarity right!
The control PCB is then fastened with a piece of tape to keep it flush inside the case. The LED PCB can be simply put into its place where it lays flat against the case on its own.
Step 12: Hanging the Lamp
There's a lot of options for hanging this lamp to the wall. Because I might continuously update the code to improve the lamp I wanted a way of taking the lamp down from time to time. You can use hot glue, but I recommend some double sided tape. It's best to use the thick and foamy double sided tape as it holds the lamp the best against a textured wall.
Step 13: Finished
With the lamp up on the wall and ready to accept commands that means you're done!
The LED panel is angled in a way that disperses the light evenly in the room. It's a nice addition to any work space and the ability for integration with home automation is a great plus. I really like the ability to set RGB colors as well as adjust white balances between cold and warm light. It looks stylish and is a great help for setting ambient or work lights, to suit whatever lighting needs I have at the moment.
Congratulations, you've now taken a big leap into the world of IoT and home automation!
Participated in the
2 years ago
Sir i want to buy this product
2 years ago
Hey, I want to ask something, can I use portable Wi-fi modem instead of router for the network connection?
5 years ago
This looks really nice. Thanks for posting clear and detailed instructions.
Reply 5 years ago
5 years ago
LEDs are inverted on the schematics.
Reply 5 years ago
You're right! I updated the schematic now.