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