An ESP32-based remote controlled servo motor to open and close plantation shutters
I have a two storey house with an open entry void. A few years ago, my wife and I decided to replace the horrible curtains, we had over our entry arched windows, with some Plantation Shutters.
Once installed, they looked pretty good and we were very pleased. When we asked where the mechanical mechanism was for opening and closing the shutters was, we were given a long pole with a hook on the end. This was not the mechanism I thought we were getting.
When I retired, I thought this would be a good opportunity to do something that would make opening and closing the Plantation Shutters much easier. Automating the whole process would even be much better. I first looked around to see if there were any “off the shelf” solutions. Surely, many people would be looking for an aftermarket product that would at least allow remote operation of Plantation Shutters.
The only thing I could find, at the time, was a Kick Starter project which, for some reason, never came to fruition. I have subsequently found devices on AliExpress and Amazon for between $100 to $200. However, this has only happened recently. I started this project almost three years ago, when there weren't really any products around suitable for my Plantation Shutters.
I have developed many versions of my Plantation Shutter Controller (PSC), but they all had the same few basic requirements.
- The shutters shall be able to open to an angle of 90 degrees (greater if possible);
- The opening and closing shall be automatic using exterior light levels to determine when the shutter opens (Maximum Light Level) and when the shutter closes (Minimum Light Level);
- If the exterior temperature goes above a certain temperature (Maximum Temperature), the shutter shall close and not open again until the temperature goes below the Maximum Temperature;
- The PSC shall be powered from batteries.
The above requirements were my basic requirements and several other “features” were added as I went through the various revisions of my design. The requirement for operating from batteries came about as I could not find an easy way of getting AC power to my shutters. An electrician would cost several hundred dollars to put in the required cabling, so batteries it was. This, of course, created several problems.
But, let me address each requirement in order. Opening and closing the shutters was the most fundamental requirement. Now at the initial start of my project, I had been experimenting with Arduino processors and the various sensors that could be interfaced to an Arduino processor.
I had been trying out Servo Motors and decided a Servo Motor may be a good option for opening and closing my Plantation Shutters. The main reason was that a Servo Motor could rotate its shaft from 0 to up to 360 degrees. I settled on the MG996R from Tower Pro, which, depending on which literature you read, rotates its shaft up to 120 or 180 degrees. I only needed 90 degrees. Also, the MG996R had a stall torque of 10 kg. I checked my shutters and they did not require anywhere near 10 kg to move them. As it turned out, the MG996R would open and close three banks of shutters linked together. This may not be the case with other brands of shutters.
The next requirement to address was the detection of Light Levels. I looked at various Light Sensors and settled on the BH1750FVI. The initial reason I liked this module was that it came in a variant contained within a plastic dome. I thought this was ideal as all I had to do was cut a hole in the lid of the case I used initially and point it at the window and read the light levels. This did work quite well. However, when I wanted more of the dome type sensors, I found they had become more expensive and harder to acquire.
Also, I found the cable frequently broke from the BH1750FVI PCB. So, I decided to use the standard module and be a little clever on how I install it, especially as, in my final version, I have 3D printed a case.
When it came to selecting the Temperature Sensor, I originally selected the DHT11. This was in my early days of design and I had been using the DHT11 in a few projects and it seemed to work well. However, as time went by, I found a better Temperature Sensor. The Dallas Semiconductor DS18B20, which has now become one of my favourite Temperature Sensors. It is much smaller than the DHT11, it is faster and uses less current (750nA Standby), which is important in battery powered projects.
The Battery selection was not too difficult. The Servo Motor was the big driver for the selection. The Servo operates at 5V and Running current is 500mA (Stall current is 2.5A). The battery needed to be fairly small to fit inside a slim case. I wanted to mount the PSC on the back of the Plantation Shutter frame. So the best option was two Li-ion (18650) batteries in series. This would provide a minimum of six volts when nearly discharged and up to 8.4V fully charged. This would allow me to use an AM1117-5.0 Voltage regulator to power the Servo as it has a drop-out voltage of one volt and can provide up to one amp of current. Power management was going to be important and I will discuss this further when I discuss some design concepts.
There are many processors I could have used. The Arduino processor would seem to be an obvious choice. However, one of my added features to the design was being able to communicate with my Home Automation System. As I could not add any cabling, the interface would have to be wireless. Now I could have used a 433MHz module or even a Bluetooth module with an Arduino. However, I wanted things to be physically compact and simple. So originally, I opted to use an ESP8266 processor. I also decided to go straight for a PCB design as I did not think a Veroboard prototype would fit into the limited size case I had found. The ESP8266 module I used was the readily available ESP12E module. The ESP12E module provided all the interfaces I needed and came with WiFi built in. This made interfacing to my Home Automation System relatively easy, once I figured out how to use the Message Queueing Telemetry Transport (MQTT) protocol. I used the MQTT protocol to send status updates to my Home Automation System. It is about this time, I also decided to learn how to design with Surface Mount Devices (SMD). I purchased a relatively cheap Soldering Iron and Hot Air Gun Workstation (a generic 853D System). While I found I could solder SMD device with a fine tip on my soldering iron, the Hot Air Gun made life much easier.
The ESP12E design worked well and after about five versions, I settled on a design, which has worked fairly well for a few years. I even made use of some Radio Control (RC) mechanics to interface it to my Plantation Shutters.
From the photograph shown here, you can see the case I found that I screwed to a piece of wood and in turn screwed it to my Shutter. I connected the Servo via a RC Horn to the Shutter, similar to the setups used in RC airplanes. You also see the dome of the BH1750FVI Light Sensor.
There were a few issues I encountered over time with using the ESP12E. I will discuss this further in the later section on Design Concepts. Eventually, this resulted in me changing the ESP12E for the ESP32. The ESP32 is a much more powerful processor than the ESP8266, not that I needed more power (Jeremy Clarkson may disagree!). However, some of the ESP32 features made it more attractive. Also, the ESP32 WROOM module, which I used, was not much bigger than the ESP12E module.
My initial Design Concept is shown in Figure 1. Here you can see many of the components already previously discussed.
Starting from the left, you can see the Li-ion Battery (BT1). This is connected to a DC connector (Charger Socket) which allows BT1 to be charged via an external charger. BT1 is also connected to the Power Switch (S1). S1 provides the BT1 voltage directly to the 3.3V and 5V Regulators. The 3.3V regulator supplies power to the ESP12E processor, the Light Sensor and the Temperature Sensor. The ESP12E also measures the BT1 Voltage (Bat In) via a voltage divider to determine the state of BT1.
To save power, I used a relay to supply power, via another 5V Regulator, to the Servo Motor. Power is only supplied to the Servo Motor when it is required to open or close the Shutter. Note a Level Shifter is required between the ESP12E and the Servo Motor for the control signal as the ESP12E operates from 3.3V and the Servo Motor from 5V. Lastly, there is a Program Connector, which is a modified FTDI connection for programming the ESP12E.
So, how does this all work? Firstly, part of the program for the PSC contains a Webserver. The Webserver allows the setting of the following parameters.
- WiFi SSID – this is the SSID of my router which is connected to my Home Automation System;
- WiFi Password;
- MQTT IP Address – the IP address of the Home Automation System, on which I run a Mosquitto MQTT Broker;
- MQTT Server Name – the login name for the MQTT Broker;
- MQTT Password – maybe not necessary, but just provides an extra level of security just in case my Router was hacked;
- MQTT Port Number – this is set by default to 1883, but can be changed;
- Minimum Light Level – this is the light level, in lux, below which the PSC will close the shutter;
- Maximum Light Level – this is the light level, in lux, above which the PSC will open the shutter, as long as the Maximum Temperature has not been exceeded;
- Maximum Temperature – this is the temperature above which the PSC will close the Shutter;
- Maximum Angle – this is the angle the Servo Motor will rotate to open the Shutter.
Once all the parameters are set up, you click the “Finish” button and all the parameters are saved in EEPROM. The ESP12E is then placed into “Deep Sleep” for five minutes, this is used to reduce power consumption from the battery as much as possible. I initially put the ESP12E into “Deep Sleep” with WiFi disabled on wakeup.
This was to save even more power as the WiFi is very power hungry. Usually on wakeup, if it was required to open or close the shutter, I would put the ESP12E back into “Deep Sleep” for a second, but to wakeup with WiFi enabled. I later decided not to use “Deep Sleep” with WiFi disabled on wakeup, as the power savings were not that great and it enabled me to simplify the program.
So the program process is fairly simple (see Figure 2). On wakeup, the ESP12E would read the Light Level from the BH1750FVI, the Temperature from the DHT11 (later replaced by the DS18B20) and the Battery Voltage.
If the Shutter was closed and the Light Level had exceeded the Maximum Light Level and the Temperature was less than the Maximum Temperature, the Shutter would be opened by the PSC and an MQTT message would be sent to the Home Automation System. The MQTT Message would contain data indicating Light Level, Temperature, Shutter State (open or closed) and the Battery Voltage. Similarly, if the Shutter was open and the Light Level was less than the Minimum Light Level or the Temperature was above the Maximum Temperature, the Shutter would be closed by the PSC and an MQTT Message sent to the Home Automation System.
If the Battery Voltage was less than the defined Battery Minimum (6.5V), an MQTT Message would be sent to the Home Automation System, regardless of any of the other readings. The idea behind this was to trigger a notification being generated by the Home Automation System to let me know the battery needed charging.
- This all worked fine until I realised that if I needed to change any settings, I would have to power down the PSC to restart the Webserver to allow changing the settings. While this was not a problem in my study, where all my development work was conducted, it was going to be awkward, if not difficult, to power cycle the PSC, when installed on my shutters over the house entry.
- Associated with the first issue is what happens if you don’t have a Home Automation System or don’t want to send MQTT Messages.
- Also, connecting a charger to the PSC to charge up the PSC Battery was going to be difficult. Initially, I thought a portable charger based on a Power Bank would be useful. This is the main reason I developed my Power Bank Spoofer (PBS) as presented in the DIYODE Article “Portable Power” in the October 2023 Issue. Yes this worked, but it was still a little awkward as every couple of months, I would have to get my ladder out and connect the Power Bank and PBS to charge the PSC batteries.
- The final issue was that I could not override the automated process of the PSC. If I wanted to open the shutter on a day that was a bit dark, I could not do this. Sometimes, my wife would like to close the Shutters, if it became too bright – not too sure why, but it is not wise to question your wife.
Addressing the Issues – Remote Control
Issue 2 wasn’t really thought about until I had my final hardware design completed and the solution is through software. So addressing issues 1 and 4, I decided some sort of remote control interface was required. I had been playing around with some simple 433MHz Transmitters and Receivers. It was not possible to interface the ESP12E to the 433MHz directly as the ESP12E is put into “Deep Sleep” for 300 seconds and there was no way that I was going to have my 433MHz Transmitter transmit for 300 seconds. So I decided another processor was required which would only process data received by the 433MHz receiver (Refer to Figure 3).
The concept here was that the 433MHz Receiver would receive a signal from the Transmitter Remote Control Unit (RCU). The ATtiny85 would decode the signal and validate it. One of three commands could be received, Open Shutter, Close Shutter or Reset ESP12E. The ATtiny 85 would send a short pulse to the ESP12E on its Reset input. This would wake the ESP12E from “Deep Sleep”. The ESP12E would read data from the two data lines (Data 1 and Data 0).
If a code 01 (Open Shutter) or 10 (Close Shutter) or 11 (Reset ESP12E) was received, then the ESP12E would take the appropriate action. If the command was Open or Close, the PSC would now be in Manual Mode and the PSC would no longer Open or Close the shutter dependent on Light Level and Temperature. The PSC now only responds to remote commands. That is until a command is received that is the same as the current state of the Shutter. For Example, if the Shutter was Open and the remote command received was Open, the PSC would then revert back to Automatic Mode.
While this remote control sort of worked. It had a few issues. Firstly, I did not know whether the command had been received except that the Shutter opened if the Open command was received. If the Reset ESP12E command was received, the only way I knew it had been received was to use my phone to see if the Access Point had been established.
Lastly and most importantly, even though the ATtiny85 only consumed a few milliamps as did the 433MHz Receiver, it flattened the battery much more quickly than before.
I then came up with Version 5! I replaced the ATtiny85 with an ATtiny84 as I also replaced the 433 MHz Receiver with the nRF24L01 2.4 GHz Transceiver. I had to replace the ATtiny85 as more I/O was required to interface to the nRF24L01 as it used a Serial Peripheral Interface (SPI), which requires four I/O pins. I also added a buzzer to let me know when the Reset ESP12E command had been received (two Beeps) or when a command had been received to put the PSC back into Automatic mode (one Beep) (refer to Figure 4).
To reduce power consumption, the ATtiny84 would go into “Sleep Mode” for about a second, but before doing this, it would put the nRF24L01 into “Power Down Mode”. The overall effect was to reduce power consumption down to a few micro Amps. So, every second the ATtiny84 would wake up, enable the nRF24L01, listen for 10 milliseconds to see if there were any incoming commands. If not, everything would go back to sleep. 10 milliseconds, at the time seemed a reasonable amount of time to sample for incoming commands, as I was sending/receiving data at 100kbits/s. If a command was received, the nRF24L01 transceiver was programmed to send an automatic acknowledgement back to the RCU. The RCU would then turn on a LED for a second to indicate the command had been received. If the sent command was not acknowledged after a timeout, a quick flash of the RCU’s LED occurred.
This design with the nRF24L01, initially, worked fairly well. It was not 100% reliable as sometimes the command was missed in the sample window. I am not exactly sure why this occurred as the RCU would transmit the command several times for over a second. However, over time, I found that the ATtiny84 and nRF24L01 became unreliable to the point it did not work at all. I found I had to power down the whole PSC and reboot everything. I did try putting a Watch Dog Timer function into the ATtiny84, as I thought maybe the program was jumping in to somewhere strange. This did not help. So I gave up for a while and worked on addressing issue 3.
Before I describe my resolution for issue 3, I did eventually return to my overall design to address the remote control issue. I decided a complete redesign of the PSC was required. This time I built a prototype on Veroboard to try out some new concepts. I also used my USB Auto-Ranging Current Meter (see DIYODE article “USB Power Meter” November 2022 Issue 64), to measure the current consumption. I measured around 150µAmps, when in “Deep Sleep” Mode.
Now, referring to Figure 5, you will see there have been several changes.
The main change is swapping out the ESP12E Processor Module for the ESP32 WROOM Module. The ESP32 has some better options with regard to “Deep Sleep” wakeup. It also has a lot more I/O pins.
I have also replaced the Relay with a MOSFET Switch. The main reason for this is to reduce the footprint. On some of my revisions, I found the Relay was getting in the way of the Servo Motor. Although, this issue was removed when I went to 3D printing my own cases. However, the MOSFET Switch is faster and consumes less current and enabled me to remove one 5 Volt Regulator.
The ATtiny84 and the nRF24L01 have been replaced by a HC-12 433MHz Transceiver. The advantage of the HC-12 is two-fold. Firstly, as it operates at 433MHz, it has a better range and I can operate it from just about anywhere in the house. Secondly, it has a mode setting (FU2 Mode), which enables it to be powered up all the time, but only consumes 80µAmps maximum.
While it only operates at 4800 baud and there is a transmission delay of half a second for a single byte being transmitted, I found this to be very reliable. The one thing it does well with the ESP32 is to wake it up directly when a command is received.
The ESP32 uses a serial port operating at 4800 baud to communicate with the HC-12. I have used the Transmit line from the HC-12 connected to another I/O pin of the ESP32, to wake the ESP32 from “Deep Sleep” using the wake on a “low” on an I/O pin function.
Addressing the Issues – Charging the Battery
As you may have already guessed, the charging of the battery was addressed using a Solar Panel with the appropriate electronics attached to ensure the Li-ion batteries are charged correctly. I have designed a Solar Battery Charger, which fits on a small PCB and is attached to the back of the Solar panel. In my case, I have used a 10 Watt Solar Panel and have attached two of my Solar Panel Charger PCBs to the back. This way, I can use a single solar panel to charge the batteries of two PSCs. In the associated photograph, you will see I have made a splitter connector to take the output of the solar panel and connect two Solar Panel Charger PCBs.
In my design, I have used an Integrated Circuit (IC) specifically designed for charging Li-ion batteries from a solar panel. It is the Consonance CN3795. As it is only available through a Chinese Supplier, I have been looking at alternatives to present in DIYODE. So at this stage, I am looking at presenting a design in a future article.
|1 x ESP32 WROOM 32 bit processor module
|1 x HC-12 433MHz Transceiver
|1 x BH1750FVI Light Sensor
|1 x DS18B20 Temperature Sensor
|1 x MG996R Servo Motor
|1 x MCP1703AT-3302E/CB 3.3V Voltage Regulator
|1 x NCP1117LPST50T3G 5V Voltage Regulator
|2 x IRLML6402TRPBF P-MOSFETs
|3 x 2N7002ET1G N-MOSFETs
|1 x PKM13EPYH4000-A0 Buzzer
|1 x 1k 0805 SMD Resistor
|1 x 4.7k 0805 SMD Resistor
|6 x 10k 0805 SMD Resistors
|3 x 100k 0805 SMD Resistors
|1 x 180k 0805 SMD Resistor
|1 x 220k 0805 SMD Resistor
|4 x 100nF 0805 SMD Ceramic Capacitors
|2 x 10µF 0805 SMD Ceramic Capacitors
|2 x 100µF CASE-C-6032 SMD Tantalum Capacitor
|3 x 2-Pin Male Header^
|1 x 3-Pin Male Header^
|1 x 5-Pin Male Header^
|1 x 5-Pin R/A Male Header
|1 x 5-Pin R/A Female Header
|1 x 6-Pin Male Header
|1 x 5.5 x2.1 mm DC Socket
|1 x PB Switch
|4 x M2.5 x16mm Screws
|Bolts and Nuts Australia
|4 x M3 x8mm Bolts
|4 x M3 Nuts
|4 x 4G x9.5mm C/S Screws
|Bolts and Nuts Australia
|4 x 4G x9mm P/H Screws
|Bolts and Nuts Australia
|1 x PSC PCB
|See Gerber Files in Proje
|1 x 3D Printed Case
|See STL files in Project
* Quantity required, may only be sold in packs. (1) Can also be made from HM3212/P5440.
Now is the time to put all the hardware together. This consists of two hardware modules. First is the Plantation Shutter Controller (PSC) which attaches to the Plantation Shutter frame and opens and closes the Shutter. Figure 6 Schematic shows my Plantation Shutter Controller design. Table 1 Shows the Parts List for PSC V7. The second hardware module is the Remote Control Unit (RCU) which will be presented in a future DIYODE article.
Using the Gerber files from the Project Resources, you can manufacture the PCB. I am not clever enough, nor have the facilities, to make my own PCBs. So I use EasyEDA (see Figure 6) to develop my schematics. Then I use the “Convert Schematic to PCB” function. This gives a basic PCB outline with all the components located nearby with all the “Nets” connecting the components. I usually adjust the PCB Outline to the size and shape I want. I then place all the components where I think they should be best located. I then place all the PCB tracks by hand. I believe EasyEDA does have an Autorouting capability, but I have never used it. I also place the ground planes top and bottom, making sure I leave gaps in the copper for things like antennas. I usually always use double sided PCBs.
Once I am happy with the routing, silk screens, component locations, I then use the “PCB Fabrication File (Gerber)” function. This produces all the files required for a PCB manufacturing facility to manufacture the PCB, in whatever quantity you want. Associated with EasyEDA is a direct link to JLCPCB, a Chinese PCB manufacturer. If you do not want to use a Chinese PCB manufacturer or not JLCPCB, you can take the Gerber files to just about any PCB manufacturing facility to get your PCBs manufactured. I have been using JLCPCB for a couple of years and have had no problems. The other great thing is the turnaround time. I usually get my PCBs within a week from Gerber file submittal.
Once I have the PCB in my hands, it is time to put the components on the PCB. You will see from the adjacent photo and the Parts List, that I have used a lot of SMD components. I always solder the SMD components in place first. You can use a soldering iron with a fine tip and solder every component I have specified. However, I found this tedious and not always successful. So almost from the start of using SMD components, I purchased a Hot Air Gun. (Actually I bought a W.E.P. 853D, a combined Hot Air Gun, Soldering Iron and Variable Power Supply Unit.) I would put some solder paste (I used the leaded kind as it is much easier to use) on the pads for some of the SMD components and use the Hot Air Gun to solder the components in place. It took a bit of practice to get the temperature right and the air speed correct – I did manage to blow some components off the PCB and still do occasionally, but I am getting better at catching them. Just recently, I bought a UYUE 946C Reflow Hot Plate. I have used it on the example in the adjacent photograph. The results are impressive and much quicker than using the Hot Air Gun.
Now that all the SMD components have been soldered on to the PCB, all the through hole components are soldered on. Note, the connector for the BH1750FVI Light Sensor is placed on the rear of the PCB.
With the connectors and Buzzer in place, the connector and Antenna is soldered onto the HC-12 433 MHz Transceiver.
The connector is also soldered onto the BH1750FVI Light Sensor Module. Note for the BH1750FVI Module, right angle connectors have been soldered onto the PCB back and onto the BH1750FVI Module back. Note the orientation in the accompanying photographs.
The 2-Pin connectors that I have used for the Power Switch, Battery and Charger Socket connections plus the 3-Pin Servo Motor connection are as per the Parts List. But as indicated in the Parts List, they could be simple header pins broken off a 40-Pin header strip. The reason I have used the connectors as per the Parts List is that they give a more secure fitting when the sockets from the Power Switch, Battery, Charger Socket and Servo Motor are connected to the PCB.
Now, it is time to start putting the PSC together. Assuming you have 3D printed the Plantation Shutter Main Lid (File “PS Main Lid.stl”), you should have something similar to that in the adjacent photograph. Note the circle in the bottom right. This is a thin section of the “Lid” to allow light to illuminate the BH1750FVI Light Sensor.
The PCB assembled previously can now be mounted onto the “Lid”. Make sure the orientation is correct, otherwise the BH1750FVI will not be illuminated. I used four 4Gx6mm Pan Head (P/H) screws to mount the PCB to the “Lid”.
Next, the Power Switch with wire and 2-Pin Female Header connected along with the Charger Socket with wire and 2-Pin Female Header connected are fitted into the 3D printed Plantation Shutter Main Base (File “PS Main Base.stl”).
The Servo Motor can now be fitted. I used four M3x9mm P/H bolts and M3 nuts to secure the Servo Motor.
It is time to complete the assembly. Attach the connectors from the Servo Motor, Charger Socket, and Power Switch to the PCB. It is now a good time to install the dual 18650 Battery Holder with batteries. The wires from the Battery Holder are also terminated with a 2-Pin Female Header. I secured the “Lid” to the “Base” using four M2.5x16mm P/H screws.
To help with installation, I have designed and 3D printed several components. They are shown in the below photographs with each component numbered. If you look at the photographs at the beginning of this article, you will see how each component is used.
The components as numbered are:
- Thick narrow Mounting Block. Two Mounting Blocks are required for each PSC. The PSC Base is screwed (two 8Gx9mm Countersunk screws top and bottom) to the mounting blocks. The PSC with Mounting Blocks are then stuck to the Plantation Shutter frame using double sided tape (eg. Bear Double Sided Tape (Heavy Duty) 36mm 4.5 metres). The extra holes allow you to move the PSC Base up or down if required.
- Thinner standard sized Mounting Block.
- Link connected to Servo Shutter Attachment (8).
- Link connected to Servo Arm (Servo Arm is standard 25T 27mm Futabo). Use M2x16mm bolt and Nyloc Nut
- Long Link for interconnecting Shutter Banks (require two of component 7). Use 4Gx9mm screws.
- Short Link for interconnecting Shutter Banks (require two of component 7).
- Shutter Blade Mount used for interconnecting Shutter Banks.
- Shutter Clip for connecting Servo Link (3). Use M4x20mm bolt and Nyloc Nut.
NEXT MONTH: PART 2
ABOUT THE AUTHOR:
Peter Stewart is a retired engineer and regular contributor to DIYODE magazine.
Other projects include an ATtiny85-based general purpose timer with buzzer in Issue 53, WiFi Temperature Sensor with Scrolling Display in Issue 60, an ATtiny84-based Auto-Ranging USB Current Meter in Issue 64, and Arduino-based Car Presence Sensor & Parking Assistant.