Electrical – Basics: Raspberry Pi Zero w with relay. Relay trips without input

raspberry pirelay

I'm not quite sure how to word this so I don't know how to search for it to see if there's already been an answer. I am an uber basic noob can't even draw out schematics, so sorry in advance.

I have a Raspberry Pi Zero w with a 5v relay board. Pi is supplied with 5v. The relay is connected with 5v and GND to the 5v and GND of the pi. I then have a lamp spliced into the relay to be turned on and off via the pi.


There is more to my setup. This is when the issue started happening. I put in a momentary push button. It seems like the input is extremely sensitive.

Here is my issue: If I turn on or off the exhaust fan in the bathroom (for example) or if I turn on a hair dryer or another fan, it will turn on the lamp. It doesn't happen all the time. I assume it is some kind of surge. I just can't quite figure out how to make sure that doesn't happen.

I've tried adding resistors, but that didn't. I'm not sure if this is a capacitor thing.. That's about the time I realized I have no idea what I'm doing and so I figured i'd ask!

Edit #2

Ok, I did by best to draw up a schematic using fritzing.
enter image description here

Here is a picture
enter image description here

EDIT 3 And fix*

I accidentally my raspberry pi and caused it unsafe power off which corrupted the SD card. I had an ESP8266 laying around that I decided to use instead. The issue was indeed a floating state as I had the same issue with the ESP. Using a 10k resistor to ground fixed the issue. Also, using the ESP8266 is MUCH better for this application than the rPi!!

Best Answer

You're not the only one who's complaining. I suppose is about this module or similar:

enter image description here

I see this isuue over and over, the answer is the same as here, the bad design of the module, the driver transistor base is left floating which makes it sensitive to EM noises. If you have some soldering skills you can put a 1k ohm resistor from the pin 3 of the optocoupler to the ground (transistor emitter). Somebody should be fired for releasing such a crappy design.

Edit using updated quesstion, the pushbutton needs also a stronger pullup, put a 1 kohm resistor from the gpio pin to 3.3V.