Distance Measurment not working properly

Hello,

i want want to measure the water level in a 30 cm high 100 l tank. For that I am using JSN - SR04T. In the distance between 20 and 30 cm it measures well. But values below 20 cm are not plausible. It should have a accuracy from 2 cm to 300 cm, so it should be possible to measure here. Here is the code. Maybe someone had the same problem and knows, where´s the mistake? Thanks in advance!

#include <LiquidCrystal_I2C.h> //bib für LCD
#include <Wire.h> 
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices 
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);



LiquidCrystal_I2C lcd(0x27, 16, 2);

int triggerPin = 6;
int echoPin = 5;
int cmDistance, tCorrectedCmDistance;
//int temperature = 0;
unsigned long duration;

// Constans to calculate the water level
const float maxDistance = 30.0; // Maximal Distance in cm
const float minDistance = 0.0;  // Minimal Distance in cm

void setup(){
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  delay(1000);
  lcd.init ();
  lcd.backlight ();
  lcd.setCursor(0, 0);
  lcd.print("Grosser Tank");
  {
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
}
  
}
void loop(){
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH);
 
  // Speed ​​of sound in air: 343m/S (at 20°C)
  // 343.2m/s --> 34.32cm/ms --> 0.03432cm/µs
  // by 2 --> echo
  cmDistance = duration * 0.03432 / 2; 
//  tCorrectedCmDistance = duration *(0.03315 + 0.00006 * temperature)/2;
  Serial.print("Distanz [cm]: ");
  Serial.println(cmDistance);
//  Serial.print("Distance [cm]: ");
//  Serial.println(tCorrectedCmDistance);
// Calculate filling level in %
  
  float fillPercentage = map(cmDistance, 30, 0, 0, 100);
  fillPercentage = constrain(fillPercentage, 0, 100);
 


  delay(1000);
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  float tempC = sensors.getTempCByIndex(0);
  // Check if reading was successful
  if(tempC != DEVICE_DISCONNECTED_C) 
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  } 
  else
  {
    Serial.println("Error: Could not read temperature data");
     
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Wasser:");
  lcd.print(fillPercentage);
  lcd.print("%");

    lcd.setCursor (0,1);
    lcd.print("Temperatur");
lcd.print (tempC);
}

From proto-pic website:

JSN-SR04T Ultrasonic Distance Specifications:

  • Operating voltage: DC 3.0~5.5V
  • Operating current: <8mA
  • Probe frequency: 40kHz
  • Max Range: 600cm
  • Near Range: 20cm
  • Distance accuracy: +-1cm
  • Measuring angle: 75°
  • Operational temperature: -20~70°C
  • Sensor cable length: 2.5m
  • Blind zone: <20cm

So not your code I suspect.

I assume that the sensor is not designed to operate in water but is just "waterproof" so it can be used outside (e.g.)? The speed of sound in water is much greater than in air so the calibration would be quite different.

Since the maximum range is 600 cm why not just move the sensor up so the distance is 20cm when your tank is full?

1 Like

Should be:

float cmDistance, tCorrectedCmDistance;

Thank you for your response! Since this is aquivalent to the HC-SR04 i thought it also works with the same measuring range. But its not accourate at 20 cm and below. I can not use it outside the tank, because i use in a van and the water would come out. So i guess, this is not a good way to measure the filling level.

If you can find an ultrasonic sensor that works under water, place it on the bottom of the tank looking up to bounce off the surface. Harder problem because the echo time is much shorter.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.