Go Down

Topic: ESP8266 with Relay stops working (Read 1 time) previous topic - next topic

brandonm222

Hi there,

I am trying to use an ESP8266 to control a light switch wirelessly connected to a relay. I am able to get everything working but when I connect the 120V to the relay side my ESP will stop working after a minute or 2 sometimes longer or shorter. If I do not connect that part then the relay and ESP will working properly. I have left it running for 2 days striaght without any issues. I followed this tutorial exactly only difference is the code. I have read that you need to isolate the Relay by taking the jumper off between VCC and JD-VCC and giving it its own 5V however, that still causes the same issue. I also read that you should put a 4001 Diode in after the 2N2222 Transistor however, that still has not fixed my issue. I am almost certain it is not the code as it works 100% when the 120V AC is not connected to the Relay. When I plug that in the ESP will work a couple of times and then just stop working.

Here is the tutorial I followed:

http://www.instructables.com/id/ESP8266-Wifi-Switch/

And here is my code:

Code: [Select]
#include
#include
#include

// WiFi information
const char WIFI_SSID[] = "SHAW-789830";
const char WIFI_PSK[] = "251169111284";

// Remote site information
const char http_site[] = "192.168.0.25";
const int http_port = 80;
//TextFinder finder( client);

// Pin definitions
const int LED_PIN = 5;

// Global variables
WiFiClient client;
TextFinder finder( client);

int result;

void setup()
{
pinMode(2, OUTPUT);
connectWiFi();
Serial.begin(9600);
delay(2000);
Serial.println("connecting...");
}
void loop()
{
if (client.connect(http_site, 80)) {
Serial.print("connected... ");
client.println("GET /projects/test.php HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
if (client.connected()) {
if(finder.find("test")){
result = finder.getValue();
Serial.println(result);
if (result == 0){
digitalWrite(2, LOW);
}
else{
digitalWrite(2, HIGH);
}

}
else
Serial.println("result not found");
client.stop();
delay(1000); // check again in 10 seconds
}
else {
Serial.println();
Serial.println("not connected");
connectWiFi();
client.stop();
delay(1000);
}
}

// Attempt to connect to WiFi
void connectWiFi() {

byte led_status = 0;

// Set WiFi mode to station (client)
WiFi.mode(WIFI_STA);

// Initiate connection with SSID and PSK
WiFi.begin(WIFI_SSID, WIFI_PSK);

// Blink LED while we wait for WiFi connection
while ( WiFi.status() != WL_CONNECTED ) {
digitalWrite(LED_PIN, led_status);
led_status ^= 0x01;
delay(100);
}

// Turn LED on when we are connected
digitalWrite(LED_PIN, HIGH);
}


If anyone has any thoughts please let me know.

ieee488

In that tutorial I don't see anything about using a 120V relay.


.

brandonm222

No my relay is a 5V relay but is controlling a light switch which uses 120VAC to provide power the the lights

ieee488

No my relay is a 5V relay but is controlling a light switch which uses 120VAC to provide power the the lights
https://electronics.stackexchange.com/questions/110574/how-to-choose-a-flyback-diode-for-a-relay
has a good explanation

brandonm222

https://electronics.stackexchange.com/questions/110574/how-to-choose-a-flyback-diode-for-a-relay
has a good explanation
Thank you for that information. Unfortunately, after some further testing I noticed that it may not be the relay at all. If take the relay off and just have the esp running using the AC to DC converter and then turn on the light by just connecting the cables together the ESP shuts off. I suspect I am getting volateg spikes. However, I put an MOV in between the AC and the converter and I am still getting the same issue.

Rudy216

A MOV clamps peak voltages that are higher than the peak voltage of the AC line. They are useful if you have inductive loads that are generating these high voltages. They are of limited use, will reduce certain problems. The reason they are used so much is because they are cheap. Not because they are effective for most transient related problems.

You stated that you are controlling a lamp but you did not say what kind. At startup incandescent lamps draw 10 times the current compared to the continuous running current. Now let us say that when the relay is switched on that it happens to turn on when the voltage on the AC line is high or near the peak voltage of the sine wave. The relay contacts come together (not vet fully closed) and the voltage arcs between the contacts. The current is high (due to the filament being cold) and the current is high. The current is switched on quickly and is very high.

Because of the sudden change the change in current is not 60 Hz at that instance. It is the sudden jump and is effectively a high frequency change. At higher frequencies (faster rates of change to the current) magnetic coupling to adjacent wiring will transfer some of that energy to what is normally electrically isolated circuitry. This can cause voltage spikes onto sensitive circuits.

So it really matters how the wiring is done. It is best to minimize the likelihood of magnetic coupling. Current loops must be kept as small as possible. Physical distance between separate circuits should be made as large as practical. 

What do I mean by a loop? Consider power coming out of a wall socket. The current path will start with one wire (pick one it doesn't matter which) and it goes toward the load. At the load the current flows through the other wire to return to the power source. The area between the two wires is the loop area I am talking about. Any increase in distance between the incoming and the return path will increase the magnetic field produced by that current path.

Loop areas must be kept small to prevent power to be radiated. High frequencies (sudden changes) are easier to couple with other circuits than low frequencies. This is why disruptions mostly occur when a load is switched on or off.

The unintentional receiver of the disruption also can be a part of the problem. If loop areas are not kept small the coupling to other external circuits will be magnified. What is a loop area in our electronic circuits? Anything that has a current path. The output of a power supply, the trace/path it takes, and the return back into the power supply. The area in the path is what matters. Keep the area small and it will make the circuit less sensitive to external coupling.

This does not just apply to power circuits. (as I used as an example) But signal circuits as well. The path to and from a sensor. The input to a cpu pin, out to the sensor, and the return path to the chip's negative terminal.

When possible use twisted pairs for wiring. That twisting cancels out much of the signal.

Is any of this related to the problem that you are experiencing? Maybe. I don't know how everything was implemented. But how things are implemented, how things are wired, matter.

Using a solid state relay that has internal electronics to switch on circuits at zero volts on the AC line can help in minimizing magnetic coupling. There is no sudden rise when the circuit is turned on. And magnetic coupling will be based on the low line frequency, which is poor.

Implementing control electronics in the real world is not as simple as it seems.   

frank2644

Thank you for that information. Unfortunately, after some further testing I noticed that it may not be the relay at all. If take the relay off and just have the esp running using the AC to DC converter and then turn on the light by just connecting the cables together the ESP shuts off. I suspect I am getting volateg spikes. However, I put an MOV in between the AC and the converter and I am still getting the same issue.
I have used my Nodemcu's/ESP8266's with relays that control  120V frequently.

It's possible that a spike is causing interruptions in the DC output of your power supply. Try putting a .1 mfd ceramic cap on the DC supply and ground at the ESP . A 10 mfd electrolytic cap in parallel at the same point is also a good practice.

Google "bypass cap" if you need more info.

Frank

Go Up