thank you this worked for me. for anyone who wants to test quickly, bring the wemos and relay at your desk. whenever you make the change to code, turn off wemos and turn it back on, see if relay clicks. if it does not then your code is working. keep trying different scenarios until you figure it out. easiest way is to plug wemos into power bar which has on off switch.
I have the clicking problem only with the MCP23017 port expander during reboot of the ESP, not during a boot from power down. My board does not reset the MCP during boot, so the previous register values are retained in the MCP. During boot of ESPHome, the code assumes that all output registers have value 0x00 while in fact they are 0xff if all my relays are off (inverted mode).
The fix is to patch mcp23017.cpp and add two register reads to get the current output register state.
@wutr , thank you for adding this solution. It corrected the momentary relay turn-on issue experienced when initiating or rebooting the ESP8266 that I’m using to control an irrigation system.
I’ve been banging my head with this problem for almost 2 weeks nothing I tried worked.
I have a passthrough battery pack to power d1 mini and a relay to charge it during the night, unfortunately because the battery turns off for a few milliseconds after the relay turns off and disconnects it from the outlet it would restart my d1 mini and with it would trigger all the relays including the one that is connected to the battery which leads to a loop where the mini keeps restarting until it fails.
The solution is super simple and right there in the documentation:
“* early_pin_init (Optional, boolean): Specifies whether pins should be initialised as early as possible to known values. Recommended value is false where switches are involved, as these will toggle when updating the firmware or when restarting the device. Defaults to true.”
Set it to false add the RESTORE_DEFAULT_OFF in the relay and everything works great without them auto switching on restart.
@Siar I have tried early_pin_init without luck. I am using Wemos D1 mini to control my Yeelight ceiling light. It has 3 GPIOs to control cold light, warm light and night light (dimmed light).
I am using pins D1, D2 and D5 (D5 is night light) and whenever I power it on, night light blinks for a split second. I know that it is that because when I disconnect the cable from D5, it is not happening.
This is my board definition:
I don’t have a lot of experience with PWN but unfortunately i think this is a hardware problem not a software one. From what i know D1 and D2 are the only 100% stable pins on boot for D1 mini, you can try a different pin to see how it reacts on boot or a different D1 mini if you have a spare(I’ve had cheap clones that react differently).
What i would try if you don’t mind complicating yourself is maybe adding a relay between your PWN pin and your light. That way after boot you activate the relay and complete the circuit and can drive your light as you would normally and during boot there might be a mismatch between the pin that operates the relay and your pwn pin which means the light won’t flicker.
I gave up quite a while ago with relays directly on esp pins becaise those short glitches. Now I always use port expander, usually mcp23017 (or 23008 for 8 outputs).
Sorry i was flying trough this thread on my mobile and i didnt saw that you are using lights with frequency. Have you tried on diferent pin maybe, and i dont know if you know, but the wemos D1 controls the PWM through software and they are known to make flickering and so on, the ESP32 have hardware PWM and they are preffered for the LED lights (ESP8266 Software PWM Output — ESPHome)
I was curious about this, and i found out that you will always get this problem because the esp8266 gives little voltage on boot on that pin and only the D1 and D2 are low in 0V always on boot. You can see in this screenshot.
Thanks @gitlaman . Initially I tried with ESP32 (Wroom 32D DevkitV4) that has no flicker, but it takes little longer to boot, so there is visible delay when device is powered via AC (I do use switches to cut the AC), even noticeably longer than stock firmware.
Then I switched to ESP8266 (wemos D1 mini), that boots way faster, probably faster than stock yeelight, but has the flicker.
I did order few more ESPs to play with, I will see which one (if any) brings satisfactory results.
I have found this hardware solution to prevent the relay from triggering. Solder a 470uF 10v capacitor over the optocoupler pins. This works perfect! Hardware solution to prevent relay from triggering
This would look like this:
Running into exactly this myself, with esp01+relay module. The information above is from Relay flickers on boot · Issue #1 · IOT-MCU/ESP-01S-Relay-v4.0 · GitHub where several possible solutions have been also discussed. Might be useful to someone.
D0 pin behavior cannot be controlled just by the firmware, it does a short low while booting.
I tried a bunch of the options supplied, some worked but my problem wat that the garage and gate I wanted to open/close also was still using the original gate/garage remote. the solution I got to work was the below code: