Problem with JSN SR04T ultrasonic sensor readings

Hi all,

I’m having a weird problem here with my ultrasonic sensors. I’m reading noise off my one of the 4 sensors in my system.
Attached is the code, output and the environment where the system is being tested. The values came out to be within 72 - 120 range where the actual distance is 2.4 to 2.6m

long ds1 = 0;
long ds2 = 0;
long ds3 = 0;
long ds4 = 0;
long duration1, duration2, duration3, duration4;

#define POWER_PIN_1 D4      
#define ECHO_PIN_1 D3       
#define TRIG_PIN_1 D2       
#define ECHO_PIN_2 D1
#define TRIG_PIN_2 D0
#define ECHO_PIN_3 B5
#define TRIG_PIN_3 B4
#define ECHO_PIN_4 A1
#define TRIG_PIN_4 A0
// particle setup function...
void setup() {
    pinMode(POWER_PIN_1, OUTPUT);
    pinMode(TRIG_PIN_1, OUTPUT);
    pinMode(TRIG_PIN_2, OUTPUT);
    pinMode(TRIG_PIN_3, OUTPUT);
    pinMode(TRIG_PIN_4, OUTPUT);
    digitalWrite(POWER_PIN_1, HIGH);;

void loop() {

digitalWrite(TRIG_PIN_1, LOW);  // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_1, LOW);
ds1 = pulseIn(ECHO_PIN_1, HIGH);    // gets duration...
ds1 = microsecondsToCentimeters(ds1);     // gets distance...


digitalWrite(TRIG_PIN_2, LOW);  // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_2, LOW);
ds2 = pulseIn(ECHO_PIN_2, HIGH);    // gets duration...
ds2 = microsecondsToCentimeters(ds2);     // gets distance...


digitalWrite(TRIG_PIN_3, LOW);  // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_3, LOW);
ds3 = pulseIn(ECHO_PIN_3, HIGH);    // gets duration...
ds3 = microsecondsToCentimeters(ds3);     // gets distance...


digitalWrite(TRIG_PIN_4, LOW);  // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(TRIG_PIN_4, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(TRIG_PIN_4, LOW);
ds4 = pulseIn(ECHO_PIN_4, HIGH);    // gets duration...
ds4 = microsecondsToCentimeters(ds4);     // gets distance...

long microsecondsToCentimeters(long microseconds)
  return (microseconds / 58.2);

You might want to first test with one ultrasonic sensor powered up and see if that makes a difference.

Did you miss some Code in the Copy/Paste?

Where are ds1-ds4 defined ?

@Rftop I have missed the copy,paste, will update shortly.

@kennethlimcp the sensor works fine when it’s working with a single ultrasonic sensor. I have tried the following ways to troubleshoot.

  1. The sensor with the problem would be working fine if one of the other sensors are unplugged together with the ultrasonic driver.

  2. If the problematic sensor was plugged out leaving the driver on, with the other 3 sensors untouched, it would be reading noise as shown in the output.

  3. If all 4 sensors are plugged out with their drivers on, the other 3 will have a timeout reading while the first sensor will be reading noise

4)If I changed the USB to be connected to my laptop instead of my PC, the all 4 sensors would have the timeout readings with the drivers on only. The readings would be correct if I have all 4 sensors plugged in.

5)When running on batteries, the output would be the same as 4).

Anyone knows what might be the cause of the problem?

@NST1992, is the USB your only source of power for everything? Can you share a schematic?

It just feels like the ultrasonic sensors are within the range/view of the sound wave sent from one device, ending up being received by another. I would test with two spaced far apart to see what happens first.

1 Like

@peekay123 either the usb or a battery via the Vin port is the only source to power the device.
@kennethlimcp I tried inserting a 2 seconds delay after transmitting and receiving each pulse from individual sensors, I have also put the echo pin to low positions after it has obtained the distance values.

I know this is a long shot, but is there any chance the “faulty” values are always related to the same pair of pins?
For instance: Is it always (ds2) on Pins D0 & D1, no matter where you install the sensors ?

You could add the (“ds1”…2…3…4) to your serial.print and swap the sensors around to test.

@Rftop the faulty values are always the same set of pins. The error would shift over to the next sensor if I connect the driver to a breadboard with jumper wires to the circuit board.

By the way, what do you all think about putting these sensors in waste and oil and gas applications?

@NST1992 , The reason I asked was because I always try to stay away from D0 & D1 pins if I can, since those are used for the i2c Bus. That probably has nothing to do with your issues, but there is a lot about the MCU that I don’t understand.

I’ve used this sensor in a Wastewater application to measure the flow of water over a V-notch weir. I never got comfortable with it measuring the height in an enclosed space (manhole), but other people have.

Didn’t have time to read all the responses, so somebody may have already caught this, but the sensor has about a 20cm minimum distance. You will get erratic readings at a distance shorter than that. I was just discussing the issue in another thread.

@scraphound As shown in the picture, there was nothing blocking the ultrasound sensors pointing to the ceiling. I’m guessing that there may be built up static within the PCB.

You can use a small ceramic capacitor to drain off any spurrious rf. If that doesn’t work, a ferrite choke, known as an antenna balun, should help. Changing the length of the lead wires might also help. Lead wires act as an antenna and will receive a much stronger RF signal at their resonant frequency. For example, in the distant past, telephone hookup wires were typically 10’ long… pretty close resonant at CB radio frequencies… This caused lots of problems.

1 Like

Hey, @NST1992 I don’t see a schematic, but the sensor really works with 5V coming in. It needs 5V to work properly. The Particle pins are 3.3. Are you stepping up the voltage somewhere? I tried using a SparkFun 5V boost converter from 3.3 to 5V, but it didn’t work because the 5V wasn’t coming at the right frequency, but I found this 5V charge pump on aliexpress and it seems to work pretty well. link here

I also think it could be potentially a great application in the waste and gas and oil space. I’d love to talk about it more.