SR04 Ultrasonic Sensor

Any bugs you encounter with Flowcode should be discussed here.
chipfryer27
Valued Contributor
Posts: 1195
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Flowcode v10 SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi

Did a couple of tests today.

Set up FC pretty much as the example file using a PIC18877 with trivial modifications to suit.

Set up a little test rig to measure distance from sensor to target. Rig holds the sensor at 90' to target, vertically, with very impressive results for such a module and my accuracy was greater than +/- 1% (bear in mind we count in cm).

These images show measured at 117 with reading of 118 (sorry, can't figure out yet how to rotate).

100_0224.JPG
100_0224.JPG (92.13 KiB) Viewed 435 times
100_0223.JPG
100_0223.JPG (91.66 KiB) Viewed 435 times

Reaching 140cm all was still good, reading showed nothing of concern.

Attached are reading and pulse trace.

100_0226.JPG
100_0226.JPG (75.61 KiB) Viewed 435 times
140cm.jpg
140cm.jpg (48.74 KiB) Viewed 435 times

Now the fun begins. At anything above 140cm the readings start again at zero. At a measured 144cm the readings return 4cm. The pulse width suggests that not to be the case.

144cm.jpg
144cm.jpg (46.96 KiB) Viewed 435 times
If I hold a clipboard underneath the sensor it returns the distance to target, but once above 140 it starts again from zero.

This pulse is at 190cm with reading showing 50cm

190cm.jpg
190cm.jpg (47.62 KiB) Viewed 435 times

This is measured at approx 270cm. Reading shows 132 (if you add in 140 then it's still with +/- 1%)

270cm.jpg
270cm.jpg (50.32 KiB) Viewed 435 times

I'm guessing some variable within FC has rolled over or the like.

Regrds

medelec35
Matrix Staff
Posts: 1471
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 516 times
Been thanked: 474 times

Re: SR04 Ultrasonic Sensor

Post by medelec35 »

Hi Iain
Many thanks, for report.
Out of curiosity, are the results the same for timer and non-timer versions?
Martin

chipfryer27
Valued Contributor
Posts: 1195
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi Martin

Sorry, only tried timer didn't check non-timer . I will try next time I'm in the evil lab. I also tried just the example file but it too does the same (just in case I'd missassigned a variable or such).

Watching the analyser, the module even detects the angle being adjusted slightly. It's an impressive little gadget.

Regards

medelec35
Matrix Staff
Posts: 1471
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 516 times
Been thanked: 474 times

Re: SR04 Ultrasonic Sensor

Post by medelec35 »

Hi Iaian.
I have just tried the sensor at greater distances.
Results for 277cm:
SR04 Sensor1.png
SR04 Sensor1.png (13.95 KiB) Viewed 401 times
I have noticed that reflections on nearby objects affects the reading the great the distance which is expected.
I.e. the further the object the wider the band.
What I would recommend is do what did, also have UART output that states the distance so both distance reading and pulse width can be matched together.
Martin

chipfryer27
Valued Contributor
Posts: 1195
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi

Yes I did expect the pulses to diverge with distance but it did not appear to affect anything at all in my test set up, as it was not always in the same position relative to surrounding, but did always have fun above 140. Just to be clear at 140cm all good, at 141cm it reads 1cm and increases accordingly (144cm = 4cm) Nothing is suddenly that close to the sensor. Pretty sure I would see it :)

When I'm next at it I can do the UART, but you can perhaps see from the pulses I already sent that the pulse width is increasing with each test suggesting it has not caught something only a cm or so from the sensor (sensor increasing in height from floor).

Regards

medelec35
Matrix Staff
Posts: 1471
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 516 times
Been thanked: 474 times

Re: SR04 Ultrasonic Sensor

Post by medelec35 »

The reason, I suggested the UART as well is because I noticed the display changing from say 160cm to 50cm then back to 160etc.
As it happens the pulse was also showing it was 50 cm, so then can be ramdom changes dure to sound bouncing of objects that are not in the line of site.
Of course, if the UART just after the pulse does not agree, then I will see if I can get to the bottom of it.
Martin

chipfryer27
Valued Contributor
Posts: 1195
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi

"Me" moving around the PC to "capture" may have been in the picture so to speak when triggering / grabbing a pulse trace, but when only reading the display I'm not really near it.

The sensor loops at one second intervals so I see when I or anything else is detected.

If me, at 140 / 144 (140 / 4 on display) I would need to have moved to within four centimeters of the sensor, and after adjusting it to different heights all the way up to the maximum I documented above, moved back to exactly the same position each time which is extemely unlikely. As I repeated the tests it is again extremely unlikely that I'm the cause.

In the traces above they document 8.xxxx / 11.xxxx / 15.xxxx mS which equates to roughly 1.4M to 2.7M, but at no time did the display ever show anything above 140cm before starting again at zero.

Probably Thursday before I can retest and will send more results then.

Regards

chipfryer27
Valued Contributor
Posts: 1195
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi

Following on.

Picture of test rig with sensor pointing to floor. This reads 114cm on display / UART with pulse width of 6.628mS

100_0229.JPG
100_0229.JPG (93.85 KiB) Viewed 303 times
100_0230.JPG
100_0230.JPG (75.14 KiB) Viewed 303 times
100_0231.JPG
100_0231.JPG (75.06 KiB) Viewed 303 times
100_0234.JPG
100_0234.JPG (80.57 KiB) Viewed 303 times
114cm.jpg
114cm.jpg (51.78 KiB) Viewed 303 times


Next is at 140cm with a pulse width of 8.15506mS


100_0235.JPG
100_0235.JPG (134.66 KiB) Viewed 303 times
140-cm.jpg
140-cm.jpg (53.62 KiB) Viewed 303 times

Now we read 1cm with a pulse width of 8.27166mS

1.jpg
1.jpg (52.81 KiB) Viewed 303 times
100_0236.JPG
100_0236.JPG (113.94 KiB) Viewed 303 times
100_0237.JPG
100_0237.JPG (76.71 KiB) Viewed 303 times

The tape measure is only added when I physically measure the distance, nothing else in near enough to interfere, you can see there is nothing 1cm below sensor and pulse width has increased, not decreased.

100_0238.JPG
100_0238.JPG (101.04 KiB) Viewed 303 times

Regards

mnfisher
Valued Contributor
Posts: 1007
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 106 times
Been thanked: 519 times

Re: SR04 Ultrasonic Sensor

Post by mnfisher »

That's one good looking lab. How goes the trs-80? And where did you hire the apprentice 🤔

chipfryer27
Valued Contributor
Posts: 1195
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: SR04 Ultrasonic Sensor

Post by chipfryer27 »

Hi

Lab is in a bit of a state just now, in the process of a good clean out, getting rid of the "might come in handy" pile amongst a LOT of acquired useless junk.

That's my "Ivory Tower" up in the clouds, i.e. the mezzanine floor I built to elevate my status :)

Scopes / analysers / soldering / test equipment / anti-static work areas and general benches all live up there with me whilst below I have CNC / milling / lathes / bandsaw / metal-bending and all other stuff you'd expect.

I do a lot of Proof-of-Concept and prototyping, but could also undertake small productiion runs if required.

I got the TRS-80 running but struggeled to find a use. I did think on building a generic interface that could plug into the expansion port but then thought "why" as even a modest PIC would outperform, so I'm still looking for a project to use it with.

My zombie used to be a minion but it displeased me so now it is undead. This came in handy as I needed help to test a sensor I was using as part of safety feature on a gun that would travel at speed. Before setting off it would check that nothing was present in the path and if anything did then appear it would come to a halt. I needed a human size target and also a "hand" to check it did come to a halt without endangering me :)

Regards

Post Reply