💬 Distance Sensor
-
I had to add some lines to the sample sketch to get console printouts:
#include <SoftwareSerial.h>
and in the void setup():
Serial.begin(115200);Is everybody so specialized that they see immediately this omission? :-)
-
I am using a Nano V3.
Apart from the above lines I didn't change anything in the sketch.
Before the change I only got debug messages and no Serial.print lines!Is the SoftwareSerial.h included in one of the other libraries?
-
Found a new distance sensor HC-SR04P .
De spec shows that the input voltage of this model is 3-5V. So this model can be used with a 3.3V arduino.
https://www.aliexpress.com/snapshot/0.html?spm=a2g0s.9042647.6.2.1AslKf&orderId=89862296499774&productId=32711959780 -
Sorry for bumping up an old question but I’m in the same boat of requirement.
And to my surprise there are quite a few new sensors in town:
This one has 2 transducers instead of 1 therefore has less minimum detection distance as compared to JSN SR04T. It is also completely sealed and waterproof.
Ebay spam link removed by moderator
Before finalizing on this I had bought another one which was a waterproof modified version of HC-SR04 from here
Ebay spam link removed by moderator
The 1st one is much more rugged and waterproof as compared to the latter one
-
Sorry for bumping up an old question but I’m in the same boat of requirement.
And to my surprise there are quite a few new sensors in town:
This one has 2 transducers instead of 1 therefore has less minimum detection distance as compared to JSN SR04T. It is also completely sealed and waterproof.
Ebay spam link removed by moderator
Before finalizing on this I had bought another one which was a waterproof modified version of HC-SR04 from here
Ebay spam link removed by moderator
The 1st one is much more rugged and waterproof as compared to the latter one
@ritesh-t Dual sensor types were the first on the market, send on one sensor receive on the other from memory, but could suffer in high water vapour Zones.
The single sensor types switches between send and receive on the same head, commonly used as car parking sensors.The cheaper one you show has exposed diaphragms and says it is water resistant not waterPROOF ;)
Aside the smaller deadband of 3cm the more expensive one used more robust moulded sensors but steep on price, 3 times the JSN single sensor head device, but 20cm deadband is adequate for my use.
Both need 5v supply same as the JSN single sensor type, for a 3.3v Node this adds complication. -
Actually, even when I use this example without any changes with single sensor, quite often node sends out 0cm. Real distance in my office from table top to ceiling is about 160cm. Maximum distance set to 300.
@apl2017 There are many techniques to get avoid bogus or wrong results, assuming there are no environmental effects (reflective side objects to make readings difficult).
On the setup here the sensor is a single transducer, and the sketch is a basic single pulse, time the echo to calculate the distance, with a sufficient delay to ensure the preceding pulse has decayed (hard surface underground tank) - The sketch tests for two consective results within the expected range, if it does not succeed in X attempts it does not update the Gateway and goes back to sleep for the next event, hence not flattening batteries. -
zboblamont . Thank you, I understand all of this except I really don't see in sketch testing of two consecutive results in X attempts. I see that node will wake up and update the distance if new data is different than old data, but, anyway, question was about something different. Why, single sensor node based on this example periodically generates 0 distance? Are there different models of these sensors, so NewPing library needs to be altered?
-
zboblamont . Thank you, I understand all of this except I really don't see in sketch testing of two consecutive results in X attempts. I see that node will wake up and update the distance if new data is different than old data, but, anyway, question was about something different. Why, single sensor node based on this example periodically generates 0 distance? Are there different models of these sensors, so NewPing library needs to be altered?
@apl2017 Verifying the reading is something you have to write into the sketch yourself, nothing complicated. I didn't use a library so no idea what advantage it brings over the simple timing method. Because I only take readings every hour triggered by an RTC, I don't care if it is the same as previous, only that it is valid in which case it is sent in.
Parts of my sketch follow, all fairly self-explanatory, perhaps they are of some use.
Main loop - Call subroutine and test to ensure it lies between two values, up to 10 attempts allowed to get a proper reading.while ((tankdepth<350||tankdepth>1000)&&counter<=10){//10 attempts to get proper range READULTRASONIC(); counter++; }The READULTRASONIC subroutine uses the simple timing method to get the 2 consecutive readings.
void READULTRASONIC(){//JSN-SR04-2.0 //Main loop checks valid range on a defined number of readings tankdepth=0; duration=0; distance=0; test=1; test2=3; // Normal range should lie between 389 and 989 absolutely empty is 1520 while (test!=test2){// Get two consecutive readings digitalWrite(Trigpin, LOW); delayMicroseconds(100); digitalWrite(Trigpin, HIGH); delayMicroseconds(150); digitalWrite(Trigpin, LOW); duration = pulseIn(Echopin, HIGH); distance = duration/5.82;//This is in mm if (test!=distance){ test=distance; distance=0; } else{ test2=distance; } delay(100);///// 500 originally but why??????? } tankdepth=distance; }There are many version of these sensors, neither of the two I trialled reliably ran unless on 5v, which made it bit more complicated for a 3.3v Node.
One operated on softserial from memory, but I couldn't get it to do what I wanted, I settled on the one which allows straightforward timing.When I was first testing these, the first reading would always be a zero for reasons unknown, then solid readings, but the 2 consecutive readings test worked for me.
-
@zboblamont Thanks, you are proposing to filter out wrong reading, I am trying to understand why system periodically sends out 0.
-
@zboblamont Thanks, you are proposing to filter out wrong reading, I am trying to understand why system periodically sends out 0.
@apl2017 Understood, good luck finding out whether environment, hardware, software, in isolation or combination are the cause.
As said previously, aside the leading zero, the arrangement and technique here returns stable readings, the only critical aspect found was need of a solid 5v.
The only anomaly found were a condensation drip on the sensor face at below -10 outside air temp, hence the 10 attempts limitation.
Good luck -
@APL2017 Did you find the reason for this problem of reading periodically 0cm?
I have the same problem with a Arduino Uno connected to a HC-SR04 sensor. (in reality I made two sensors and both have regularly 0cm as output)
I can blindly adapt the software to filter out these strange values, but like you, I prefer to understand why this behaviour? -
@APL2017 Did you find the reason for this problem of reading periodically 0cm?
I have the same problem with a Arduino Uno connected to a HC-SR04 sensor. (in reality I made two sensors and both have regularly 0cm as output)
I can blindly adapt the software to filter out these strange values, but like you, I prefer to understand why this behaviour?Maybe replying to myself : I read just in the sensor example:
int dist = metric?sonar.ping_cm():sonar.ping_in(); Serial.print("Ping: "); Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in");0 = outside set distance range
Good chance that this is the reason... -
@mfalkvidd Should we not adapt the example sketch to include this problem of returning a zero?
I did configure an automatisation in my controller when the distance was less than 10 cm, so it was fired regularly because 0cm was sent as message from the sensor node.
Change following code:if (dist != lastDist) { send(msg.set(dist)); lastDist = dist; }to
if (dist != lastDist && dist != 0) { send(msg.set(dist)); lastDist = dist; }The '0' means here that an error is fired inside the newping library, not that the distance is 0cm.
-
@mfalkvidd Should we not adapt the example sketch to include this problem of returning a zero?
I did configure an automatisation in my controller when the distance was less than 10 cm, so it was fired regularly because 0cm was sent as message from the sensor node.
Change following code:if (dist != lastDist) { send(msg.set(dist)); lastDist = dist; }to
if (dist != lastDist && dist != 0) { send(msg.set(dist)); lastDist = dist; }The '0' means here that an error is fired inside the newping library, not that the distance is 0cm.
-
@mfalkvidd Should we not adapt the example sketch to include this problem of returning a zero?
I did configure an automatisation in my controller when the distance was less than 10 cm, so it was fired regularly because 0cm was sent as message from the sensor node.
Change following code:if (dist != lastDist) { send(msg.set(dist)); lastDist = dist; }to
if (dist != lastDist && dist != 0) { send(msg.set(dist)); lastDist = dist; }The '0' means here that an error is fired inside the newping library, not that the distance is 0cm.