Go Down

Topic: Not having fun/luck with ESP-8266 12E (Read 269 times) previous topic - next topic

DJDJDJDJ

Hi,

I am trying to configure an ESP-8366 12E to trigger a relay. I have no clue what I am doing wrong but I simply cannot make it fire one of the pins as intended.

Here is my simple code:

Code: [Select]



const int relay1Pin = 3;
const int relay2Pin = 4;
const int ledPin = 2;


void setup() {
  // initialize digital pin ledPin as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ledPin, HIGH);   
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH); 
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH); 
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH); 
  delay(100);                       

  digitalWrite(relay1Pin, HIGH);   
  delay(500);                       
  digitalWrite(relay2Pin, HIGH);   
  delay(1000);                       

  digitalWrite(ledPin, LOW);   
  delay(500);                       
  digitalWrite(relay1Pin, LOW); 
  delay(500);                       
  digitalWrite(relay2Pin, LOW); 
  delay(1000);                       
 
}



It is a very simple code to make the LED blink a few times and then trigger relays. I am using 2-relay module for this setup.

When I run this same code on Arduino Nano (changed led pin to 13), it works fine. LED flashed a few times and then both relays are triggered.

When I load it onto ESP, LED and Relay one trigger at the same time (same frequency). Relay 1 is not triggered at its intended frequency and Relay 2 does not trigger at all.

My hardware setup is pretty simple as well. It is simply to trigger the relays with nothing attached. I have attached images.

ESP unit itself seems to be working fine because if I load a wifi sketch, it will connect and acquire IP address. Just won't fire pins. I have been at this for a couple of weeks now. :-(

Thank you.


DJDJDJDJ

Here is the hardware setup

Danois90

#2
Sep 16, 2019, 01:58 pm Last Edit: Sep 16, 2019, 01:59 pm by Danois90
What type of relays are you using? Arduino uses 5V logic and the ESP uses 3.3V - if the relays need 5V to activate that may be your problem.

I've inlined your images here:






Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

Deva_Rishi

#3
Sep 16, 2019, 02:18 pm Last Edit: Sep 16, 2019, 02:27 pm by Deva_Rishi
On an ESP (nodeMCU as you have) the GPIO pin nrs are there ones declared in the sketch, but they are not the same as the numbers printed on the board. Check the pinout You have connected the Relay to pins D3 & D4 those are GPIO0 & GPIO2 so either change that in you sketch or change the way you connect the relay (a bit of both would actually be best, i would not want to use GPIO3 because it is the RX pin and  GPIO0 is a pin that can not be pulled low during startup or the unit will go into FlashMode
To 'Correct' you have to be Correct. (and not be condescending..)

Danois90

#4
Sep 16, 2019, 02:23 pm Last Edit: Sep 16, 2019, 02:23 pm by Danois90
On an ESP (nodeMCU as you have) the GPIO pin nrs are there ones declared in the sketch, but they are not the same as the numbers printed on the board. Check the pinout
Yes, using the constants D2, D3 and D4 would eliminate this "issue".

Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

alex_fagard

I'm finding it hard to follow all of your wires.

It seems like you need the following:

Breadboard power adapter 5V -> VIN/VCC on relay and to VIN/VCC on esp (assuming your esp module has a built in 3v reg !important).

Breadboard negative -> GND on esp and relay.

Now your relay is probably 5v, so you can't just connect it to a digital pin on your esp as those are all rated at 3.3v. This means, you'll probably have to trigger it via a transistor.

Something like:



If you don't have a npn transistor you can also use a mosfet.

Whether or not you've hurt your pins by feeding it too high of a voltage is another thing.

Please take what I've said with a grain of salt. I might not be entirely correct. If I am wrong, anyone can feel free to correct me.

PerryBebbington

Have you tried a multimeter on pin 4 to confirm it changes from 0 to 1 (0V to 3V3)? Better still an oscilloscope. A resistor in series with an LED to ground would be good too.

I found the pin out of the ESP8266s I have (different to yours) confusing. Maybe the outputs are not on the pins you think they are.

anthonyHope

I found the pin out of the ESP8266s I have (different to yours) confusing.
My nodeMCU is one of those early ones that's exactly the same width as a breadboard (is that v0.9 maybe,not sure), that is to say 2 pins too wide to be able to use it. So seeing as I had to mount it on 2x mini breadboards, I then stuck those on a bit of hardboard and glued the pin diagram to the back of that: it's the only way I can ever fathom out how to connect the bloody thing.



DJDJDJDJ

Hi all,

Thank you for your replies and suggestions. Here are a few of the answers to the questions raised.

My MCU and Relays are 5v. I was not aware that MCU gives out 3.3v on the pins. This could be one of my issues for sure. I have never worked with mofset or npn resisters so need to look these up and figure out which one I need ad ebay shows a boat load of different ones.

Another issue I see if probably the pin numbers as suggested above. Since I have worked with arduinos only and pin numbering on the board has not been an issue, I thought this would be same for MCU as well. I will check the  image mentioned above and also see if I can check it with multi-meter. I, unfortunately, do not have an oscilloscope. Yes, I have LEDs and resisters that I could use to test this as well. Thanks for the suggestion.

Hardware-setup wise it is pretty simple. Since MSU to as wide as the middle pins of the breadboard, I am using two breadboards. This fits perfectly and leaves me with enough room to attach wires. Breadboard rails are fed through power supply set to 5v.

I will come back with my findings.




Danois90

In order to fix the pin numbering issues, try this:

Code: [Select]
const int relay1Pin = D3;
const int relay2Pin = D4;
const int ledPin = D2;


A nice, cheap allround NPN transistor could be a BC337. The transistor needs to be in a "common emitter" (+5V switch) setup and not the "common collector" (GND switch) as the diagram in #5 shows. Alternatively you could look for a "logic level shifter" which will convert 3.3V logic to 5V and vice versa.
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

PerryBebbington


Quote
Unfortunately, do not have an oscilloscope
If you are serious about electronics as a hobby buy an oscilloscope. If you don't have a clue what to buy then either search these forums for advice (the subject has been discussed) or buy a cheap one. A cheap one will teach you 2 things: It will teach you how useful they are and it will teach you what features you need on a decent on. Then buy a more expensive one. Mine cost me about £250 for a duel input. 4 input would have been worth the extra cost however.




DJDJDJDJ

#11
Sep 16, 2019, 08:55 pm Last Edit: Sep 16, 2019, 09:31 pm by DJDJDJDJ
In order to fix the pin numbering issues, try this:

Code: [Select]
const int relay1Pin = D3;
const int relay2Pin = D4;
const int ledPin = D2;


A nice, cheap allround NPN transistor could be a BC337. The transistor needs to be in a "common emitter" (+5V switch) setup and not the "common collector" (GND switch) as the diagram in #5 shows. Alternatively you could look for a "logic level shifter" which will convert 3.3V logic to 5V and vice versa.
Thank you for the quick fix. I shall try it. I saw it in a couple of place but thought it was in error as we are defining the const as an int but assigning a string value. I am extremely new to electronics so don't even know what logic level shifter is. I will, however, check it out and order some BC337s as well. Thanks.

If you are serious about electronics as a hobby buy an oscilloscope. If you don't have a clue what to buy then either search these forums for advice (the subject has been discussed) or buy a cheap one. A cheap one will teach you 2 things: It will teach you how useful they are and it will teach you what features you need on a decent on. Then buy a more expensive one. Mine cost me about £250 for a duel input. 4 input would have been worth the extra cost however.
Thank you Perry. I will check it out. Since I am very new to this hobby, I am still collecting tools and equipment. I will look into getting one of these as well. Thank you.



Deva_Rishi

If your Relay board is of the type that i suspect it is (the type that includes an optocoupler) you can also modify it to take a 3.3v logic level by bypassing 1 of the 2 resistors (per channel) but to guide you with this you'll need a multimeter and we need a good close-up picture of the relay board (without the wires connected, they're obstructing the view)  Either way GPIO 0 is only usable as an output if the output is active-high which this relay board probably isn't.
To 'Correct' you have to be Correct. (and not be condescending..)

anthonyHope

#13
Sep 17, 2019, 07:19 am Last Edit: Sep 17, 2019, 07:26 am by anthonyHope
OP's project worked for me first time at bat. The builtin led blinks, and both relays switch. (I have a LoLin nodeMCU and the exact same relay module.)

I used this code (OP's, but note pin definitions):

Code: [Select]

// https://forum.arduino.cc/index.php?topic=636314

const int relay1Pin = D5; //aka 14
const int relay2Pin = D6; //aka 12
const int ledPin = D4; //aka 2, built in on nodemcu


void setup() {
  // initialize digital pin ledPin as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ledPin, HIGH);   
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH);
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH);
  delay(100);                       
  digitalWrite(ledPin, LOW);   
  delay(100);                       
  digitalWrite(ledPin, HIGH);
  delay(100);                       

  digitalWrite(relay1Pin, HIGH);   
  delay(500);                       
  digitalWrite(relay2Pin, HIGH);   
  delay(1000);                       

  digitalWrite(ledPin, LOW);   
  delay(500);                       
  digitalWrite(relay1Pin, LOW);
  delay(500);                       
  digitalWrite(relay2Pin, LOW);
  delay(1000);                       
 
}



I took 5V from the nodeMCU's "VV" pin to the relay module's Vcc, with the Jdd-Vcc jumper in place. (edit: nodeMCU powered from laptop USB.)

3V3 on the digital output to the opto's led cathode (relay module INx) is evidently high enough to turn the opto-led off (it has 5V on its anode, from the relay module Vcc (originally from nodeMCU VV).

When I get a gap I'll test it again with the jumper off and an external 5V supply, with no ground between the nodeMCU and the relay module.


anthonyHope

#14
Sep 17, 2019, 08:03 am Last Edit: Sep 17, 2019, 08:49 am by anthonyHope
UPDATE to previous:

First I ran exactly as above but with nodeMCU powered off a cell phone battery pack; all good.

Then I took the Jdd-Vcc jumper off, and removed the ground between the nodeMCU and the relay module. With no external power connected to the relay module, the relay module leds indicated correctly but of course the relays didn't activate. That's as one would expect: the relay indicator leds are across Vcc (nodeMCU VV) and a low INx (on) or a high (3V3) INx (off).

Then I supplied an external 5V to the relay module Jdd, and all worked perfectly.

THEN I had an idea: using the nodeMCU simply as a handy 3V3 power supply (no code doing anything), and with the jumper back on, I tried to activate the relays manually with 3V3 to the relay module Vcc, nodeMCU gnd to relay gnd, and nodeMCU gnd to relay INx as a low signal to activate.

Although I only tried to activate one of the relays at a time, only one of them ever worked! One seemed happy to activate with 3V3 on the coil and 0V signal, the other wouldn't. (Both relay indicator leds were working correctly.)

Anyway I think the upshot of the matter is that if (and it's not really clear from the photos) the OP is powering the relay coils from 3V3 that may be the problem. (Funny that OP seemed to get only one relay working at 3V3, same as I did....) That the nodeMCU signal is 3V3 is not an issue as far as I can tell: with 5V on the coils, nodeMCU digital low turns the relay on and nodeMCU digital high (3V3) turns it off.

Solution? OP should try powering the coils from the nodeMCU VV pin (which is 5V on mine) or better yet, take advantage of the opto isolation and use an external 5V.)

BTW, one thing I don't think the OP mentioned (I may have missed it though) is to say if the relay indicator leds are behaving correctly, although the relays (or one of them) are / is not switching.




Go Up