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);
}