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

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

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

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
Has thanked: 1 time

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 ?

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

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

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

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
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
Has thanked: 1 time

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
Has thanked: 1 time

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

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

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

AqaFlow
Posts: 34
Joined: Mon May 16, 2016 2:03 pm
Has thanked: 6 times
Been thanked: 9 times

Re: Ultrasonic Distance sensor using HC-SR04

Post by AqaFlow »

Hi Martin,

I try open file Distance Sensor 16F883 V1 but cant open, I try with FC8, FC9 FC10 but cant.
Please advise
Distance Sensor 16F883 V1 (1).fcf
(11 KiB) Downloaded 314 times
Please advice.
Thanks
Amrat

chipfryer27
Valued Contributor
Valued Contributor
Posts: 774
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 185 times
Been thanked: 204 times

Re: Ultrasonic Distance sensor using HC-SR04

Post by chipfryer27 »

Hi

Projects created prior to v7 cannot be directly opened in your versions of Flowcode. On another machine I have version v5 onwards so when I'm next at it I'll see what I can do.

However someone will probably assist before I get the chance to :)

Regards

chipfryer27
Valued Contributor
Valued Contributor
Posts: 774
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 185 times
Been thanked: 204 times

Re: Ultrasonic Distance sensor using HC-SR04

Post by chipfryer27 »

Hi

The attached should open from v7 onwards.

You will probably have to resize components and they may be split between 2D and 3D panels.

Regards
Attachments
Distance Sensor 16F883 V1_v7.fcfx
(13.42 KiB) Downloaded 269 times

Post Reply