Problem with JSN SR04T ultrasonic sensor readings


#1

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(ECHO_PIN_1, INPUT_PULLDOWN);
    pinMode(TRIG_PIN_2, OUTPUT);
    pinMode(ECHO_PIN_2, INPUT_PULLDOWN);
    pinMode(TRIG_PIN_3, OUTPUT);
    pinMode(ECHO_PIN_3, INPUT_PULLDOWN);
    pinMode(TRIG_PIN_4, OUTPUT);
    pinMode(ECHO_PIN_4, INPUT_PULLDOWN);
    
    Serial.begin(9600);
    
    digitalWrite(POWER_PIN_1, HIGH);
 
    Cellular.off();
}

void loop() {
    
delay(500); 

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...
Serial.println(ds1);

delay(500);

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...
Serial.println(ds2);

delay(500);

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...
Serial.println(ds3);

delay(500);

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...
Serial.println(ds4);
}

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


#2

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


#3

Did you miss some Code in the Copy/Paste?

Where are ds1-ds4 defined ?


#4

@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?


#5

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


#6

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.


#7

@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.


#8

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.


#9

@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.


#10

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


#11

@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.


#12

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.


#13

@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.


#14

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.