Help with hr-sr04 and jsn-sr04t

Hi to all, Im working in my terrarium control project. I want measure the water pump level, using this sketch:

 /*
Cayenne Ethernet Example
This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100
and runs the main communication loop.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 9 // LED 
#define VIRTUAL_PIN V3


// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xbitpta40p";

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop()
{
  Cayenne.run();


  digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(50);  
}

CAYENNE_OUT(VIRTUAL_PIN)
{
  Cayenne.virtualWrite(VIRTUAL_PIN, distance);
}

I tried it with jsn-sr04t module with no results, but it work fine with hr-sr04. The only drawback is that it gives me the result in inches and I would like to see it in centimeters.
I would prefer to use the jsn-sr04t module as it is waterproof
Any suggestions?

I don’t have this sensor to test, but if you can share some code that does work for you maybe we can try to combine the two sketches into something that works for you.

1 Like

I’d try using the NewPing library. I have used this library before and works well and you can easily get the measurement in any format you want. It should also support that other jsn-sr04t sensor I believe since it uses a trigger/echo style communication same as the.

NewPing Library

2 Likes

the code was. in my first post…

Ill try thk!

Hmm…guess I must have missed something. I thought the code you posted there only worked with the hr-sr04 sensor and not the jsn-sr04t sensor?

Sorry i didnt understand, i havent these code, but i search one and them will post

Thank you very much

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 for water level sensing:

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.

Before finalizing on this I had bought another one which was a waterproof modified version of HC-SR04 from here

The 1st one is much more rugged and waterproof as compared to the latter one.

2 Likes

@ritesht thanks for sharing this.

@shramik_salgaonkar
That user is promoting their own product. They have posted the same links in multiple places on the MySensors forum.

thanks @mfalkvidd for reporting it. but seems to be helpful for a-lot of user who want a water resistant sensor.

what would be the maximum cable lenght usable for this sensor ? what if i have a tank on the roof at the 4th floor, can i run a cable to the ground and use the arduino to read out the values ?

also UART or PWM, which one to choose ? It it difference between analog and digital reading ?

it is not advisable to have a long length cable. you can have the arduino and the sensor on the 4th floor and connect it to the wifi.

this depends on what type of communication the sensor is using.