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: 8926
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

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

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

Re: Ultrasonic Distance sensor using HC-SR04

Post by kalmar79 »

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
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by wantedmama »

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: 8926
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

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 54 times
Distance Sensor 16F1937 V2.fcfx
(19.78 KiB) Downloaded 54 times
Martin

wantedmama
Posts: 4
Joined: Thu Mar 05, 2020 11:35 pm
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by wantedmama »

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: 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,

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
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by wantedmama »

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
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by wantedmama »

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: 8926
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

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

Post Reply