AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Use this section to discuss your embedded Flowcode projects.
chipfryer27
Valued Contributor
Posts: 1956
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 425 times
Been thanked: 647 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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

mnfisher
Valued Contributor
Posts: 1880
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 886 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

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

Matrixv8
Posts: 28
Joined: Mon Jun 03, 2024 2:51 am
Been thanked: 4 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post by Matrixv8 »

Thank you for your help, I intend to use this in the future projects.

Regards,
Pierre

mnfisher
Valued Contributor
Posts: 1880
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 886 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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
Attachments
ajs4_minimal.fcfx
(14.9 KiB) Downloaded 8 times

medelec35
Valued Contributor
Posts: 2269
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 697 times
Been thanked: 767 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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
Martin

Matrixv8
Posts: 28
Joined: Mon Jun 03, 2024 2:51 am
Been thanked: 4 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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

Matrixv8
Posts: 28
Joined: Mon Jun 03, 2024 2:51 am
Been thanked: 4 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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

mnfisher
Valued Contributor
Posts: 1880
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 154 times
Been thanked: 886 times

Re: AJ-SR04M Returning 9999 while using the AJ-SR04M Component

Post 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

Post Reply