JSN-SR04 ultrasonic sensor

I have a JSN-SR04 sensor I just purchased that I suspect is bad. Can anyone tell me if it is the same as the HC-SR04 except they are waterproof? I am able to get firmware to work with the HC-SR04 but not the JSN-SR04.

@jstobaugh, it’s possible that the JSN-SR04 needs +5V and gives a +5V output which explains the issue.

I recall that the HC-SR04 has two versions and one is not 3v3 compatible.

If you have an 5V arduino, you can test it to see if it damaged.

The ports I am using are 5 volt tolerant but it does give me something to look at as the output may be to low.

You might need to power it using 5v/Vin?

I am using the 5 v/Vin but since it is a little lower voltage i am going to supply it with 5 volts and run the trigger Photon output thru a transistor tied to 5 volts and see if that works.

What causes you to suspect it’s bad?

They key difference between the two is that the HC-SR04 has both a transmitter and receiver. The JSN-SR04 has a single component that switches between transmitting and receiving. This means it has a dead zone or blind within 25cm of the device because the time required to switch modes.

The readings are random between 20 to 30 cm no matter the distance I have ordered another from a different place to try. The device was shipped with minimal packaging.

You can flash the code Below to know if your sensor is defective.

Connect:
TRIG to D6
ECHO to D7
5v to VIN
Gnd to Gnd.

CODE ```cpp /* Code for Cheap UltraSonic JSN SR04T with PHOTON. Attach TRIG to D6, ECHO to D7, 5v to VIN, Gnd to Gnd.

Minimum Range is about 11", Max Range is aoout 10 Feet. Still Testing

*/

volatile int StartMeasurement = D6;
volatile int EchoPin = D7;
volatile unsigned long startPulse = 0;
volatile unsigned long endPulse = 0;

int attemptDistanceMeasurementOnce(void);
int attemptDistanceUntilSuccess(void);
void ultrasonicISR(void);
double AverageDistance ;

void setup() {
pinMode(StartMeasurement, OUTPUT);
pinMode(EchoPin, INPUT);
Serial.begin(9600);
}

void loop() {
int duration = 0;
float distanceInches = 0.0;

// get a distance measurement (might have to retry a few times -- hardware has been inconsistent)
duration = attemptDistanceUntilSuccess();

Serial.print("Duration in microseconds: ");
Serial.println(duration); 

// empirical conversion, your sensor may be different !
distanceInches = duration / 127.000;

AverageDistance = (0.95 * AverageDistance) + (0.05 * distanceInches);  // 20 shot rolling average

Serial.print("Distance in inches: ");
Serial.println(distanceInches); 
Serial.println(" "); 

Serial.print("Average in inches: ");
Serial.println(AverageDistance); 
Serial.println(" "); 

// make a new measurement about 4 times per second
delay(250);

}

int attemptDistanceUntilSuccess()
{
int duration;
int attempts = 1;

while(attempts < 10) {
    duration = attemptDistanceMeasurementOnce();
    if(duration > 0 && duration < 60000) {
        break;
    }
    // wait a short time before attempting again -- the primary failure mode is very slow - 187 ms echo pulse
    delay(200);
}

return duration;

}

int attemptDistanceMeasurementOnce(void)
{
int duration;

endPulse = startPulse = 0;
attachInterrupt(EchoPin, ultrasonicISR, CHANGE);

// pulse the sensor to make it get a distance reading
digitalWrite(StartMeasurement, HIGH);
delay(5);
digitalWrite(StartMeasurement, LOW);

// wait while we get both up and down edges of pulse (and interrupts)
// the interrupt service routine sets our start and end "times" in microseconds
delay(20);
duration = endPulse - startPulse;

detachInterrupt(EchoPin);
return duration;

}

void ultrasonicISR(void)
{
if(digitalRead(EchoPin) == HIGH)
startPulse = micros();
else
endPulse = micros();
}

</details>

Thanks very much. I will give it a try.