🔴 TASMOTA TUYA SHUTTERS – Arduino AC line detection

Welcome to my new post for my video on YouTube. In this one, I will show you how to add a few lines of code to Tasmota and make Tuya wifi modules work with your motorized shutters or rollers. This hack will work on single relay versions too. Basically we will make some changes to the counter functions. You seem interested, so let’s get to it!

See the video about it on YouTube!

Tuya makes several home automation modules. Many of the modules use a variant of ESP8266 module which is what we want. The reason is Tasmota, which runs on ESPs. Some of these modules have an AC line connected as an input to it. Tasmota at the moment does not support detection of the AC voltage line as a button or switch. As I develop simple, fast and useful solutions, I will show you where and which lines of code we should add into Tasmota to make it work with AC lines. This video might even serve for new and better Tasmota with some of my ideas. Well, the Tuya model we will work today with is the MS-104B-220 model. There is also a module for non-european markets. Links are in the description below. I’ll unpack one of them.

So, it comes with a nice manual showing how to connect it and how to use its’ dedicated phone software to use it. It even works with Amazon Alexa. But as I hate sending information to unknown foreign servers, I will upload Tasmota and even modify it a bit to serve my purpose. I’ve removed a fast mounting bracket.
First see how it looks like. The package states the maximum output is two times 1100W, but the relays inside state 2500W. Let’s check the connections. The left two, N and L, are for the power supply, S1 and S2 are AC voltage inputs and L1 and L2 are corresponding outputs from relays.
As we are hackers, we will use a back entry. Some tiny screwdrivers are necessary for lock-picking. You can see this job makes me troubles. I admit this specific module was hard. Others opened really fast. At the end push electronics from the front. This one is really stuck. You might take advantage of the side hole too.
Let’s take a closer look of eletronics. There are two relays, some side passive elements below and our point of interest – the ESP wifi module on the left as an addon board connected via 7 pins. As you can see there are even some useful holes at the bottom of addon board. If we flip module around, we can see there are no optocouplers – so we swim in dangerous waters.

Here is the module again. This picture I’ve made simplifies it all. The mentioned pins beneath the ESP are following. And the 7-pins connector is on the right side. This is the scheme we will need later on… To simplify it even further, here’s a collage of all important pictures. You can print the picture for further usage. We will connect the USB to serial adapter to all the pins under the ESP except the reset pin which is marked as RST. The IO0 pin will be connected to ground.

First, let’s open Visual Studio and open latest Tasmota folder. In the Tasmota subfolder where all the source code files are located, open “my_user_config.h” and the “xsns_01_counter.ino” which is located towards the bottom of the list. We’ll firstly edit the config file. It is advised to uncomment the override file. It’s name is here. But for the purpose of this video I will add overrides here. I use these ones. The important here are USE_SHUTTER and USE_COUNTER. Another lines we can change here to speed up the process are the following. WIFI SSID name and password. A few lines below you can enter your MQTT server IP if you use one. And the line I often change is telemetry period. The reason is, I try not to saturate my wifi. Well, you might look around and change other settings too. Save the file and go to counter .ino file.

I’ll uncomment the first line, so we can see the code clearly. Add these three lines. StopTime variable is used to prevent unintentional triggering. The author line is always a must-have :-). The counteroverflow counts zero-crossings when we press a key. In a 50Hz AC current we have 50 triggers, so in a 1/4s only 5. If you have 60Hz AC, you’ll have only 4 and a bit of triggers.
In the counter structure we’ll add pulse_counter for pulse counting.
In the CounterUpdate function you can uncomment these lines. And add the following pulse-counting line. For every zero-crossing when we hold the key, the counter will go up.
Go down.
In the CounterInit you can add pin setup for the buzzer if you decide to use one. This is a shortcut for using the Buzzer. The long way around it to enable the buzzer subroutines in the override .h file.
Add the complete procedure to reset pulse counters as seen here.
Another complete addition is the procedure seen here. It will be called every 1/4 of a second. First we’ll copy counter states in these two variables. If either one is higher then 4 we sat above, procede. Check if enought time has elapsed since last keypress. If so, do turn the buzzer on. BuzzerBeep(1) function turns it on and also off after a single beep. Next we reset pulse counters and set the StopTime 3 seconds in the future. The next line prevents times when variable might overflow at the top of 32-bits. Val1 and Val2 will hold relay states. If either one is ON, this means the shutter is moving and our intention was to stop the movement by powering relay OFF. ExecuteCommandPower does this. We do this as a simulation of pressing a button. If relays are OFF, we want to trigger movement. Here we have to check which key was pressed and simulate the right button press for the right relay. See the SRC_BUTTON as the source – just as above. We finish by turning the buzzer off.
In the CounterEverySecond() comment out all lines or delete them as we only need CounterReset() which we have to add. It will erase all unintentional zero-crossing detections.
Comment CounterSaveState() as we have nothing to save.
We will also modify CounterShow() as we have nothing important to display or report back. The only urgent thing to display is authors’ name, but just after you satisfy your urge to SUBSCRIBE, LIKE and SHARE this video.
The three procedures and functions below may also be omited as I have found no use for them in this Tuya – Shutter module.
And the last thing to add is a case call in the XSNS01 function. Add these three lines. It is a call to a function we have discussed above.
Follow by commenting out the lines here. We have nothing to report and nothing to save.

At this point find under the “Teminal” the “Run Task…” option. Find one of the useful commands like “Clean”, build minimal or build Tasmota. Both builds will come handy in next chapters.

For the software part of flashing I will use ESPEasyFlasher. First plug the device in one of your USB ports. You can find both exe files on my Github pages. Run FlashESP8266 exe file. The firmware file which is in the same folder is automatically found. Select it and click Flash. At the end it will report back Flash complete.
That’s it.

On my Windows machine I use “Advanced IP scanner” which is free. It is simple to use. Click Scan and find the device with the name starting with “Tasmota”. On the Android phone I recommend “Fing“. You can change the name Tasmota and I will show you how in the chapter starting now.

So, let’s see what we have to change in Tasmota, when we upload the new firmware. Go into Configuration. Under Configure Module we have Sonoff Basic at the moment. Click the Configure Template. First let’s change the name to Shutters. If we do not use the GPIO, we select None(0). So, GPIO0 None. GPIO1 None. GPIO2 has a Button1 on it. GOPI3 None. GPIO4 has a buzzer connected. GPIO5 is connected to an AC line. It can be used as a clock, if you want. GPIO9 None. GPIO10 None. GPIO12 is one of our counters. Select Counter2. GPIO13 is Counter1. GPIO14 is connected to a Relay1. GPIO15 is connected to a Relay2. GPIO16 None. And ADC is None. Save the template. Device will have to restart. When it restarts, go into Configuration, Configure Module. Select newly created Shutters module and click Save. Device will restart again. This might take some time. More than a few seconds. Now go into Confuguration, Configure Module again. You can see everything is correct. Let’s fly over other settings. These are my logging settings. Sparse logging. I prolong even Telemetry period. Now click Configure other. Select appropriate names. As we need only first name, I add a letter B to the other name, which is the same. Save settings and wait for a restart again.
Now here are the most important steps to make. Let the Tasmota know, we want to use shutter functions. Under Console we’ll enter some commands. The first one is “SetOption80 1” – which means: enable blinds and shutters support. Next one is “ShutterRelay1 1”, followed by “backlog pulsetime1 0;pulsetime2 0”. Backlog only means we are joining commands in succession. You can write “Pulsetime1 0” first and press enter and follow it with “Pulsetime2 0”. Next combined command is “backlog interlock 1,2; interlock on”. See, I’ve made a mistake, but it still works.
If we go back to first page, we can see the module works. Only one relay can be ON at any time. And it will switch off according to assumed position of the shutter. Mind we have not set any information about the specific shutter yet.
Now we’ll set specific information. Enter “ShutterOpenDuration1” folowed by the correct time in seconds it takes for the shutter from fully closed to fully open position. In my case 23 seconds. If it takes 23.5 seconds, just use a period mark (“23.5”). Now also enter “ShutterCloseDuration1” in the same way. Mind that uppercase letter are not important in the console. Now you can check if the entered numbers are right for you. And this should be all.

In Configure Other you can paste the template, which you can find in the description below. Don’t forget to check the Activate checkbox and click Save afterwards. After restart follow the steps from the last chapter.

If you already have an existing Tasmota on your device and want to upgrade to a newer version follow these steps. Click Firmware Upgrade. Click Choose File. Find and select a Tasmota bin file which is compiled as minimal. Emphasis is on minimal. Click Start Upgrade and wait for it to finish. Check the lower left corner of the video. When the device restarts it reports it is a minimal version and that you have to upgrade, so repeat steps again but with new and latest firmware you have made. At the end you will have your new version.

Here you can see my old setup. I have used HopeRF 868MHz modules with Microchip Mega-328s. When designing my house I’ve pulled 4-wire cable to every inwall box for home automation. I will not need it in this case anymore. The blue wire floating in the air is antenna and the two white boxes in the front are relays. I will dismentle the old module and take care of the wires. I almost always work with live wires, not because of extra thrill, but because I need to know the function of each one. I put them one by one into this new Tuya module. Here I have to mention I use push buttons for inputs, not toggle switches.

Now let’s check if everything works. If I push the down button on my tablet, the shutter goes up. Ups… :-). That’s not good. This confirms I should do a test montage before I shoot the video, but as I do all the work myself, this should suffice. So, this is the place I ask for your support, so I could afford additional help. Please do hit LIKE below, SHARE and SUBSCRIBE. Buy me a COFFEE and become my PATREON. Donations are surely welcome. So the solution to my mistake is to switch wires between S1/L1 pair and S2/L2 pair. That’s what I am hiding behind my left arm. In a recap, to avoid this mixup, remember the first S/L pair is for UP and the second S/L pair is for DOWN. Let’s check the tablet again. Up is up. Down is down.

So, this was simple and fast Tasmota code modification to use on Tuya modules dedicated to shutters. I have shorten the modifications, as new versions of Tasmota come out every few days. The ideal way would be a separate code module which would use even more simplified code that I’ve presented. The solution running inside the main loop and counting zero-crossings would interfere with the execution times of Tasmota. Some of you don’t agree? If so, do comment below and present your solutions. I hope I’ve given you something to think about. See ya next time!

#tasmota #arduino #tuya #esp8266 #Celje #slovenija #slovenia
In this video I will show you how to modify Tuya wifi switch module (MS-104B-220, QS-WIFI, QS-WIFI-S04-2C) into Tasmota device. This Tuya module has Espressif ESP8266 on it. As you know Tasmota runs on ESP8266 chips. Tasmota is written in Arduino C variant. This 2-relay Tuya module also has 2 inputs. Inputs are tied directly to AC line. We will modify Tasmota code to use this AC lines – live wires. We will use counter functions and modify them to count zero-crossings when we hold down the button / pushkey. When the count will be correct, the module will trigger corresponding relay. We also use shutter support in Tasmota. So, we’ll learn to change Arduino code to move the shutters. Tasmota Tuya Flash.
I will show you hardware pinout of the Tuya module, disect Tasmota counter functions, teach you how to flash the device, how to find the ip address of Tasmotized device, how to correctly setup the Tasmota for shutters support and how to mount the Tasmotised Tuya module.
This code also can be used on single relay Tuya modules, just as long as it has ESP8266 wifi module.

Links to 2-relay modules:

Links to single-relay modules:

Other super value deals:

Please check price.

Shutters Template:

Commands for Tasmota Shutters:
SetOption80 1
ShutterRelay1 1
Pulsetime1 0
Pulsetime2 0
Interlock 1,2
Interlock on
ShutterOpenDuration1 23.5
ShutterCloseDuration1 23.5
ShutterInvert1 1


Backlog SetOption80 1; ShutterRelay1 1; Pulsetime1 0; Pulsetime2 0; Interlock 1,2; Interlock on; ShutterInvert1 1
Backlog ShutterOpenDuration1 23.5; ShutterCloseDuration1 23.5

Set the 50% open position of the shutter. This can be corrected by using ShutterSetHalfway1. Use this procedure to calibrate the half-open position:

  1. ShutterClose1
  2. ShutterSetHalfway1 50
  3. Move the shutter to actual 50% open position.
  4. ShutterPosition1 to inquire the shutter’s current position and record the value. This value is a percentage of the total opening (e.g., 63 = 63% of opening).
  5. ShutterClose1
  6. ShutterSetHalfway1 63 (using the value from step #4 above)
  7. Restart 1

After tuning add extra seconds at end movements to fix movement driftoffs: ShutterEnableEndStopTime1 1 (1 = 1 second).

Details for shutters on Tasmota site.

Reward for readers of this description:
If you need to erase complete flash: esptool.py --port COM5 erase_flash

If you want to increase the wifi strength in Tasmota use “Wifipower X” in console, where X is 0-20.5. Default power is 17.0.
Please like, share, subscribe and buy me a coffee!

Do you like my work?
1. Hit LIKE.
2. Hit SUBSCRIBE! Did you consider subscribing to my youtube channel and being updated when new videos are published?
3. COMMENT below.
5. Support my work on Patreon: https://www.patreon.com/GreenEyedExplorer
6. You can also donate directly (COFFEE) to me via: http://paypal.me/rokrodic
7. Buying through my affiliate links makes you no additional cost, but gives me a small headstart in getting new things for my explorations.

GreenEyedExplorer Thingiverse: https://goo.gl/jdyHbF
GreenEyedExplorer GitHub: https://goo.gl/XtjB3h
Tasmota: https://github.com/arendst/Tasmota

Songs in the background: Andrew Langdon – Waiting, Topher Mohr and Alex Elena – Where I am From, YouTube Audio Library.

3 thoughts on “🔴 TASMOTA TUYA SHUTTERS – Arduino AC line detection”

  1. Nice video!
    – Do you know if Tasmota has been updated since your post with such a modification or those added lines still need to be implemented (today is June 2020).
    – I have on/off switches, no push buttons. Do you know if I need to adapt your modifications to cope with my hardware?
    Thanks for this interesting post!

  2. Hi
    Thanx for nice tutorial.
    However i’m trying to use a module to control two different lights (double switch). I have successfully flashed tasmota but don’t know how to configure the template and whats more needs to be configured. I’m new at this and still learning so any help is appreciated.
    Thank you in advance!

  3. Hi
    Thank you for great tutorial! However i’m using the module to control two different lights but i’m stuck with the configuration. I have successfully flashed the device with Tasmota but don’t know which template to use or how to configure one and everything else needed. I’m new at this and i’m still learning so any help would nice :).
    Kind regards!


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: