Wawa:
The two resistors are used in series with the pin, but should be used as pull up resistors (NPN sensor).
So connect sensor outputs directly to the Arduino pins, and connect the resistor between pin and 5volt.
Educated guess is that the relays are using 80mA each when active.
That, and the draw of the Nano and sensors (~400mA) is just withing the limit of the USB supply.
Maybe wise to connect the 5volt supply directly to the 5volt pin instead of running it via that tiny USB diode
(that diode also lowers the supply by ~0.4volt).
Interference:
Don't run the fan wires next to the low voltage wires.
You might have to use snubber circuits across the relay contacts.
A VDR/Varistor, rated for your mains voltage, could be enough.
The relays itself might be rated for your mains voltage, but the board might not be.
YOU are the judge of that.
You might have to ground any exposed ground/controls/etc. of the Nano to mains ground.
Ask a electrician if you're not sure.
Leo..
Thanks for all the responses, much appreciated.
I Tried that didnt help:
- removed the 10k resistors from IR sensors
- added 0.1uF ceramic capactors from D4 and D8 to GND
Additional info:
- With lights OFF (meaning only 1 relay channel active at time) still resets, so I don't think its current problem
- I have pinMode INPUT for sensors, and they seem to be working correctly, should I still change to INPUT_PULLUP?
UPDATE:
- I removed the relay, reattached the connection and it seems to work without resetting at least while out of the enclosure. I also reattached 10k resistor and this time between D4&D8 and 5V rail
Arduino monitor shows that sometimes (although rare now) it records millis() handOnSensorTime timestamp a bit wrong for ir sensors but that could be maybe a reflection problem than hardware.
-> will put back in for more testing...
UPDATE2:
Again resetting problems;
- Can the metal enclosure cause problems? Arduino -> Relay wires are 20cm long jumpers that go through metal "canal"
- Does it affect that usb power supply and AC mains are connected to same outlet via outlet splitter? Can these cause interference?
Outlet
Arduino in enclosure
UPDATE3:
- I plugged some extension cable(1m) between power supply and arduino usb cable(50cm) and that seems keep resets away for now
- I'll keep testing why IR sensors register times for swipe motion wrong.
Test A : reduce proximity sensors to minimum -> RESULTS: Still errors reading swipe direction, probably close range hand reflection
Test B : set proximity sensors to max -> RESULTS: Too much reflection e.g. from opposite wall
Test C . take of sensors and test with angle to see if the problem is really a reflection problem -> RESULTS: 0 erros of reading swipe direction
Final test: Put sensors back and use zip tie to pull them together from back to create an angle to reduce reflection errors- > RESULTS: not enough angle, they need more space or some kind of filter system
As always, time to fix design flaws with software. Any experience with switching between sensors?
I'm first just trying by just reading every other loop left and every other right
UPDATE4:
Problem A - Arduino resets randomly using IR sensors and relays at the same time
Fixed by (active?) usb extension cable. Probably by reducing noice from the relays AC mains that was plugged in the same outlet using splitter (does anyone agree/disagree?)
Prolem B - IR sensors proximity crossed readtimes not consistent
Fixed by switching reading between sensors
UPDATE5:
Interesting update, using oven below (turning the dial to switch between programs), IR sensors registers a swipe right and turns the fan on! :O( That only happens when powered from laptop usb though)
Anyway,I modified code to only detect fast swipe or hand on sensor so it's not reading direction of swipe anymore.