Ultrasonic sensor JSN-SR04T to Electron


#1

I want to measure the approximate volume to determine the percentage of water in a water tank. I researched a little and I think the most economical solution for my case is a waterproof ultrasonic sensor. It seems that this one especially works in voltages between 3-5V without problem according to the specifications of the seller. Since I intend to use the electron with just battery.

I only need to read the water level a couple of times a day to determine if it is necessary to fill it or not.

https://www.aliexpress.com/store/product/Integrated-Ultrasonic-Module-Distance-Measuring-Sensor-Module-Reversing-Radar-Waterproof/1089303_32312190912.html

My questions are @KyleG :

Is it possible to directly connect this device to the Electron?

Would you need additional components?

Is there a library to access it from code?

I would appreciate your comments. Thanks


#2

If the volume / dimensions of the tank are known, could you use a water level sensor to achieve the same goal, or make a digital switch? This would be a lot simpler.


#3

Hi @nrobinson2000 looks nice and easy, If I understood the diagram well, just dip a cable of D0 and another GND to detect a specific water level? Are there any components to avoid damage to the electron? If I need two more levels, would I just use another pin of the electron maybe D1 and D2?

Thanks


#4

As long as the water is sealed away from the Electron and there won’t be any other electrical sources going into the water, I think the Electron should be safe. You should be able to use multiple pins for different levels.


#5

Another option which sould work is a Laser Distance sensor to measure the water level from the top of the bucket.


#6

Thanks @nrobinson2000, I’ll try, looks promising :grin:. Thanks @RWB for the tip, although I believe my tank is too deep more than 2 meters. Although it might be useful in a next project :relieved:


#7

Let me ping someone that might know the answers, @rickkas7 are you able to help?


#8

It should be easy to interface the JSN-SR04T to the Photon/Electron. It can be powered off the 3V3, you trigger it with a GPIO pulse, and measure the pulse that comes back. The device looks handy, especially since it’s weatherproof and inexpensive. I just ordered several, but of course it could be a while before they arrive.


#9

I have some code for this sensor that I hacked together from a few sources. I’ll try to dig it up later tonight. I do remember that it seemed to work.


#10

This is what I’ve used in the past.
I just dug the sensor out of my stash and tested the code.
I didn’t write this, and I’m not a programmer…but this should get you started.

/*
Code for Cheap UltraSonic JSN SR04T with PHOTON.
Attach TRIG to D6, ECHO to D7, 5v to VIN, Gnd to Gnd.

Minimum Range is about 11", Max Range is aoout 10 Feet.  Still Testing

*/

volatile int StartMeasurement = D6;
volatile int EchoPin = D7;
volatile unsigned long startPulse = 0;
volatile unsigned long endPulse = 0;

int attemptDistanceMeasurementOnce(void);
int attemptDistanceUntilSuccess(void);
void ultrasonicISR(void);
double AverageDistance ;

void setup() {
    pinMode(StartMeasurement, OUTPUT);
    pinMode(EchoPin, INPUT);
    Serial.begin(9600);
}

void loop() {
    int duration = 0;
    float distanceInches = 0.0;
    
    // get a distance measurement (might have to retry a few times -- hardware has been inconsistent)
    duration = attemptDistanceUntilSuccess();

    Serial.print("Duration in microseconds: ");
    Serial.println(duration); 
    
    // empirical conversion, your sensor may be different !
    distanceInches = duration / 127.000;
    
    AverageDistance = (0.95 * AverageDistance) + (0.05 * distanceInches);  // 20 shot rolling average
    
    Serial.print("Distance in inches: ");
    Serial.println(distanceInches); 
    Serial.println(" "); 
    
    Serial.print("Average in inches: ");
    Serial.println(AverageDistance); 
    Serial.println(" "); 

    // make a new measurement about 4 times per second
    delay(250);
}


int attemptDistanceUntilSuccess()
{
    int duration;
    int attempts = 1;
    
    while(attempts < 10) {
        duration = attemptDistanceMeasurementOnce();
        if(duration > 0 && duration < 60000) {
            break;
        }
        // wait a short time before attempting again -- the primary failure mode is very slow - 187 ms echo pulse
        delay(200);
    }
    
    return duration;
    
}

int attemptDistanceMeasurementOnce(void)
{
    int duration;
    
    endPulse = startPulse = 0;
    attachInterrupt(EchoPin, ultrasonicISR, CHANGE);
    
    // pulse the sensor to make it get a distance reading
    digitalWrite(StartMeasurement, HIGH);
    delay(5);
    digitalWrite(StartMeasurement, LOW);
    
    // wait while we get both up and down edges of pulse (and interrupts)
    // the interrupt service routine sets our start and end "times" in microseconds
    delay(20);
    duration = endPulse - startPulse;
    
    detachInterrupt(EchoPin);
    return duration;
}

void ultrasonicISR(void)
{
    if(digitalRead(EchoPin) == HIGH)
        startPulse = micros();
    else
        endPulse = micros();
}

#11

Looks promising, I’ll buy the sensor, it will take a month to get to my country. In the meantime I will try the advice of @nrobinson2000 In my case the consumption of battery is critical for my project. Thanks


#12

@nrobinson2000 Based on your suggestion, this can save many pins. But I think it must be analog sensor, right?. And excuse me, my knowledge of electronics is very basic.


#13

FWIW, I’ve tried using ToF sensors (including this exact one, and others) to measure liquid level. The liquid being measured was completely clear, and as a result, I was not able to get accurate readings (presumably because the surface of the liquid was not a “reliable” reflector). I ended up abandoning this approach and went with a high-frequency (2MHz) ultrasonic sensor (mounting to the bottom of the liquid container, pointed up), which works perfectly.


#14

Good to know.

Did you try with dark liquids also just to see if it made a difference?


#15

No, my application was only for clear liquids, so I didn’t do that experiment. But I will say that I was generally impressed with the ToF sensors when the reflection target was “solid” (i.e. piece of paper, plastic object, hand, etc.)


#16

Hi @Eric24, Could you share the device model you used for your solution?, In my case it is a tank of water of 2 meters of depth. Thanks


#17

Yes, I tried both VL6180X and VL53L0X. The VL53L0X is capable of 2 meters’ sensing, but I had problem with both when the reflecting surface was clear liquid.


#18

I’ve been trying to find a low-cost reliable solution to track residential heating fuel usage over time. Keeping track of the amount of fuel in the tank is one of my options. I researched this a bit and found a number of commercial products using ultrasonic fuel level sensors. Ultrasonic level sensing, which you discuss above, appears to be the best modern-day solution available.

I ordered some of the JSN-SR04T sensors and look forward to receiving them in a couple of weeks. I’m going to add tank level sensing to a particle photon application I’ve been working on for a month or so. I’ll keep you folks posted when I find out more.


#19

I did something similar. I used a JSN-SR04T sensor epoxied into a reducing bushing on the top of the fuel tank. Accuracy is fine once the level drops below the sensor’s minimum sensing distance. I wish there was a comparable sensor with a shorter minimum, but I think the problem is that the transducer has to be switched between transmitting and receiving, and there’s some minimum amount of settle time before it can start receiving. The ultrasonic sensors with separate transmitter and receiver can detect shorter distances, but I haven’t come across any that would mount so well and be as resilient.


#20

I appreciate your pointing out an issue I’m going to run into in advance. I’m measuring the fuel level in a 200 gallon tank that’s basically a cylinder on its side. I’ll be mounting the sensor on the underside of an nylon screw-in plug that’s on top of the tank. I’m guessing the tank will almost never be full within 20cm of the sensor (so close proximity sensing shouldn’t be a problem). I’m more concerned about the effects of long-term exposure to diesel fuel fumes and about the sensor’s performance in low temperatures (down to 30 below zero fahrenheit). I’m still waiting for my order to arrive from China. I’ll keep everybody posted on my progress once they arrive.