Random on-off toggles of 8ch relays module - NodemcuV2

Dear all,

hope this is the right section to write.
I’ve been getting creazy trying to do what, at the beginning, looked like a simple project.

My purpose: get 8 lights controlled by NodemcuV2. To do this I used

  • two NodemcuV2 (I will call them “node 1” and “node 2”)
  • two 4ch relay modules active low
  • breadboard for prototyping
  • components shown in schematics
  • 12v 5A power supply
  • AMS1117 5v module to lower 12v->5v and powering everything (for testing I used 5v bench power supply, later I ordered 5v power supply but I’m still waiting for delivery)
  • wall switch already used in my home

ESPhome firmware has been used for NodemcuV2, indeed ESPhome is a good solution to integrate control in Home Assistant (my personal domotic hub). I don’t post program code because, from my point of view, the problem is in the hardware of the project.

Problem description: relays toggle randomly, it looks like control pin is floating. Moreover, it is not possible to turn all lights on. Two are still ok, but more lights on results in relay not be activated (even if the In pin is low, I can see it from LED mounted on relay module for each channel).

Schematic: find attached

Description:

Relay modules are powered at 5v, while nodes (1 or 2) control IN pin of relay using NPN transistor.
Looking at the schematic attached, collector is connected to 5v with a 1kOhm resistor in series. Between collector pin and the resistor is connected the IN pin of the relay. In this way, the 1kOhm resistor acts as pull up resistor for the IN pin when Ib = 0, whereas IN pin turns to LOW if transistor is active.
Relay module has a jumper to use or not use the same power supply for nodes and relay modules. I plugged the jumper, so relay modules and nodes are powered by the same supply.

Each node has 8 digital pins available from D1 to D8 (some of them must be used as output only).
ALL the following needs must be respected:

  • know the state of the lights (if it’s on or off)
  • control lights remotly using Home assistant app
  • control lights using “physical” wall switch

Therefore, each node can control only 4 lights (instead of 8 lights). Below you can find pins description of node 1 (same concept for node 2):

D1 → used to read when wall switch of light 1 is toggled
D2 → used to read when wall switch of light 2 is toggled

D3 → connected to IN1 of relay module, it controls light 1
D4 → connected to IN2 of relay module, it controls light 2

D5 → used to read when wall switch of light 3 is toggled
D6 → used to read when wall switch of light 4 is toggled

D7 → connected to IN3 of relay module, it controls light 3
D8 → connected to IN4 of relay module, it controls light 4

How firmware works
Lights state can change in two cases:

  • pushing virtual switch in the app
  • pushing “physical” wall switch

In the second case, node reads that wall switch is toggled and changes the output pin state accordingly.
For example, if I toggle the wall switch that control light 1, the node reads this using pin D1 and changes the state of pin D3. Please refer to schematic2 to see how I connected wall switch and how I connected it to input pins of nodes.
The advantage of this structure is that I know always the state of the light. On the contrary, it’s not possible to know it only connecting relays module AC pins to physical switch. Indeed, in this latter case, if I’d toggle physical switch the state of the light changes, but the node won’t be aware of any change.

In theory, it should work but it does not. As I mentioned in the title, I see many random toggles of relays even if I used pull-up resistor for input pin D1-D2-D5-D6.
Moreover, when I don’t get random toggles, it is not possible to turn all lights on. Two are still ok, but with more lights I get relay not be activated (even if the In pin is low, I can see it from LED mounted on relay module for each channel).

Please consider that I tested schematics and firmware with jumpers before soldering on the prototyping board and everything worked properly (with only one light bulb connected). I also tested the soldered board and it worked (with only one light bulb connected). While testing I didn’t see any of those random toggles.

I’m not a beginner, but I learnt everything by myself, therefore I have some lack of knowledge. From my point of view the two problems (random + impossible to turn all lights on) might be related to:

  • not enough power/current from output pins of nodes or transistors
  • noise → everything is inside a wall box where all AC cables of my home are in
  • NPN transistor not saturated properly → IN pins are not almost low (0.6V of Vce,sat) but are instead “border line”

This is what I concluded from my months of pain.
Any suggestion, example, EXPLANATION, is more than welcome.
I’d like to use this project to improve my knowledge of high power/current control.

Thank you!

relayModule.JPG

nodemcuV2.JPG

kasas:
AMS1117 5v module to lower 12v->5v and powering everything...

Four 5volt relay coils and the NodeMCU draw close to 500mA, with 700mA peaks.
(12-5)*0.5 = 3.5watt is no job for an AMS1117, unless you have a large heatsink attached to it.
Better wait for that 5volt supply.

The relay board pictured can be driven directly with a 3.3volt logic NodeMCU pin.
Remove the JD/JD-VCC jumper.
Connect 5volt to JD-VCC (coil power).
Connect VCC to the NodeMCU's 3.3volt power pin (opto power).
Connect all the ground together.
Leo..