I'm working on a home IOT using Rpi to run script that controls opening/ closing window's shade ( switching its electric motor up or down ).

1) Code is very simple and working fine - switching gpio "on" and "off"( using python, gpiozero, pigpio ).

2) Code can switch up or down using schedule (code ) or a physical switch ( input ).

3) Hardware in use: Rpi zero, a standard 2 relays with opto-coupler 5vDC, 220VAC motor to open close shade, and 3 state switch ( UP/OFF/DOWN ), originally belongs to window's shade.

As said - code works fine on the same hardware on table, for a very long time - doing the same tasks: scheduled on/off , switch using a physical switch.

Problem starts when installed to operate a real UP/DOWN of motorized shade, cause after some operations to Rpi to FREEZE. This freeze happens only when triggering UP/DOWN, it can happen after 1-30 switches. when pi freezes only unplugging it and plug back on restarts it.

This are the set of experiments done to find out what is the reason for freezing:

1)RPi was replaced with a new one - same result.

2) GPIO pins were replaced - same result.

3) RPI powered using battery (window uses wall outlet ) - same result

4) USB cable + DC converter - replaced several times - same result.

5) To be sure physical input does not causes this - I wrote a test code to create fast switches ( about 0.1 sec between UP/DOWN ) - caused same result ( meaning that physical switch does not causes it )

6) Twin relays was replaced 2 single relays - SAME result

7) Running test code to do same switching -without any relays connected - OK !

8) Disconnecting relay's output, 220VAC( and running code ) - OK !!

9) Returning Relay AC output - back to freeze.

to sum up:

a) RPI freezes due to switching 220 relay. it happens more frequent when switching is fast. Switching the relay while its output was not connected- it never fails, nor switch a 220AC bulb. My guess - It has something to do with switching an AC motor.

b) It is not related to input power, DC converter, USB cable, physical switch, cables or so.

Any ideas why it is happening ?

  • 1
    Top secret code running an unspecified module (apparently relays with opto-coupler - which makes no sense) in an undisclosed circuit - what do you expect anyone to suggest?
    – Milliways
    Jul 11, 2018 at 9:46
  • This will not be re-opened until you include a schematic and an SSCCE.
    – goldilocks
    Jul 12, 2018 at 13:46

1 Answer 1


The Pi GPIO operates at 3.3V logic. Looking at the schematics of the commonly used relays with optocouplers, you might be shorting the 3.3V logic and 5V when you turn the relay on. There is a resistor in there so it won;t go bust, however it can do something weird with your power supply which is what might be causing the freezes.

The best way to debug this would be to place an oscilloscope/multi-meter probe to the 3.3v and 5v channels and keep looking for noise that comes on those lines. There are a few workarounds that might work. First, try connecting the 5V VCC on the relay board to 3.3v and check if it works. It might work and this should do away with the problem of 5V and 3.3V shorting. If that doesn;t work, there is a hardware mod to make the relay boards 3.3v compatible here.

  • a) power supply was replaced several times, incl USB cable, with same result. b) your suggestion is to connect relay's VCC to 3.3v instead of 5v ? c) what is the purpose of your last suggestion ( external link )
    – guyd
    Jul 11, 2018 at 7:57
  • For b) the reason for connecting relay's VCC to 3.3V so that there is no shorting b/w the 5V VCC and 3.3V Input signal from the GPIO which might be causing some of the problems c) I think they are adding a 1k resistor in parallel so that the resistance on the octocoupler line reduces to 500ohm and the voltage drop reduces, so that the octocoupler still gets voltage greater than the activation voltage which might be b/w 1.4-2V (Though I saw in a few posts that people have been able to just use the 3.3V directly without this modification)
    – karan
    Jul 11, 2018 at 8:17
  • do you think using internal pull_up/ pull_down resistor will help ?
    – guyd
    Jul 11, 2018 at 8:31
  • No, pullup/pulldown would not help.
    – karan
    Jul 12, 2018 at 4:51
  • 1
    Yesterday I tried the 3.3v. It works, BUT after some switching, relay got stuck, and only after smacking on it- it got released. So I think it is not a very robust solution
    – guyd
    Jul 12, 2018 at 5:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.