I have fitted my arduino with an ethernet shield and 4 single relays, that can control 240v.
I am trying to make a web interface, that will control some 240v devices in my house. I will post the code below.
When testing this with nothing connected to the relays, everything works like a charm. I can press a button in my web browser, an the digital state shifts accordingly.
But whenever I connect something to a relay, the arduino crashes, and the web server stops.
I can even control the other relays flawlessly, but when I choose a relay that is carrying a load - CRASH!
Hope that someone can help me?
#include <SPI.h>
#include <Ethernet.h>
String commandString = String(30);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
String portNames[10] = {"","","","","","","Port 6","Port 7","Airport Express","Forstaerker Stuen"};
// Initialize the Ethernet server library
EthernetServer server = EthernetServer(80);
void setup() {
// Set up the Ports
for ( int port = 6; port <= 9; port++ ) {
pinMode(port,OUTPUT);
}
// start the Ethernet connection and the server:
Ethernet.begin(mac);
server.begin();
}
void loop() {
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client) {
commandString="";
for ( int i = 0; i < 10; i++) {
char c = client.read();
if( c != '\n' && c > 0 ) {
commandString.concat(c);
}
}
turnPortsOnOff();
delay(100);
printWebPage(client);
client.println(commandString);
client.stop();
}
}
void printWebPage(EthernetClient client) {
String color;
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("");
client.println("Bendts Arduino Netkontakt");
client.println("");
client.println("");
client.println("");
client.println("</pre><h1>Taend og sluk for ting paa loftet</h1><pre>");
for ( int port = 6; port <= 9; port++ ) {
if(!digitalRead(port)) {
color = "green";
} else {
color = "red";
}
client.println("
");
client.print("<a href=\"http://");
client.print(Ethernet.localIP());
client.print("/Port");
client.print(port);
client.print("\"><button style=\"background-color:");
client.print(color);
client.print("\">");
client.print(portNames[port]);
client.println("</button></a>");
}
client.println("");
}
void turnPortsOnOff( ) {
if (commandString.indexOf("Port6") >= 0) {
digitalWrite( 6, !digitalRead(6));
delay(100);
}
if (commandString.indexOf("Port7") >= 0) {
digitalWrite( 7, !digitalRead(7));
}
if (commandString.indexOf("Port8") >= 0) {
digitalWrite( 8, !digitalRead(8));
delay(100);
}
if (commandString.indexOf("Port9") >= 0) {
digitalWrite( 9, !digitalRead(9));
delay(100);
}
}
I'm not sure how much is visible in the pictures, so I'll try to describe:
VCC on the relays are all connected to +5v on the arduino
GND on the relays are all connected to GND on the arduino
IN1 on the relays are connected to digital ports 6-9 on the arduino
Most of the 220v outlets are connected to NO on the relay.
I have tried to connect to NC on the first one, but it makes no difference.
You have a problem with some electrical noise in the system, induced by the Relay-board.
How does the underside of the relay-board look ? I have seen some relay-boards with insufficient insulation from the low-voltage to high-voltage side, making for dangerous situations and problems all together.
For example, look at this board - the one of the high-voltage pins is 2-3mm from the pin header to arduino - dangerously close!
Benno_fra_dk:
...so your advise is: buy a better relay board?
Only if the relay-boards is flawed. If they are not made like that, and dangerous, i thing the noise is entering by the air.
Also, try plugging the power supply for the Arduino in another outlet, or supply the Arduino with power from your USB-cable.
What voltage are you powering the Arduino with ? As far as i can see, the wall wart you use is a rather old un-regulated unit, and a spike on the mains line will be carried over to the Arduino easily. As you have wired it, a load on your relays will induce a power drop on the wires supplying the wall wart.
Some things to try out:
Set the power supply for 7,5V.
put a load on a relay, a lamp for example, turn the lamp off by it's switch. try turning on the relay - what happens ?
power the arduino from your USB cable plugged into the computer.
you are more than likely experiencing electromagnetic interference due to radiated magnetic field emissions or conducted susceptibility issues. What kind of current is flowing through the relays?
in a project i have built, i am using a 8 channel relay (not all 8 channels are actually used) and i list where i got it in the first post. i only use 115 VAC, and the entire system only draws around 3 amps. the system has had no issues once i got all of the software glitches fixed.
the suggestions to use a different source to power the arduino should work, or you can get a "Curtis filter" http://www.curtisind.com/RFIFilters/. these filters will help to filter out any common-mode or differential mode noise that may be on the line. another option to try would be to try separating the relay board from the arduino board.
i do not think you should have to go through all of this as i am sure other people have not had as many issues switching the loads you desire, but it may help.
For starters, clean the board with mineral spirits (Husholdsningssprit) & an old toothbrush - clean both sides.
Now, connect the cleaned board up to power and Arduino, and place the relay-board on something insulating (a piece of paper etc.)
My best guess is that it works. Try it out and return with your findings.
Anyhow, those relay-boards looks dangerous, with that minimal spacing between "Hot" and lov voltage side.
Have you metered the current from the Arduino output to the relay board input ? Also , if you monitor the DC voltage to the input on the relay board to see how stable it remains as relays are switched on and off .
The other thing is the wall wort that you are using , is it rated at 1 amp ? If 500 mA or less it could be insufficient current to drive the relays reliably.
Bob D