JSN-SR04 callback confusion

Hello—we are using a JSN-SR04 distance sensor and have followed to the best of our ability @rickkas7's fantastic documentation (GitHub - rickkas7/JSN-SR04_Gen3_RK: Library for JSN-SR04 ultrasonic distance sensor for Particle Gen 3 devices). The example script provided in that repo works flawlessly.

However, in our application, we're trying to log a bunch of different sensor data, including the JSN-SR04, to a microSD card, and we're receiving a result of 0.00 about 95% of the time when we request result.cm() (deriving from distanceCallback(JSN_SR04_Gen3::DistanceResult result)) in our datalogging script.

I imagine the problem stems from my poor understanding of how to access a result of the callback function. We are running distance.loop() within void loop() and passing the callback function's result.cm() to a global that we call in the datalogging portion of the script.

I should note that we're running it on a Boron, we've added an external boost converter to give the sensor its desired 5 V, and we're level shifting the response down to 3.3 V back into the Boron. It's certainly possible that there's a hardware/wiring issue, though the example script from the provided repo worked fine, so I suspect it's a firmware issue. I wouldn't expect anyone to read through our firmware to troubleshoot for us to that extent, but the full .ino file is here.

Is anyone able to offer advice regarding how to access the callback function's result.cm() outside of that callback function such that we can log it and/or do some simple math with the result?

It's not clear why that's happening, however:

The distanceCallback should be void, not return a float

Add the SerialLogHandler logHandler back in and see if you're getting the distance log messages from the callback, or are getting an error.

You shouldn't mix Log.info and Serial.print, you should switch everything to use Log.info, Log.trace, etc.

If your sensor is returning 0 in some cases, you may want to only save the result to depth_cm when it's non-zero, so at least you'll have the last good distance saved.

1 Like

Thanks, @rickkas7, much appreciated. I added the logHandler back in. Not sure how we lost that, but it's back. I also changed the distanceCallback back to void. The change to returning a float was an experiment in how to access the result when needed.

Those updates seem to be an improvement, but still getting lots of zeros (responses below, including other output).

I still need to switch all Serial.print statements to Log.info. Next step will be to save only nonzero responses.

3 Responses in Serial Monitor:
Time: 23:33:09.000
Date: 11/30/2023
Fix: 1 quality: 1
736
0.59 volts
9.30 degrees C
Location: 3408.4885N, 7752.0127W
23113003.csv
0000050673 [app] INFO: cm=0.000000 inch=0.000000
0000051173 [app] INFO: cm=82.297000 inch=32.400411

Time: 23:33:10.000
Date: 11/30/2023
Fix: 1 quality: 1
739
0.60 volts
9.54 degrees C
Location: 3408.4885N, 7752.0127W
23113003.csv
0000051673 [app] INFO: cm=0.000000 inch=0.000000
0000052173 [app] INFO: cm=60.809000 inch=23.940564

Time: 23:33:11.000
Date: 11/30/2023
Fix: 1 quality: 1
748
0.60 volts
10.26 degrees C
Location: 3408.4885N, 7752.0127W
23113003.csv
0000052673 [app] INFO: cm=52.955000 inch=20.848436
0000053173 [app] INFO: cm=48.297000 inch=19.014577

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.