jsn sr04t v 2.0 not stable, im noob here, help me quickly

can anybody help me how to make a stable values on ultrasonic sensor JSN-SR04T v2.0? pelase i need ur help

What have you tried?

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

im making a distance measurement on a car prototype at the back. but, in the LCD, it give me a random values about the distance. any advice how to make it stable and correct distance.

here’s my code

#include <TFT.h>  // Arduino LCD library 
#include <SPI.h>

// pin definition for the Uno // 
#define cs   10
#define dc   9
#define rst  8


// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);   

char sensorPrintout[4];

#define trigPin 2
#define echoPin 3

long duration; int distance;
int realdistance;

void setup() {

   // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin(); 

  // clear the screen with a black background
  TFTscreen.background(255, 105, 65);

  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Sensor Value: \n ", 0, 0);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(3);
  
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
}

void loop() {
  
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; 

  readldistance = (distance + 0.6035) / 0.9639; //calibrate

  Serial.println(distance);
  String sensorVal = String(realdistance);
  sensorVal.toCharArray(sensorPrintout, 4);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.text(sensorPrintout, 0, 25);
  TFTscreen.text("Cm", 75, 25);
  delay(250);
  TFTscreen.stroke(255, 105, 65);
  TFTscreen.text(sensorPrintout, 0, 25);
}

or what i have to do with the hardware of the jsn-sr04t v2.0?

Remove the first instance of

digitalWrite(trigPin, LOW);

As it is sending a pulse that will interfere with the one you are trying to measure.

Both distance and realdistance are defined as int, but, you expect them to be float in the calculations. Herb

Grumpy_Mike: Remove the first instance of

digitalWrite(trigPin, LOW);

As it is sending a pulse that will interfere with the one you are trying to measure.

so how about the delaymicrosecond(2);?

(2) could be a problem: Notes and Warnings This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.

herbschwarz: Both distance and realdistance are defined as int, but, you expect them to be float in the calculations. Herb

so what i have to do is change int to float?

so what i have to do is change int to float?

cast them as a float in the calculations. ( look up C's cast operation )

so how about the delaymicrosecond(2);?

It it totally irrelevant so remove it or keep it in you will not notice any difference. But it is useless.

Grumpy_Mike: cast them as a float in the calculations. ( look up C's cast operation )

It it totally irrelevant so remove it or keep it in you will not notice any difference. But it is useless.

I'll try it bro. thanks

Grumpy_Mike: cast them as a float in the calculations. ( look up C's cast operation )

It it totally irrelevant so remove it or keep it in you will not notice any difference. But it is useless.

hey sir, i wanna ask again. the truly is, i put 2 sensors jsn-sr04t v.20 in my prototype, like a real car, have 2 sensors in the back.

the sollution that u gave, only work for 1 sensor.

if i put 2 sensors, 1 sensor give me correct distance, the other one just give random value

for the record, the jsn-sr04t have Measuring angle about 75 degree

does it matter if i put the sensor close enough about 8cm?

So you need to post the code you have used for two sensors.

You need them to be read one after the other and you need a delay between reading the two sensors so that acoustically things settle down before reading an other one.

Note this means a delay before calling the second one for a reading and another delay after the second reading. You will have to experiment with the delay values to see what you can get away with. I would start off with about a second and then adjust it until you have the minimum delay for your setup.

Grumpy_Mike: So you need to post the code you have used for two sensors.

You need them to be read one after the other and you need a delay between reading the two sensors so that acoustically things settle down before reading an other one.

Note this means a delay before calling the second one for a reading and another delay after the second reading. You will have to experiment with the delay values to see what you can get away with. I would start off with about a second and then adjust it until you have the minimum delay for your setup.

here's the code

void setup() {  
  TFTscreen.begin(); 

  // clear the screen with a blue background
  TFTscreen.background(255, 105, 65);

  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Sensor 1: \n ", 0, 0);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(3);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.setTextSize(2);
  TFTscreen.text("Sensor 2: \n ", 0, 70);
  TFTscreen.setTextSize(3);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  
}

void loop() {
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034) / 2;  
  realdistance = (distance + 0.6035) / 0.9639; 
  
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 * 0.034) / 2;  
  realdistance2 = (distance2 + 0.6035) / 0.9639; 

  String sensorVal = String(realdistance);
  String sensorVal2 = String(realdistance2);
  sensorVal.toCharArray(sensorPrintout, 4);
  sensorVal2.toCharArray(sensorPrintout2, 4);
   // set the font color
  TFTscreen.stroke(255, 255, 255);
  // print the sensor value
  TFTscreen.text(sensorPrintout, 0, 25);
  TFTscreen.text("Cm", 120, 25);
  TFTscreen.text(sensorPrintout2, 0, 95);
  TFTscreen.text("Cm", 75, 95);
  // wait for a moment
  delay(250);
  // erase the text you just wrote
  TFTscreen.stroke(255, 105, 65);
  TFTscreen.text(sensorPrintout, 0, 25);
  TFTscreen.text(sensorPrintout2, 0, 95);
}

as u can see, the lcd displays the result of both sensor measurement

That code is not complete. And thereis no delay between sensor readings like was suggested earlier.

@Zemm

DO NOT CROSS POST !

Duplicates DELETED !

Any further scatter gun of posts will result in a timeout.

Now READ THIS before you get into any further trouble.

Bob.

@zemm The need to cross post shows you do not like the answers you have received so far.

It is not like when you were a kid and asked your mom for some candy and she said no so you asked your dad hoping for a different answer.

Grownup life is not like that in the technical world.

First of all many people are telling you the same thing. There is probably more things wrong with your code than you imagine so different posters will not get all of them. Then most of the people capable of answering such questions here read many forum sections so you don’t get more people to look at your problem by cross posting.

It seems like you are not understanding the answers you are getting, maybe they are going over your head. If this is so then say so and we can explain things more simply. Try and say specifically what you don’t understand.

We need to see all your code each time you change things and still have problems otherwise we can’t check on our own machines that you have applied our advice correctly.