Hello, I am trying to measure distance using Jsn-sr04t v3.0 waterproof ultrasonic sensor. But the problem is I can't measure distance between 35cm to 50cm. Iam getting random values. However, I can measure distance from 20cm to 35cm and 50cm to 400cm. I tried mode 0 to 3. Does anyone encounter this problem?
Post the code you used for each mode! A wiring diagram would help too! Also post a sample output you get in the mentioned problem range.
/*
JSN-SR04T-V3.0 Ultrasonic Sensor - Mode 0 Demo
srt04-mode0.ino
Uses JSN-SR04T-V3.0 Ultrasonic Sensor
Displays on Serial Monitor
Mode 0 is default mode with no jumpers or resistors (emulates HC-SR04)
DroneBot Workshop 2021
https://dronebotworkshop.com
*/
// Define connections to sensor
#define TRIGPIN 11
#define ECHOPIN 10
// Floats to calculate distance
float duration, distance;
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set pinmodes for sensor connections
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop() {
// Set the trigger pin LOW for 2uS
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 20us to send pulse
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);
// Return the trigger pin to LOW
digitalWrite(TRIGPIN, LOW);
// Measure the width of the incoming pulse
duration = pulseIn(ECHOPIN, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
// Divide by 1000 as we want millimeters
distance = (duration / 2) * 0.343;
// Print result to serial monitor
Serial.print("distance: ");
Serial.print(distance);
Serial.println(" mm");
// Delay before repeating measurement
delay(100);
}
Here is the code I used for mode 0. Iam powering the sensor with 5v. And trigpin is connected to digital pin 11 and echo to digital pin 10. Below is the scree
nshot of the values measured at the distance of 45cm.
Btw, I have a another Jsn-sr04t board which works fine. I think it's v2.0.
And what code did you use for the other modes? I would have expected the serial modes to actually work better.
What kind of Arduino are you working with?
Code i used for mode 1
JSN-SR04T-V3.0 Ultrasonic Sensor - Mode 1 Demo
srt04-mode1.ino
Uses JSN-SR04T-V3.0 Ultrasonic Sensor
Displays on Serial Monitor
Mode 1 is set by bridging "M1" pads on board
Also works with A02YYUW Ultrasonic Sensor
DroneBot Workshop 2021
https://dronebotworkshop.com
*/
// Include the Software Serial library
#include <SoftwareSerial.h>
// Define connections to sensor
int pinRX = 10;
int pinTX = 11;
// Array to store incoming serial data
unsigned char data_buffer[4] = {0};
// Integer to store distance
int distance = 0;
// Variable to hold checksum
unsigned char CS;
// Object to represent software serial port
SoftwareSerial mySerial(pinRX, pinTX);
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set up software serial port
mySerial.begin(9600);
}
void loop() {
// Run if data available
if (mySerial.available() > 0) {
delay(4);
// Check for packet header character 0xff
if (mySerial.read() == 0xff) {
// Insert header into array
data_buffer[0] = 0xff;
// Read remaining 3 characters of data and insert into array
for (int i = 1; i < 4; i++) {
data_buffer[i] = mySerial.read();
}
//Compute checksum
CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
// If checksum is valid compose distance from data
if (data_buffer[3] == CS) {
distance = (data_buffer[1] << 8) + data_buffer[2];
// Print to serial monitor
Serial.print("distance: ");
Serial.print(distance);
Serial.println(" mm");
}
}
}
}
Iam using an arduino uno board.
And you got the same results using this code? In that case I would also subject the hardware of not working correctly. I don't have a V3.0 device so I cannot check myself.
Yeah, I got the same result.
Hello, I like to Inform that the sensor now works perfectly. Now I'm powering the sensor and Arduino with an external 5v power source. Although, In mode 0 sometimes it returns 0 values. In mode 1 it works without any errors.
That seems a faulty device, very unusual behaviour,but did u try to measure speed of object with that sensor?
I didn't try to measure speed of object. But using an external power supply fixed the issue. And it also works without any issues when I connected with esp8266 powered with 3.3v.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.