JSN-SRT04 sensor reading "0" values

Hi all !

I have a problem reading values from the JSN-SRT04 sensor. When I try to read values from the sensor i read :

11:43:09.988 -> Distance: 0 cm
11:43:11.107 -> Distance: 0 cm
11:43:12.197 -> Distance: 0 cm

I'm using an ESP8266 (not arduino, I couldn't find the right tag) and this sensor together. I don't have a lot of equipement to tests things out so i'm pretty much stuck also I'm a beginner in electronics and MCU.

I'm using this code i found on the internet just to test the sensors. I'm understanding how the code works but cannot find what is wrong with what I'm doing :

#define echoPin 4
#define trigPin 5                  

long duration; 
int distance; 

void setup()
{
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);  
  Serial.begin(9600);
  delay(500);
}

void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2); 

  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 


  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0344 / 2; 

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(100);
}

As for the cabling, I'm using a breadboard and connected my setup as follows :

  • Trigger on GPIO5 which is D1 on ESP
  • Echo on GPIO4 which is D2 on ESP
  • Sensor 5v to Vin on ESP connected to my computer on USB 5v too
  • And ground to ground obviously

I have tried another code simpler without unit conversion and tried to test if I receive something other than 0 but nope. I understand that it may be because my trigger part is not receiving the echo but i don't know how to test it further...

Thanks in advance for your responses :smiley:

This will likely devolve into integer math with your distance variable ending up zero.
Try something like
distance = long(float(duration) * 0.0344f / 2.0f);
This forces the compiler to treat everything on the right side of the equation as floating point numbers.

Welcome to the forum

Have you tried using the pin numbers printed on the board rather than the GPIO numbers ?

Hello,
As I said I already tried removing the conversion part and printed directly the duration variable to see if anything is returned from pulseIn() but nope. I tried your solution anyway but it doens't work. I still keep your code because it's a good security to cast to float anyway.

pulseIn() always returns something. I assume you mean it returns 0 in your case?
Do you have a scope?

Do you mean I should define my pins as :

#define echoPin D1
#define trigPin D2

Yes

D1 and D2 are returning errors so I tried 1, 2 but also return 0s. I guess this has to do with my pins. But on the backside of the D1 and D2 is written IO5 and IO4. So i think i should put it that way...

Which board type have you got selected in the IDE ?

Yes PulseIn retun value is 0. But as I switched pins definitions I also got 0s. So I'm thinking it may be my pins mapping. I thought I understood that matter but I guess not. How should I map it then ? I have a pinout mapping on the side of the screen and it looks correct to me...

Generic ESP8266 Module

On ESP32/8266 boards I always use the actual GPIO numbers. Arduino makes IMO a horrible mess out of pin numbering which isn't helped by the proliferation of 3rd party boards with their own logic (or lack thereof). GPIO's as defined by Espressif are in my view the most dependable way of addressing pins.

To make sure you've got the right numbers, attach an LED or a button to the pins you define and test if you get the expected response.

Please post a link the the board that you are using

Unfortunately I don't have LED nor buttons

Is there a reason that you have not set the board in the IDE to match the one that you have ?

Then use a multimeter to see if you can read a pin high/low.

If you have no multimeter, LED or buttons...get some.

No, I thought I selected the corresponding one, mine is an ESP8266, right ?

Yup, I think I will have to buy that anyway to get equipped for next projects :smiley:

1 Like

Yes, your board has an ESP8266 but the board definition files for your exact board may contain different details then the generic one