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

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Thu Sep 21, 2017 8:37 am

kalmar79 wrote:ok I did again in another file and working good

That's good, do you know what you did different to make it work?

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

You have two ways to go.
1) is with a 5V sounder with built-in driving circuitry.
2) A piezo type sounder which requires a driving waveform e.g. PWM.
Due to age of your microcontroller, it does not have any useful functions built-in .
E.e. PWM, analogue to digital converters, internal oscillator etc.
So with that in mind the second option is possible but is complex.
This is because of the need to be bit banged while sill allowing detection function.
So with your chip, option one would be easiest as you just put 5V DC in and it will buzz.
If the buzzer takes more than the current that a pin can supply then you will required an additional transistor for controlling power to the buzzer.

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

How the Proximity detector works is a pulse of 10us is sent into the trigger pulse input which causes a burst of ultrasonic sound to be generated.
Then the microcontroller waits for a signal caused by receiving sound wave that was generated by the trigger.
So its waiting in a tight loop.
What if the sound is not received back?
Then it will stay stuck in a tight loop.
The

Code: Select all

count > 320
just acts as a timeout function.
If the signal is not received within a few milliseconds then exit tight loop and resend trigger pulse.

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 22, 2017 6:08 am

my friend thanks for take the time to explain me. I only change the file or carpet were I was doing the flowchart, I do it in english and spanish but the only one word I change was count(contador in spanish). I don't know what happened.
And I put a buzzer in the output and work good.
Is possible make a PWM with the switch funtion? I made something like this for a servomotor.

thanks

wantedmama
Posts: 4
Joined: Thu Mar 05, 2020 11:35 pm
Has thanked: 1 time
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby wantedmama » Thu Mar 05, 2020 11:48 pm

medelec35 wrote:This could work on 4MHz?

Sorry to bring a very old post , but needed help with connection pins/ports of LCD EB-005 and HC-SR04 to PIC16F1937 using EB-006 board . I can't seem to contact private message you via this forum, where can I contact you ?

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

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Fri Mar 06, 2020 9:52 am

wantedmama wrote:but needed help with connection pins/ports of LCD EB-005 and HC-SR04 to PIC16F1937 using EB-006 board

I have attached the two original flowcharts that was created with Flowcode V5, but have converted them to V8.
Just make sure that for the V1 & V2 flowcharts, LCD is connected to port A
You can change the port if required.
If you look at the output icon, it connects to Trigger
The input icon connects to Echo
In V1 its in main & in V2 it's within Echo_Detect function macro

wantedmama wrote:I can't seem to contact private message you via this forum, where can I contact you ?

Like other forums, you can only send PM's after you have added a few posts.
It's against forum rules(2) to ask for help via personal message.
However, you can use personal message (when post count is high enough) for any flowcharts that require keeping private.
Attachments
Distance Sensor 16F1937 V1.fcfx
(13.01 KiB) Downloaded 28 times
Distance Sensor 16F1937 V2.fcfx
(19.78 KiB) Downloaded 31 times
Martin

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

wantedmama
Posts: 4
Joined: Thu Mar 05, 2020 11:35 pm
Has thanked: 1 time
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby wantedmama » Mon Mar 09, 2020 6:45 pm

medelec35 wrote:
wantedmama wrote:but needed help with connection pins/ports of LCD EB-005 and HC-SR04 to PIC16F1937 using EB-006 board

I have attached the two original flowcharts that was created with Flowcode V5, but have converted them to V8.
Just make sure that for the V1 & V2 flowcharts, LCD is connected to port A
You can change the port if required.
If you look at the output icon, it connects to Trigger
The input icon connects to Echo
In V1 its in main & in V2 it's within Echo_Detect function macro

wantedmama wrote:I can't seem to contact private message you via this forum, where can I contact you ?

Like other forums, you can only send PM's after you have added a few posts.
It's against forum rules(2) to ask for help via personal message.
However, you can use personal message (when post count is high enough) for any flowcharts that require keeping private.

Thank you , I have followed your guide however the circuit doesn't seem to be working for me .https://imgur.com/csz44SW https://imgur.com/lm1QIkc
is there anything I am missing ?
I have fixed the vcc same row as trig and connected ground
Last edited by wantedmama on Mon Mar 09, 2020 7:15 pm, edited 1 time in total.

User avatar
Benj
Matrix Staff
Posts: 14786
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4750 times
Been thanked: 4358 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby Benj » Mon Mar 09, 2020 6:59 pm

Hello,

From your photos, the power cable seems to be in the same row as the trig signal on the breadboard.

Also you haven't connected up the ground pin.

wantedmama
Posts: 4
Joined: Thu Mar 05, 2020 11:35 pm
Has thanked: 1 time
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby wantedmama » Mon Mar 09, 2020 7:10 pm

Benj wrote:Hello,

From your photos, the power cable seems to be in the same row as the trig signal on the breadboard.

Also you haven't connected up the ground pin.


I realised that later too , I fixed the same row trig and vcc , also connected ground . However it still doesn't seem to be working
Edit : the simulation in flowcode doesn't seem to be working too. I am currently using V8.

wantedmama
Posts: 4
Joined: Thu Mar 05, 2020 11:35 pm
Has thanked: 1 time
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Postby wantedmama » Wed Mar 11, 2020 1:22 am

medelec35 wrote:
wantedmama wrote:but needed help with connection pins/ports of LCD EB-005 and HC-SR04 to PIC16F1937 using EB-006 board

I have attached the two original flowcharts that was created with Flowcode V5, but have converted them to V8.
Just make sure that for the V1 & V2 flowcharts, LCD is connected to port A
You can change the port if required.
If you look at the output icon, it connects to Trigger
The input icon connects to Echo
In V1 its in main & in V2 it's within Echo_Detect function macro

wantedmama wrote:I can't seem to contact private message you via this forum, where can I contact you ?

Like other forums, you can only send PM's after you have added a few posts.
It's against forum rules(2) to ask for help via personal message.
However, you can use personal message (when post count is high enough) for any flowcharts that require keeping private.

Hello, The simulation and circuit doesn't seem to be working for me using V8 flowcode

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

Re: Ultrasonic Distance sensor using HC-SR04

Postby medelec35 » Wed Mar 11, 2020 8:24 am

Can you post the flowchart and a picture showing all the connections from programmer to sensor.
V1 should simulate but V2 won't as the c code

Code: Select all

FCV_COUNT = TMR0;
won't simulate.
These users thanked the author medelec35 for the post:
wantedmama (Thu Mar 12, 2020 3:51 pm)
Rating: 5%
 
Martin

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