Page 2 of 2
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Fri Mar 13, 2026 1:09 am
by chipfryer27
Hi
One way might be to use the Mapping Function component.
Briefly you enter expected input Min / Max values (e.g. 0-9999) and then the respective output Min / Max values that you want (e.g. 0 - 100). I have probably overcomplicated by trying to explain, but it is a very handy component and very easy to use. This may save you a lot of bother.
Regards
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Fri Mar 13, 2026 2:57 am
by mnfisher
I'm just waiting on some resistors to have a play....
In the UART modes it outputs the length as two bytes - in a 4 byte block (header, datahi, datalo, checksum) - the length is datahi * 256 + datalo
So if we catch the data to an array d[4] it would be d[1] x 256 + d[2]
The result is in mm
The exception is mode 5 (output as a string "12345mm\r\n")...
I've written an outline program - will post the code once tested...
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Fri Mar 13, 2026 5:47 pm
by Matrixv8
Thank you for your help, I intend to use this in the future projects.
Regards,
Pierre
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Mon Mar 16, 2026 9:19 pm
by mnfisher
Hi,
Sorry to take so long getting back to this - a few days away...
So - in the end I just wired a standard resistor (47K) across R19 (for mode 4). Less than elegant.
I then wired to a UART (I used a software port on an Arduino for testing) - and fired off the trigger (1) at 20ms intervals. I connected the UART pins to a logic analyser so I could see the result - and they look okay (for example 0xFF 0x02, 0xE8, 0xE9) - this is the header byte (0xFF) - the distance (0x2E8mm) and the checksum (0xE9 = (0xFF + 0x02 + 0xE8) MOD 256)
To 'catch' the UART output it is really good to use an Rx interrupt (it is difficult to synchronise without) - however - as I was experimenting I used ReceiveByteArray with a 30ms timeout. This worked okay...
A very simple example - It outputs the distance, waits a second and repeats. I use 2 UARTs - one to transmit the data to the user (UART1) and one to communicate with the sensor (UART2 - software).
The Arduino only has one hardware UART - and using this for the sensor would mean that uploading a program wont work without disconnecting the hardware (which is a pain) - using a hardware interrupt for UART receive - would allow such things as asynchronous measurement (send trigger - do something else - receive data) or constant monitoring using the 'auto' mode.
I haven't tested it - but does the variable resistor allow the distance to be 'tuned'?
Martin
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Mon Mar 16, 2026 11:19 pm
by medelec35
Hi Martin.
I’ve tested the Flowcode Software UART (Timer‑based) and can confirm that the RX capture works reliably.
It performs just as well as the microcontroller’s built‑in UART RX interrupt.
The Timed interval component can be used to set the baud rate.
You might already know this but 9999 is not related to any distance, it only shows that if the echo pulse width is larger than a specified period, in this case, greater than 520uS
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Tue Mar 17, 2026 1:05 am
by Matrixv8
Hi Martin, you are amazing, I have done a lot of flowcode over the years but this is the first time I have to read and convert data of this kind from a UART. Great work. I will test it tonight and let you know. This helps me to understand the String manipulation at greater level.
Regards,
Pierre
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Tue Mar 17, 2026 3:05 am
by Matrixv8
Martin, the program works very well. This will further my understanding of Data Arrays. I will now be able to use the conversion block in my programs.
You are always such a great support for us.
Thank you very much and everyone for your great help.
Regards,
Pierre
Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component
Posted: Tue Mar 17, 2026 8:08 am
by mnfisher
Hi Pierre,
Good to hear you have it working. Arrays are very useful - enjoy
I'd be tempted to just use 0 as an invalid reading... then can just do if(.distance) which is slightly more efficient than if (.distance != 9999)
Martin