Ultrasonic Distance sensor using HC-SR04

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

The Flowchart was designed using 19.6608MHz crystal. Since timer interrupt is used, accuracy will depend on oscillator speed.
If different speed from 19.6608MHz then the distances will not be correct.

Crystal will give better results than internal oscillator.

If using 8MHz then all the calculations will need altering before any reasonable accuracy can be seen.

If I get time, I may put together a 8MHz version, but no promises.

Martin
Martin

Frank607
Posts: 114
Joined: Mon Mar 04, 2013 8:07 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

Hi Martin,

Thanks, but you don’t have to build one for 8MHz. I just bought a few 19.6608MHz crystal’s and when these arrive I will try this and let you know.

Regards,

Frank

Frank607
Posts: 114
Joined: Mon Mar 04, 2013 8:07 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

Hi Martin,

With the right crystal 20MHz all is working fine now. Thanks !

Frank

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Your welcome,
Just glad your now all sorted.
Thanks for the update. :)


Martin
Martin

jnbruno
Posts: 1
Joined: Fri Feb 28, 2014 4:40 am
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by jnbruno »

Hi,

I tried the code attached here but I cant get it to work.. Im using PIC16F877A and HC-SR04 sensor, I want my program to be like this video on youtube.. LED will turn on when obstacle is detected. My Echo is connect to PortD.4 , Trigger is connect to PortD.5, The output LED is on PortD.6.. I really need help..

http://www.youtube.com/watch?v=dRq0e9rwqHo

Please help me.. I need that for my project.. My professor require to use flowcode.. I really need help..

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

If you post your Flowchart then I can see if I can help you out and let you know where you're going wrong.

Martin
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

Hello my friends, I want to know if is possible to use the ultrasonic sensor HC-SR04 with the pic 16F84A ??
I only want to use a led active in 10 cm, I don´t need to use a LCD in this moment.

Thanks

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

I want to make this in flowcode V6, with 16F84A, without LCD only with Led diode. But I don't know how to star the flowchart.

Please teach me !!!

http://www.youtube.com/watch?v=dRq0e9rwqHo

User avatar
Benj
Matrix Staff
Posts: 14865
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Benj »

Hello,
I want to know if is possible to use the ultrasonic sensor HC-SR04 with the pic 16F84A ??
Yes this should be possible if the echo from the module is digital, the 16F84A is now well outdated and doesn't have analogue input capability.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

Thanks for response, well I saw a video in youtube with the 16F84A but ok I going to use a 16F88. I want to do it but I don't know how to star the flowxhart.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

I don't need to use a LCD only a led.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi kalmar79,
I have posted a very basic flowchart that should work with 16F84A and HC-SR04
Use a crystal as high frequency as possible.
The flowchart assumes a crystal of 19.660800MHz
Connections are:
Echo = B6
Trigger = B7
LED = A0

The LED with light when an object is within 10cm of Sensor.

Martin
Attachments
Distance Sensor 16F84A LED_v6.fcfx
(9.94 KiB) Downloaded 201 times
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

My friend thank you very much. Well, I need to find a crystal in 19.66 Mhz, I only have 4 Mhz. :D

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

This could work on 4MHz?
Attachments
Distance Sensor 16F84A LED 4MHz.fcfx
(9.84 KiB) Downloaded 167 times
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

Working good !!! :D :D :D :D

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

That's good.
Thanks for the update.
Benj wrote:the 16F84A is now well outdated and doesn't have analogue input capability.
I fully agree with Ben.
Analogue input capability makes it much easier to add a pot.
Then you can set the distance e.g. from 2cm to 3m you would like to LED to activate.
Of course than can be done with 16F84A but its more complex.
Thank goodness we have microcontrollers with analogue to digital converters.
If you want to make hardware smaller, as you will have a fair few pins left over and use less components then you could consider using 12F1840.
If you do you could use the internal oscillator which then would make crystal and two caps redundant.
The internal oscillator can run a different speeds up to 32MHz .

Martin
Martin

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi kalmar79,
I have merged topics related to ultrasonic sensor to keep forums tidy.
Any questions related to ultrasonic sensor ultrasonic sensor flowchart I posted, please post here rather than making a new topic.

Thank you

Martin
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

Ok my friend, I am very grateful with your support.

Thanks.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

My friend Medelec, I want to change the distance a little bit. What I need to change??
Remember with pic 16F84A in 4Mhz crystal.
thanks

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi kalmar79 ,
The number you see below are approx distance in cm + about 3:
Distance cm.png
(5.21 KiB) Downloaded 8794 times
so if you want to change to about 20cm
use

Code: Select all

 Count <= 22
&

Code: Select all

Count > 23
as a starting point.

Martin
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

Thank you my friend I going to test.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

My friend your flowchart working good with the changes. But I am doing the same flowchart but in spanish, but don´t work.
sensor de distancia con 16F84A.fcfx
(5.37 KiB) Downloaded 186 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8924
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

I can't see why it won't work.
What about adding a delay of say 50ms at the start of the main loop?
You could also try changing Osc type from XT to HS although technically XT is not wrong.

Martin
Martin

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

maybe my HC-SR04 don´t understand spanish :lol: :lol: :lol:

I going to make once again, but in english.

thanks my friend

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

ok I did again in another file and working good, in spanish and english. :lol: :lol:

I want to introduce a buzzer insted a led. do I need to modificate something?

Why you use '320' in (Echo = 0) || (count > 320) ??

thanks

Post Reply