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: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Wed May 01, 2013 7:53 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Frank607
Posts: 93
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 21 times
Been thanked: 9 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby Frank607 » Thu May 02, 2013 7:19 am

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: 93
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 21 times
Been thanked: 9 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby Frank607 » Tue May 07, 2013 4:09 pm

Hi Martin,

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

Frank

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Tue May 07, 2013 6:01 pm

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


Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Re: Ultrasonic Distance sensor using HC-SR04

Postby jnbruno » Fri Feb 28, 2014 4:50 am

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: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Sat Mar 01, 2014 9:01 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Sun Aug 27, 2017 11:01 pm

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
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Tue Aug 29, 2017 6:35 am

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: 14073
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4297 times
Been thanked: 4050 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby Benj » Tue Aug 29, 2017 1:01 pm

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
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Tue Aug 29, 2017 7:59 pm

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
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Tue Aug 29, 2017 8:09 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Tue Aug 29, 2017 9:41 pm

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 152 times
These users thanked the author medelec35 for the post:
Benj (Wed Aug 30, 2017 11:02 am)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Tue Aug 29, 2017 10:28 pm

My friend thank you very much. Well, I need to find a crystal in 19.66 Mhz, I only have 4 Mhz. :D
These users thanked the author kalmar79 for the post:
Benj (Wed Aug 30, 2017 11:02 am)
Rating: 5.56%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Tue Aug 29, 2017 10:45 pm

This could work on 4MHz?
Attachments
Distance Sensor 16F84A LED 4MHz.fcfx
(9.84 KiB) Downloaded 119 times
These users thanked the author medelec35 for the post:
Benj (Wed Aug 30, 2017 11:02 am)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Thu Aug 31, 2017 2:52 am

Working good !!! :D :D :D :D
These users thanked the author kalmar79 for the post:
Benj (Thu Aug 31, 2017 11:25 am)
Rating: 5.56%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Thu Aug 31, 2017 8:45 am

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
These users thanked the author medelec35 for the post:
Benj (Thu Aug 31, 2017 11:25 am)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Thu Aug 31, 2017 6:40 pm

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Fri Sep 01, 2017 1:04 am

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

Thanks.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Tue Sep 19, 2017 5:20 am

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: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Tue Sep 19, 2017 6:42 am

Hi kalmar79 ,
The number you see below are approx distance in cm + about 3:
Distance cm.png
Distance cm.png (5.21 KiB) Viewed 6075 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
These users thanked the author medelec35 for the post:
Benj (Tue Sep 19, 2017 10:20 am)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Wed Sep 20, 2017 1:11 am

Thank you my friend I going to test.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Wed Sep 20, 2017 2:42 am

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 116 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2372 times
Been thanked: 3379 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Wed Sep 20, 2017 8:52 am

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

kalmar79
Posts: 45
Joined: Sun Aug 20, 2017 8:18 pm
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Thu Sep 21, 2017 2:46 am

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
Been thanked: 4 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby kalmar79 » Thu Sep 21, 2017 5:05 am

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