Bad Distance Results

Hello there,

Setup:
-Arduino Uno R3
-Ethernet Shield W5500
-Ultrasonic sensor HC-SR04

Problem: I get maximum bad values on the display on the website by IP address. At a distance of about 6cm it gives totally confused values like 0cm or 150cm. The values have no logic and are incoherent.

As soon as I connect the ultrasonic sensor alone and compile a standardscript it gives good and correct results again.

2 approaches: Either the Ethernet Shield sucks so much current that the ultrasonic sensor is underpowered and therefore outputs wrong values. I have no oscilloscope or multimeter here.

or

Error in the program code (especially the HTML part)

Does anyone have ideas or the same problem already?

LG

Code:

#include <Wire.h>       
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x2C, 0xF7, 0xF1, 0x08, 0x3F, 0x23 };  //The mac adress - VOLLSTÄNDIG ANGEPASST
byte ip[] = {192,168,178,70}; //The IP adress - LETZTE ZIFFER ANGEPASST
byte gateway[] = {192, 168, 178, 1}; //The gateway - GLEICH     
byte subnet[] = {255, 255, 255, 0 };   //The subnet-mask - GLEICH

EthernetServer server(80); // EINE FREIE WÄHLEN AUF DER AUCH HTTP LÄUFT

int trigPin=8;
int echoPin=9;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  delay(500);

  digitalWrite(trigPin, LOW);
  digitalWrite(echoPin, LOW);

 Wire.begin(); 
 Serial.begin (9600);    
 Ethernet.begin(mac,ip);    
 server.begin();
}

void loop() {
 EthernetClient client = server.available();

 long duration, distance;
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  distance = (duration/2.0) / 2.91;
        if (client) {                  
server.print("HTTP/1.0 200 OK\r\nServer: arduino\r\nContent-Type: text/html\r\n\r\n");
client.println("<meta http-equiv=\"refresh\" content=\"7\">"); //The website is updated every 7 seconds
server.print("<HTML><HEAD><TITLE>");
server.print("Fuellstandshoehe");
server.print("</TITLE>");
server.print("</HEAD><BODY>");
server.print("<h2 align=center><font size=7><b>Fuellstandshoehe </b></font></h2>");
server.print("<center><font size=5><b>Fuellstandshoehe im Tank betraegt </font>");
server.print( distance);
server.print("cm."); 



        delay(500);                                                                          
       client.stop(); 
        }
  
}

In addition, it seems like the sensor makes weird noises (not like when it usually measures, way more louder and shrill/flashy).

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

It was a suggestion that is has something to do with current. Nevertheless im fine with it as long ppl see the post :smiley:

The IDE doesn't use current. Well, I guess the computer does.

Did you try running the Ethernet example sketches by themselves?

Do you know for sure that the I/O pins you used for the sensor, are not already in use by the Ethernet shield?

Nope, post it and ill let it run without the sensor

As far as i know just 10-14 are the critical ones.
In my built i used 6 for trigger and 7 for echo

The bottom half of the HTML is missing...

<HTML><HEAD><TITLE>
Fuellstandshoehe
</TITLE>
</HEAD><BODY>
<h2 align=center><font size=7><b>Fuellstandshoehe </b></font></h2>
<center><font size=5><b>Fuellstandshoehe im Tank betraegt </font>

... that is to say, the open-center (<center>) needs a close-center(</center>), same with <BODY> and <HTML>

how for example?

Hi.
Not sure to have THE solution, but the PulsIn function is a blocking function. It could interferates with the running HTML function.

I have 2 suggestions in mind:

  • code HTML connexion and especially US measurement in separate functions, maybe one disabling the other (like with interrupts wich can trigger at the same time)
  • Code a US measurement function in a non blocking way. I made it quite easily on an Arduino 33BLE but I used core code so could be non appliable to your config.

Can u give an example how? I did it before but didnt make any diffrence to the posted code

Is there any more to your sketch? Or, I can make the rest up...

@ARahmani

This should be how the HTML portion of your sketch looks.... only two lines added/corrected

  if (client) {
    
    server.print("<!DOCTYPE HTML PUBLIC HTTP/1.0 Content-Type: text/html"> // LINE CORRECTED
    // server.print("HTTP/1.0 200 OK\r\nServer: arduino\r\nContent-Type: text/html\r\n\r\n");
    client.print("<meta http-equiv=\"refresh\" content=\"7\">"); //The website is updated every 7 seconds
    server.print("<HTML><HEAD><TITLE>");
    server.print("Fuellstandshoehe");
    server.print("</TITLE>");
    server.print("</HEAD><BODY>");
    server.print("<h2 align=center><font size=7><b>Fuellstandshoehe </b></font></h2>");
    server.print("<center><font size=5><b>Fuellstandshoehe im Tank betraegt </font>");
    server.print( distance);
    server.print("cm.");
    server.print("</b></font></center></body></html>"); // LINE ADDED

    delay(500);
    client.stop();
  }

I had issues with some "communication" library like Bluetooth, or Wifi, as they used to have "background" functionning and can lead to a totally different behavior of your code.

If you don't need a very high rate of measurement, I would probably write a wifi function wich:

  • enable the wifi;
  • connect to the server / access point;
  • do the communication;
  • disable the wifi.

then I would play my US measurement, store the result in order to be send by wifi at the next loop occurence.

EDIT: did you just try to send the distance as an integer rather than a float (with point or coma in the number) ?

No that was all.

no but i can try it

there is no float i was using. The measurement results given in full digits without coma.

Hi,
Are the two pins used by the ultrasonic also used by the ethernet shield?
Have you checked?

Can you please post link to spec/data of the ethernet shield?

Can I suggest you use the "NewPing" library to do ALL your ultrasonic work?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi Tom

as you can see on the picture above in my first post i just connected all cables to the ethernet shield pins, so yes

As you can see in my code i didnt include Newping.h, so no. Actually i just installed the lib now but need to adjust the code then.

Im pretty sure that it has something to do with the current distribution at the device. The sensor gets so loud after powering it through the ethernet shield 5V pin, that u can clearly hear it.

if u let the example script running for the HC-SR04, for example this one:

// defines pins numbers
const int trigPin=14;
const int echoPin=15;
// defines variables
long duration;
int distance;
void setup() {
  Serial.begin(9600); // Starts the serial communication
  pinMode(trigPin,OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin,INPUT); // Sets the echoPin as an Input
}
void loop(){
  // Clears the trigPin
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration=pulseIn(echoPin,HIGH);
  // Calculating the distance
  distance=duration*0.034/2; //mit c=340m/s geteilt durch 10k : Schallgeschwindigkeit normale atmosphäre und durch 2
  // Prints the distance on the Serial Monitor
  Serial.print("Distance:");
  Serial.print(distance);
  Serial.println("cm");
}

you see, that the sensor running quite and calm (as it should do) and just here the ultrasonic tone if u put ur ear right enxt to the emitter. There should be the problem i got for the bad distance values.

yeah something's wrong here. Maybe you are a kind of a bat, a dolphin or a superman, but the emitting frequency is twice higher than our max audible frequency (roughly 20kHz versus 40kHz for the transducer) AND the pulse train is too short to trigger a detection by human hears.

If it is a power issue related to the shield, it won't be easy to solve!
A chance you have other card model on a bench, like 33IOT or ESP as they can directly connect to internet (and properly deal with a HC-SR04 at the same time!) ?

just heard the diffrence to the normal tone. Common human sense and standard hearing is quite sufficient @GrandPete

Actually i thought the same and ordered a ESP-8266. Im curious if the problem gonna be solved or still be the same. Ill let you know when i tested.