Page 1 of 1

[SOLVED]cannot connect relay module

Posted: Wed Aug 08, 2018 2:27 pm
by atux_null
hi. i a taking my baby steps with micropython and esp8266 nodemcu. i am trying to connect a relay and play with it.Image and i have a 9V battery and a led connected. the circuit is:
-a cable from the + side of the battery goes to the middle of the relay (C)
-a cable from the NO of the relay goes to one leg of the led.
-a cable from the - side of the battery goes to the other leg of the led.
Once everything is connected i see the led to be switched on.
the pin used in ESP8266 nodemcu to connect to is the D1.

then i issue on the repl:

Code: Select all

from machine import Pin
relay = Pin(5, Pin.OUT)
relay.value(0) # to switch it off
relay.value(1) # to switch it on
the problem is that the led statys always on and the onboard led(red) of the relay is always on. Also i do not hear a click as it is supposed to be when changing state.
thanks in advance.

Re: cannot connect relay module

Posted: Wed Aug 08, 2018 2:43 pm
by yeyeto2788
Try adding a delay between on and off to see if it's doing what is supposed to do.

What could be happening is that the pin changes its state (from on to off and vice-versa) so quick that you might not be seeing the led to go off or hearing the relay being activated/deactivated.

try something like this:

Code: Select all

from machine import Pin
import time
relay = Pin(5, Pin.OUT)
relay.value(0) # to switch it off
time.sleep(2)
relay.value(1) # to switch it on
Note the `time.sleep(2)` which will hang for 2 secs.
P.D: Hope it helps!

Re: cannot connect relay module

Posted: Wed Aug 08, 2018 2:55 pm
by atux_null
no changes at all. :?

Re: cannot connect relay module

Posted: Wed Aug 08, 2018 7:46 pm
by yeyeto2788
Are you sure the relay is 3.3 volts compatible? Is it active low or active high? If it is active low try connecting it to GND directly.

Also take into account that you should have all GND (relay and esp8266 GND) connected together.

I don't really see anything wrong on the code side, so it might be hardware related.

Re: cannot connect relay module

Posted: Thu Aug 09, 2018 4:34 pm
by atux_null
solved. instead of vin i had to connect to 3.3v pin.

Thanks a lot.

Re: cannot connect relay module

Posted: Mon Aug 13, 2018 10:39 am
by yeyeto2788
No problem! Glad you got it working. :D

You might consider changing the name of the Post adding a [SOLVED] at the beginning.

Regards.

Re: cannot connect relay module

Posted: Tue Feb 11, 2020 9:53 pm
by Sniper
yeyeto2788 wrote:
Wed Aug 08, 2018 2:43 pm
Try adding a delay between on and off to see if it's doing what is supposed to do.

What could be happening is that the pin changes its state (from on to off and vice-versa) so quick that you might not be seeing the led to go off or hearing the relay being activated/deactivated.

try something like this:

Code: Select all

from machine import Pin
import time
relay = Pin(5, Pin.OUT)
relay.value(0) # to switch it off
time.sleep(2)
relay.value(1) # to switch it on
Note the `time.sleep(2)` which will hang for 2 secs.
P.D: Hope it helps!
I get this output in micropython

Code: Select all

>>> from machine import Pin
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name Pin

Re: [SOLVED]cannot connect relay module

Posted: Wed Feb 12, 2020 9:13 pm
by Roberthh
Which module are you using? Really ESP8266? And are you sure that you are talking to that device?