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?
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
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.
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) ?
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!) ?