Arduino UNO + Ethernet Shield + Ultrasonic Sensor = Fail

It's been 3 hours, still trying to figure out what is wrong :confused:

With my Arduino Uno I measure the distance using HC-SR04 ultrasonic sensor with no problems at all using the wiring below.
When I attach ethernet shield, my ultrasonic sensor does not measure distance any more, it constantly says 0cm no matter what. I have tried different digital pin pairs such as 5-7, 6-8, 5-9 but no luck.

I wonder whether HC-SR04 is not compatible with Ethernet shield or is there something wrong with my ethernet shield's digital pins. I need help :frowning:

P.S. My ethernet shield works fine while running a web server or web client script.

Here is the ethernet shield I have;
http://www.ezshopfun.com/product_info.php?products_id=169

Here is the code;

#define trigPin 6
#define echoPin 7

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH) / 2;
  distance = duration / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  delay(500);
}

Post your code.

SurferTim:
Post your code.

Added to the main post.

Post a link to the ethernet shield you are using.

edit: This code has a problem. The variables distance and duration are both integers. No fractions on integers.

  distance = duration / 29.1;

SurferTim:
Post a link to the ethernet shield you are using.

edit: This code has a problem. The variables distance and duration are both integers. No fractions on integers.

  distance = duration / 29.1;

Nope no problem with the code, same code works for arduino digital pins itself but not for ethernet shield. There is nothing wrong with dividing integers to doubles, it will just truncate the fraction part. Btw, I added the my ethernet shield's link to the original post.

Then the ethernet shield should not affect the sensor, but I have an Arduino ethernet shield, not your model. Nothing on my shield that would affect that code.

Yes exactly, there should be no effect at all. Still same :confused: I get 0 as output, I have tried different extensions on top of arduino uno and my code works well with them.

If you are ok with experimenting, you can try bending the ethernet shield pins you are using for the sensor (D6 and D7) a little so they do not connect to the Uno, then connect the sensor to those pins (D6 and D7) on the Uno. Does it work then?

It could be a power problem. Do you have a SD card in the shield's slot?

I have no SD card in the slot. Actually I don't want to bend the pins but here is what I'll do to test D6, D7 and the other slots; I'll just try those pins with other components such as temperature sensor and a simple LED and see if they work well with them. Will be posting test results.

I don't mind bending pins. I do it all the time. I bend them just enough so they do not insert into the Arduino. But that is just me. :slight_smile:

BTW, you should mention any other devices connected to the Arduino/shield. That could affect the device that is failing.

There is nothing attached to the arduino besides ethernet shield and the ultrasonic sensor itself.

Tested the temperature sensor on D5, D6, D7, D8, D9. It works fine, it gives me the temp. and the humidity.

Now things have gotten weird. Now what ? The ultrasonic sensor and the ethernet shield are not compatible :confused:

Today I bought a multimeter and tested my circuit. Here are the results;

When my circuit directly attached to Arduino itself;
4.80V & 5.7mA

When my circuit attached to ethernet shield;
3.06V & 3.8mA

I think the problem is that 3.06V is not enough for my HC-SR04 to operate.