I have successfully wrote a sketch for Ethernet Shield and Arduino. If I plug it into computer's USB, it works.
But if I plug into any other power source, like USB power adapter from iPhone or laboratory power source to power socket -- it doesn't work.
LEDs are doing various strange bursts, very different than if powered from computer.
What can be the reason?
UPDATE
I have updated sketch so that it doesn't refer serial:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
// B6-62-C5-CC-48-81
byte mac[] = {
0xB6, 0x62, 0xC5, 0xCC, 0x48, 0x81
};
IPAddress ip(192, 168, 10, 90);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
long requestCount = 0;
long upCounts[] = {0, 0, 0, 0, 0, 0};
long downCounts[] = {0, 0, 0, 0, 0, 0};
int currVals[] = {0, 0, 0, 0, 0, 0};
int prevStats[]= {0, 0, 0, 0, 0, 0};
int upThreshold = 1024-32;
int downThreshold = 32;
void setup() {
// Open serial communications and wait for port to open:
// Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Serial.print("server is at ");
// Serial.println(Ethernet.localIP());
}
void loop() {
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
currVals[analogChannel] = sensorReading;
if( prevStats[analogChannel] == 0 ) {
if( currVals[analogChannel] >= upThreshold ) {
prevStats[analogChannel] = 1;
if( requestCount>0 ) {
upCounts[analogChannel] ++;
}
}
}
else {
if( currVals[analogChannel] < downThreshold ) {
prevStats[analogChannel] = 0;
if( requestCount>0 ) {
downCounts[analogChannel] ++;
}
}
}
}
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
//Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 2"); // refresh the page automatically every 2 sec
client.print("X-Inthemoon-Request-Count: ");
client.println(requestCount);
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("X-Inthemoon-Analog-To-Digital: ");
client.print("Channel=");
client.print(analogChannel);
client.print("; ");
client.print("Min-Value=0; ");
client.print("Max-Value=1023; ");
client.print("Value=");
client.print(currVals[analogChannel]);
client.print("; ");
client.print("Up-Change-Count=");
client.print(upCounts[analogChannel]);
client.print("; ");
client.print("Down-Change-Count=");
client.print(downCounts[analogChannel]);
client.println(";");
}
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print("Was counting: ");
if( requestCount>0 ) {
client.print("YES (request count is ");
client.print(requestCount);
client.print(")");
}
else {
client.print("NO");
}
client.println("<br />");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(currVals[analogChannel]);
client.print(", up changes count is ");
client.print(upCounts[analogChannel]);
client.print(", and down changes count is ");
client.print(downCounts[analogChannel]);
client.println("<br />");
}
client.println("</html>");
requestCount++;
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
//Serial.println("client disconnected");
}
}
The situation is the same:
It is plugged into Samsung tablet USB power of 2A, so insufficient current is unprobable reason.
UPDATE 2
I also tried to remove inclusion of SPI.h
UPDATE 3
If I connect another Arduino board (but of the same vendor, it is from AliExpress), the behavior is the same.
UPDATE 4
I have compiled blink example and it works in any combination. If I connect Ethernet shield, then it blinks with both LEDs on Arduino board and on Ethernet shield.
UPDATE 5
I bought different Ethernet Board and it doesn't work too.
Please help. Have anybody ever powering Arduino + Ethernet not from USB?
4 Answers 4
Sorry I didn't see this earlier but I suspect you have a bad earth, because:
- It works with the PC
- You are using a wall wart
- You are wiring your board to another electrical source (Ethernet)
- You are saying the LEDs are flashing differently.
Run a lead from the GND connector on the board to an Earth point.
(It might be the Ethernet that is at fault, but unless you have access to the switch I don't know how you could ground that properly)
-
Indeed, bring the board over to a friend's house, check if it also happens there.aaa– aaa2017年07月26日 04:56:58 +00:00Commented Jul 26, 2017 at 4:56
-
There should be absolutely no difference between powering your Arduino from a computer USB port or a wall plug, especially if you keep your serial monitor closed.
So this is the debug procedure I would advice:
- disconnect the shield
connect the Arduino and upload a firmware with
no calls to
Serial
, but the most important one you must avoid is thewhile
loop in thesetup()
functiona short blink of an LED (you can use the onboard one, if you wish so) every time the
loop()
function iterates
- power your Arduino (without the shield attached) from your computer with no serial consoles and check the LED blinks at constant intervals
- power your Arduino (without the shield attached) from your wall plug and check the LED blinks at constant intervals
- disconnect the wall plug, attach the ethernet shield without an ethernet cable, reconnect the wall plug and check the debug LED and the LEDs on the shield are all blinking as expected
- plug the ethernet cable and hit the Arduino reset button and check all the LEDs are blinking as expected
Possible faults i can currently think of:
- STEP 3
- the firmware running on your board is incorrect: double check the compilation and upload process has completed successfully and you have no error in the Arduino IDE console
- STEP 4
- the firmware still contains some code which expects some input from the serial console: start opening the libraries you use in search for such code
- the voltage measured at the board between
GND
and5V
pins is insufficient and the board browns out: check the voltage is above 4.5V and the USB cable is not damaged or faulty
- STEP 5
- some of the code inside the ethernet shield library is trying to read data from a serial connection: check the library code again
- some of the components on the ethernet shield are faulty and they are pulling enough current from your wall wart to actually make your board go into protection (hard to believe, a 2A serious wall plug should be much better power source than your PC)
- STEP 6
- same as for step 5, but your ethernet cable might be playing a role
This might not be an answer per se, but it might help you find out the answer you need.
-
This won't work, the code is designed to run Ethernet shield and may be blocking forever at some point if the shield is not present. Moreover Dims already wrote that a simple blink code works with and without Shield. The problem only seem to occur when using the ethernet library.Julien– Julien2016年09月28日 19:28:32 +00:00Commented Sep 28, 2016 at 19:28
-
@Julien i didn't open the Ethernet library, so I can't state you are either right nor wrong when you say may be blocking forever, but I believe he can give a try to this. Also, he said the board is blinking with the blink sketch, but I am suggesting to use a LED pulse at the beginning of his
loop()
function as a debug measure.... something you also suggested in the comments.Roberto Lo Giacco– Roberto Lo Giacco2016年09月29日 15:16:53 +00:00Commented Sep 29, 2016 at 15:16
Maybe your laboratory power source is of poor quality. A computer's USB is a good power source. Try using a pack of 4x1.5v AA batteries. I have use this setup with arduino+ethernet shield+cheap servo.
I too have had this problem with a Mega. It was working with my SCADA package, running on linux and you could ping the Mega/ Ethernet board, but then something changed, and it wouldn't run/ read using the ehternet port. Had two units, and both behaved the same.
When connected to the programming laptop ( runing W7) and the ethernet port, and running serial monitor, ethernet functions would work. De powering and repowering using W7 usb port wouldn't work. Tried removing all calls to Serial.print(ln) via if Debug type logic , didn't fix the problem.
Restoring the Serial messages didn't change the behaviour.
The "fix" is to press the reset button on the ethernet board after powering up, power source independent. Giving the stack a general reset via the reset pins doesn't work, it needs to be the reset button on the ethernet module.
Did it via the unit connected to the linux machine and instantly got ping, and the SCADA system reading data.
If anyone can shed more light on the need to reset the ethernet module after power up I would be delighted.
Have anybody ever powering Arduino + Ethernet not from USB?
- yes I have one sitting around plugged into a wall-wart and connected to the (ethernet) network. It certainly works in that configuration.