AJ-SR04M Ultrasonic Module

Hi
Hi,

I’m working on a project with an ultrasonic module that I’d like to have a range from 20 cm to 400 cm. I find that the normal HC-SR04 module just doesn’t seem to work with this range. I am using an AJ_SR04M module; however, the Xod code will not work with this module but works fine with the HC module with no changes. Using a small Arduino IDE sketch, both modules will display the distance, but again the HC module comes up short. I like to use the XOD IDE as I am not a very good C++ coder, and if I need to adjust a bit of code, XOD is king. If there is any way to tweak the “xod-dev/hc-sro4” code to have the AJ module work with the XOD IDE, I would really appreciate some guidance. I did do a search on this forum and found one person that had a similar module but did not find any resolution.

Best regards,

What happens when using the existing module with AJ hardware? The numbers seem to be incorrect? It just returns random garbage? If the distances returned are proportional to actual distances, you may just need to perform some math, similar to converting Fahrenheit to Celsius.

Have you compared the specs for the two boards? It is possible one requires Trig to be high & dropped low to trigger while the other requires it to be low and go high to trigger or similar difference with Echo pin. This would be fairly easy to switch in code (or by adding hardware to reverse the pins).

Hi gweim
Thanks for the reply
“What happens when using the existing module with AJ hardware?”
The two modules will not interconnect with each other as the AJ has a 2 wire umbilical to the actual sensor.

“The numbers seem to be incorrect? It just returns random garbage?”
I am not familiar with how to see what is returned from the echo line, whether it is sourcing or sinking the returned logic.

“If the distances returned are proportional to actual distances, you may just need to perform some math, similar to converting Fahrenheit to Celsius.”

“Have you compared the specs for the two boards?
I have not really found the proper specs for the AJ module yet, I am a little confused as the arduino ide works with both modules with no change in code or wiring.
It is possible one requires Trig to be high & dropped low to trigger while the other requires it to be low and go high to trigger or similar difference with Echo pin. This would be fairly easy to switch in code (or by adding hardware to reverse the pins).”
As I mentioned I am not good with C++ but I am learning with trial and error, lots of errors.
this is a screenshot for what it is worth.

Do you get any details about error if you click on the node or view the output window below the workspace?

Unfortunately I do not get an error in the output window. There does seem to be an error in the pin node. It uploads normal then a second later goes into an error.
There is as on board led that pulses at a 1 second rate with the arduino sketch and flicker rapidly with the XOD program, I think indicates the ping signal. Any specs I found for this AJ module indicates that it should be interchangeable HC module.
Thanks

You can try changing the UPD pin on the ultrasonic module from loop to the output of a clock node that ticks every 10 seconds. Ultrasonic modules become unstable if they try to send out pings too close together. If that works, you can try reducing the time between pings.

I tried you suggestion, got the same error. I seen another Library for the HC-SR04 " gweirner/hc-sr04-ultrasonic" , “example” thought I would give that a go. The example code wouldn’t compile had an error with " static const uint32_t HCSR04_COOLDOWN_MS ". So I looked at the difference between the example codes from each library. I modified the code using lines from one for the other and adding the math line, now the example code compiles and doesn’t give any errors when compiling or running. It only gives one reading with either of the modules. Not sure if this was a step in the right direction or not. In the photo below I commented out some of the original code and added code below it. The other screen shot shows the result with no errors.

routetrip timeout might be too short, so it always times out early. Not sure why you multiple by 2 & divide by 2…what you commented out had 100010004*2/340, so new number is about 1000 less

Good catch, not sure why I left that there. I changed all the “constexpr” to “static const” and left the calculation line as original, now it actually works with the AJ module. One step in the right direction. It is fairly accurate up to 2 meters, then not so accurate after. In the screen shots left hand side is at 31 cm which is correct, the right is over 2 meters. This is with the " gweirner/hc-sr04-ultrasonic" library as the xod library doesn’t work

Sounds like I need to update my library :slight_smile: Thanks for the feedback/followup.

I’ve made the var declaration changes on my library if you want to try it instead of your custom code.

I will absolutely try it, and thanks for the help, I really appreciate it.