DIY Motorized Roller Shades for your smart home with WiFi, MQTT, and Voice Control

August 8, 2018

Smart Shades

“Alexa, turn off the chair shade”

Today on the hookup we’re going to make a motorized smart shade that can be controlled with amazon echo and your MQTT smart home controller for around $125.

Lets start out by taking a look at each of the products we’re going to use for this project.

I used this shade from amazon.com, it’s available in 4 by 6, 6 by 6, 8 by 6, and 10 by 6.  I specifically used the 6 by 6 version, but my setup would work fine for the 4 by 6 and the 8 by 6 also.  The 10 by 6 version might be a bit much for a single motor to handle, so I’d steer clear of that.

Roller Shade: https://amzn.to/2nkbQqI

I like these shades because they significantly reduce the amount of sunlight passing through without completely blocking the view of the pool, they let a decent amount of breeze through, and they are very weather resistant.  They fit my needs perfectly, but if you’re looking for a blackout shade, these aren’t going to cut it.

Next we need a motor to move these things.  I opted for a planetary geared stepper motor with a 5:1 gear ratio.  Stepper online makes super high quality stepper motors for relatively cheap.  When I made my original project I purchased them on amazon for around $45 each, but they were nice enough to send me one to make this video, so thanks stepper online.

Geared Stepper Motor (stepper online): https://goo.gl/DW3Bgw

Geared Stepper Motor (amazon): https://amzn.to/2nkomGN

Lets talk briefly about the important differences between a stepper motor and a normal DC motor.  When you apply current to a DC motor it spins in one direction, raise the voltage and it spins faster, flip the polarity and it spins in the opposite direction.  Those are all useful things, but one thing you can’t do with a DC motor is move it to a specific location and lock it in that location.  Here’s where a stepper motor shines.  A stepper motor uses 4 wires instead of 2.  Applying current to two of those wires will advance the motor exactly 1 step.  If I want it to go the next step I’ll need apply current to the other two wires.  The motor we selected has a step angle of .35 degrees, which means every step rotates the shaft by only .35 degrees.  This is really useful information when we consider that this will allow us to know the exact amount that our shades have been rolled down at any given time.  For this specific case we know that we’ll need to do 1028 steps in order to move 360 degrees without using microstepping.

The decision to use a planetary gears is just as important as the decision to use a stepper motor.  Planetary gears allow us to amplify the torque of the stepper motor by a factor of 5 in this case, and that’s important because it means that a less torquey stepper motor will still be able to supply the needed rotational force to roll the shades up and down, but the other reason it is important is that it also makes it 5 times more difficult for the weight of the shades to rotate the shaft of the stepper which will prevent slippage between steps.  When I made my shades over a year ago I tried every non-geared high torque stepper motor available on amazon, but none of them worked exactly how I wanted them to.  Another good option for this project would have been to use a DC motor with a motor encoder, but I like stepper motors, so that’s what I used.

Since a stepper motor needs to have a relatively large current pulsed rapidly, you’ll also need a stepper motor driver to handle the heavy lifting.  I really like the quality of the products from stepper online so I asked them to send me one of their drivers.  Unfortunately, I didn’t do enough research before requesting the driver from them, and the one I got wasn’t compatible with the 3.3V logic that the ESP8266 outputs.  I went ahead and bought the same driver that I used for my first two shades, it works flawlessly and it’s one of the cheapest ones on amazon.

Stepper Driver: https://amzn.to/2AReI85

For a microcontroller I used an ESP8266 based node-mcu because I’m very comfortable with them.  If you’re going to be placing these outside far away from your wireless router I’d recommend checking out the wemos D1 pro since it has an external wifi antenna.  My Arduino sketch will work on the nodeMCU and the wemos D1 pro without modification, so go with whatever one you’re most comfortable with.

D1 Mini Pro: https://amzn.to/2Msm17L
External WiFi Antenna: https://amzn.to/2MsmfvD

We’ll also need a way to power our microcontroller.  Since we will already have a 12V power source for our stepper motors a buck converter is all we need to step that down to 5V.  If you’d rather not mess with a buck converter you can also just power it with a USB cord and wall charger.

5V Power Supply: https://amzn.to/2MsSWcr

Buck Converter: https://amzn.to/2MsPiPu

Those are the main parts that I needed to buy, but lets quickly talk about everything that I already had around from previous projects that I used in this project:  You’ll need wire, I used cat6 cable because I had it around. it has 4 twisted pairs, and I used one twisted pair for each of the 4 wires on the stepper motor.  If I was going to buy wire specifically for this problem I’d get one of these 250ft spools of four core 18 gauge wire, for $30, it’s an absolute steal, that’s like 12 cents a foot.

4 Core 18AWG Wire: https://amzn.to/2OjVmdA

Jumper Wires: https://amzn.to/2OQzzeE

You’ll also need some wire to hook up the stepper motor driver, male to female jumper wires are perfect for this, and it means you won’t have to solder anything.  You’ll need an AC adapter, I used an old one 12 volt one from an HP printer, you want between 12 and 19V, more volts = more torque, 2 amps should be plenty.  You’ll also need some jumper wires, and some M3 screws.  Of course, all of the specific parts I used are linked in the description.

M3 Screws: https://amzn.to/2KCWuXT

I also used a 3D printer to make the connector hub between the the stepper motor and the shade.  If you don’t have a 3d printer there are probably lots of different places around you to access one.  Do a google search for a maker space in your local area and you may be surprised how many there are.  Sometimes you can even 3d print at your local library.  I’d recommend printing this part with at least 80% infill and 3 to 4 shells.  If you use less than that the hub may strip itself out.

I might regret doing this, because I’m really not interested in getting into the 3d printing business, but as a last resort if you can’t find anywhere else, I’m going to add a $10 tier to my patreon page called “3d printer” and I’ll print out the part for this project and send it to you as your reward for that tier.

3d Printed Hub STL File: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/ShadeHub.stl

Patreon: https://www.patreon.com/thehookup

So if you needed to buy absolutely everything that I just mentioned this project is going to run you around $150, I built mine for around $175 for the pair, but I only had to buy the motors, drivers, and shades, and I had all the rest of the parts laying around as leftovers from previous projects.

 

Lets build it!

We’ll start out by making some modifications to our shades.  Start by removing the mounting plug from the end of the rod, we’re going to replace this with our 3d printed part that has the mounting hub for the stepper motor.  Next you’ll need to modify the end that comes with the pull chain.  Start by removing the screw from the front and pull off the rod mount.  Inside there’s a clutch to stop the shade from moving when the chain isn’t under tension.  You can remove the clutch and gear set completely.  When you replace the rod mount onto the shaft just offset the notches so they aren’t in the middle of the little prongs on the spring.  You can remove sets of these springs if you need to in order to change the amount of resistance to rolling.  If your stepper motor doesn’t seem powerful enough to unroll your shade you can remove a few springs, if your shade is unrolling by itself add them back.

Next we need to modify our mounting brackets to accommodate our stepper motor.  This part was actually super hard for me to get the holes right.  Luckily I’ve done all the hard work for you and created a PDF file with a template for where to drill the holes.  Make sure when you print out the PDF file you have “actual size” checked and not fit to page, or shrink to fit, or any of those other options. The bracket we’re going to modify is the one without the tab, remove the 8 screws on the back of the bracket and tape the template onto the face. Drill out the mounting holes and attach your stepper motor with the M3 screws

Mounting Holes Template: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/mounting_template.pdf

Next we’ll move on to the code.  My code is fairly simple and utilizes the AH_easydriver library for stepper motors.  The code is based off the fact that these shades will go from completely rolled to completely unrolled in 13 full rotations.   If you’re using a different shade you’ll want to change the unrolled variable to something other than 13.  If you’re going the Arduino route you’ll also need to edit the user configuration section with your wifi and mqtt information.

Arduino Code: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/ShadeMCU_CONFIGURE.ino

If you’re against using the Arduino IDE and you’d rather use the flashesp8266 tool that DrZzs lovingly refers to as esp easy, I’ve also uploaded a bin file with a configuration portal so you can avoid having to install different libraries.  After flashing the bin file to your microcontroller you’ll notice a new wireless network pop up called “ShadeConfig”, connect to it and navigate to 192.168.4.1.  Put in your wireless SSID, password, and mqtt information.  The MQTT client ID will be used to create unique mqtt topics for each of your shades, so make sure you remember it and make sure it doesn’t have any spaces in it.  I’ve also added a number of rotations input in case you want to use this with another shade that requires a different number of rotations to fully unroll.  If you’re using the same shade as me, leave this number at 13.

ESPFlashEZ Program: https://github.com/letscontrolit/ESPEasy/releases
ESPEZ Shade Bin File: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/Shades_Portal_Config.ino.nodemcu.bin

You’ll need to set up a way to control the shades by sending them MQTT values.  I personally use home assistant, but most of the popular home automation platforms should be able to handle the task.  I’ve configured mine as an input slider, with an an automation to send the value we select to the MQTT topic specified in the code.  That topic is shadePosition/ and the client id you selected.  It’s very important that you use a retain flag here, because our MQTT server is going to store our shade’s position for us in the event of a reboot.

Configuration YAML Additions: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/shade.yaml

Once you’ve got your code loaded up and your MQTT messages configured you can test it with your stepper motor and driver.

This driver has 3 pins that need to be connected to your microcontroller:  The DIR positive input on the driver connects to GPIO4, which is marked as D2 on the node-mcu, The PUL positive input on the driver connects to GPIO 0 marked as D3 on the node-mcu, and the ENA positive input connects to GPIO13 which is D7.  You’ll also need to connect each of the negative inputs to your board.  I connected them with a single wire to my nodeMCU and then just bridged the connections together with small pieces of wire.

Next you’ll attach the 4 wires of your stepper motor to the driver.  The black wire connects to A positive, Green to A negative, Red to B positive and Blue to B negative and attach your 12V power supply to Vdc and Ground.

Remember if you’re using a buck converter you’ll need to attach it to your input voltage and then turn the set screw on the top of the board until the output voltage is correct.  I try to shoot for around 4.8 volts on the nodeMCU.

Before we plug it in, lets double check that our dip switches are in the right place.  Our stepper motor is rated for 1.68 amps, so we’re going to set the current limiting driver to 1.34 amps RMS just to make sure we don’t overload our motor.  We also want to set the resolution to 800 pulses per revolution.

This means our dip switch setup is going to on off off, and on on off.

Alright! Lets test it out.  To test it I’d recommend turning the shaft of the motor manually before you plug it in to line it up with one of the screws we want to make sure that our motor moves in full rotations, so send your motor a command to move 13 full rotations and if it rotates 13 times and ends up in roughly the same position that it started you’re ready to install.

Mount the brackets loosely on either side of the shade and slide the mounting hub onto the stepper motor shaft.  Once everything is in place, slide the brackets closer together to make everything fit nicely, and tighten the mounting bracket screws.

Before you plug in your microcontroller and driver you want to make sure that you’ve sent the message “13” to the mqtt topic with a retain flag.  With home assistant all I needed to do was slide my slider all the way to the end.  You also want to start with your shade completely unrolled, not only because it’s much easier to mount it this way, but also because that corresponds to position 13.  Now it’s time to plug it in and test it out.  The sleep function on a stepper driver turns off all power to the motor at specific positions, not only does this save power, but it puts less stress on the driver and motor.  The Arduino program that I wrote is set to sleep at position 0, which is fully rolled up, and position 13 which is the fully rolled down position.  If you set a different value in the configuration section, it will sleep at whatever maximum value you set.  What this means for you, is that if the values ever get uncalibrated, all you need to do is send an MQTT value of 13, or whatever your max is, and then after the shade is done moving, manually pull it down to it’s maximum value… but this shouldn’t really be a problem unless your kid decides to hang on the shade while it’s trying to move.

If everything went according to plan you’re done and you should be able to control your shades with home assistant!  Test it out by sending different values to the shades.  The great part about this code is it won’t get confused if you send it a new value before the old one has finished processing.  If you find that your shades are rolling up the wrong way, you can just reverse the + and – wires for each of the A and B motor pairs on your driver and the motor will spin in the opposite direction.

I also have my shades set up to work with my amazon echo using the alexa local node.  I’m going to post the node-red flow that I use to control my shades in the description, but I also covered it in depth in my 2nd node red how to video.  You can click this link to go there now.

Patreon: https://www.patreon.com/thehookup

Thanks to stepper online I now have an extra stepper motor and I bought some other stuff just to make this video.  Sounds like a great time to do my first give away!  Since I’ve just started this patreon thing, in 2 weeks I’m going to randomly select 1 one of my patrons and send them a pre-programmed node-mcu, a stepper driver, the geared stepper motor, and the 3d printed hub.  All patrons who have joined as of August 22nd will be entered into the drawing. If I were Sean I’d be feeling pretty good about my odds right about now.

If you get stuck anywhere while building this project, post down in the comments… I’m pretty good about responding quickly most of the time.  Links to the exact parts that I used are in the description, so if you stick with those you shouldn’t run into any problems.  I know this video has been a long time coming, hopefully it has lived up to your expectations.  If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.

=====TheHookUp=====

Website: http://www.thesmarthomehookup.com
Twitter: @TheHookUp1
Patreon: https://www.patreon.com/thehookup

=====PARTS=====

Roller Shade: https://amzn.to/2nkbQqI

Geared Stepper Motor (amazon): https://amzn.to/2nkomGN
OR
Geared Stepper Motor (stepper online): https://goo.gl/DW3Bgw

Stepper Driver: https://amzn.to/2AReI85

ESP8266 NodeMCU: https://amzn.to/2nkeahq
OR
D1 Mini Pro: https://amzn.to/2Msm17L
External WiFi Antenna: https://amzn.to/2MsmfvD

Buck Converter: https://amzn.to/2MsPiPu
OR
5V Power Supply: https://amzn.to/2MsSWcr

4 Core 18AWG Wire: https://amzn.to/2OjVmdA

Jumper Wires: https://amzn.to/2OQzzeE

12V Power Supply: https://amzn.to/2APkGGy

M3 Screws: https://amzn.to/2KCWuXT

=====FILES=====

3d Printed Hub STL File: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/ShadeHub.stl

Mounting Holes Template: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/mounting_template.pdf

Arduino Code: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/ShadeMCU_CONFIGURE.ino

ESPFlashEZ Program: https://github.com/letscontrolit/ESPEasy/releases
ESPEZ Shade Bin File: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/Shades_Portal_Config.ino.nodemcu.bin

Configuration YAML Additions: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/shade.yaml

Wiring Schematic: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/schematic.jpg

Node-RED Alexa Sequence: https://github.com/thehookup/MQTT_Motorized_Shades/blob/master/Node_Red_Alexa.js

Credits

Music by www.BenSound.com

Related Posts