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
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by JohnCrow »

Ok
I'll see what I can come up with myself as well.
Though Ive got a new toy to play with now, my Raspberry Pi dropped through the door today. Only been 6 weeks.

Hmm Flowcode for the Raspberry Pi. (Hi Benj Only joking :lol: )
1 in 10 people understand binary, the other one doesn't !

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 »

Hi John,

We have a Raspberry Pi GPIO to E-blocks adapter board about to be released. This gives you two full 8-bit E-blocks ports from the Raspberry Pi allowing direct compatibility with most of the E-block range :D

We do need some basic examples writing to do simple I/O if your interested John.
E-blocks-raspberrypi_small.jpg
E-blocks-raspberrypi_small.jpg (26.6 KiB) Viewed 43712 times
Flowcode for Rasp Pi has crossed our minds :wink:

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by JohnCrow »

Hi Benj
Interfacing the RP is something I want to look at eventually. Your GPIO boards looks good.
But for the moment, I am going to have to use it with a TV. My monitor just has a standard 15 pin VGA connector.
I have a DVi to VGA to connect it to the PC video card but I understand this doesnt have all the digital signals so a HDMI to DVi cable wont work.
I think you can get adaptor boxs but they are not much less than a new monitor.
Tv is downstairs and the pc and pic hardware is upstairs, so not easy to set up a lot of hardware.
Once ive tried the RP and decided if it will be worth it I'll get a new monitor with hdmi and then look at interfacing experiments, its also a long time since I did anything with LINUX.

Edit

Ive just found a recommendation for a converter on amazon for about £20, Ive just ordered it. If it works it will make things a lot easier (Ok its not going to give blueray quality video but it should be fine for coding, and isnt going to break the bank)
Will post findings when it arrives.
1 in 10 people understand binary, the other one doesn't !

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by kersing »

JohnCrow wrote:Ive just found a recommendation for a converter on amazon for about £20, Ive just ordered it. If it works it will make things a lot easier (Ok its not going to give blueray quality video but it should be fine for coding, and isnt going to break the bank)
Will post findings when it arrives.
Hi John,

That sounds incredibly cheap. Got a link? I would love to connect my RPi to a 'spare' VGA monitor. It now shares the monitor of the system I use most, not ideal...

My RPi got delivered mid April after 6 1/2 weeks, it seemed a lot longer at the time due to the unexpected delay of RoHS testing. (I was one of the lucky few to get an order in very early on launch day) Naturally it arrived at a time I had too many things going on, so some colleagues borrowed it the past few weeks.

Regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by JohnCrow »

Hi Jac

Ive just tried my RPi with the HDMI - VGA adaptor.
Works fine. Not had chance to really play with it and check just how high the graphics resolution will go, but the default looks fine.During the boot stages the text messages are small but readable. Proberbly about the same size as booting a normal Linux system.

http://www.amazon.co.uk/gp/product/B007QT0NNW

I'm very impressed. Cheaper than a having to replace the monitor.
Ive got a KVM switch somewhere, will use that for the video to save having to keep swoping the monitor lead.
For completeness my monitor is a:
Sampo PD-80A11 Series 17" TFT (Under windows runs 1280x1064 max resolution)
1 in 10 people understand binary, the other one doesn't !

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Martin,

Thanks great work :)

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Martin,

Thanks for the nice work. I have manged to improve accuracy in v1 using TMR1 for 100 us time measurement. In fact I have changed to 92us because we are loosing couple of pic cycle due to other commands..so by adjusting few microseconds produce much better accuracy..

I have posted my program here..I have tested this program for 10cm, 20cm, 30cm, 40cm, 50cm, 60cm and 70 cm and the measured distance correspondingly 10.2cm, 20.4cm, 29.0cm, 39.2cm, 50.2cm, 60.4cm and 70.6cm.

Note: PIC 16f877a, LCD>>PORTD, RB0>ECHO, RB1>Pulse,Xtal>20MHz
C code :
tmr1l=0x34;
tmr1h=0xFE;

This produces 65076 in total which initializes TMR1 so after 460 count TMR1 will overflow..requires 0.2us*460 = 92us.

Thanks,

Enamul
Attachments
Distance Sensor 16F877 V1.1.fcf
(15.66 KiB) Downloaded 1174 times
Enamul
University of Nottingham
enamul4mm@gmail.com

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

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi Enamul,
You have posted in the correct place :)

Its great your taking the time to modify code, so thank you!.
I will try it out when given the chance and let you know how it goes.

My initial code was a quick see if module works code, so I'm sure there could be different ways to improve on the original.

Martin
Martin

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Martin,

I have modified my last V1 to deal with temperature change. For that, I have now used distance formula, D = ((temp dependent speed)*time of echo)/2. In your v1 and my last post, Count is measuring the number of 100us (actually 92 us implemented in TMR1) which ultimately measures Echo time. And temperature dependent sound velocity can be expressed as
V= 331 + 0.6*(del T) where del T = change of temperature from 0 deg C.

I have used VA variable to calculate sound speed in environment temperature.

VA = 331 + ((6 * TEMP) / 10)

Here TEMP is byte temperature value which can be easily obtained from a LM 35Dz temperature sensor.

I have calculated distance using Flowcode V5 feature Ulong variable (32-bit) which makes it more precise.

D= (Count*100us)*(VA*100cm/1000000us)/2 which can be re-written as D= (Count*VA)/200

I have tested this version and it shows much more accuracy than the previous version (ECHO time/58) which assumes sound velocity approximately 340 m/s but that might not always true as the temperature is not 15 deg C .

Thanks,

Enamul
Attachments
Distance Sensor 16F877 V2.1.fcf
(22.76 KiB) Downloaded 993 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Martin,

Thanks a lot. I have found a problem in Ulong variable just now. Although it should support 32-bit which means 4294967296 could be calculated with this. But I have seen now in hardware it only support 32768..which means that it behaves like normal integer. I am changing my code to adopt the problem.

I will post new program soon..

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

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,

Not sure if this is the problem but may help...

The unsigned 32-bit variables should be working correctly but the LCD print macros may only support signed 16-bit numbers.

To get around this you can use the number to hex function to generate a hex string representation of your 32-bit variable which you can print out via the LCD.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Martin,

I have tested in hardware all new feature of V5 like Uint, Long, ULong. None of them work in hardware although they work in simulation.

I have to sort out the problem by changing back to all integer variable. I have noticed that my last version can measure distance precisely but it has low resolution because we are sampling ECHO time by 100us. I have reduced that to 25 us but in program I have implemented 25 us as 18 us to compensate some pic cycles.

Now resolution problem is resolved and I can measure fraction of cm variation in object position. The range of object detection is now 2 cm to 230 cm.

To adopt D variable as INT, I have changed VA variable in two part VA_1 and VA_2 where VA_1 is 3 and VA_2 is 45 if VA = 345. This can be easily accommodated in INT variable.

Hope this will help. :)
Attachments
Distance Sensor 16F877 V2.3.fcf
Ulong removed version
(23.63 KiB) Downloaded 920 times
Enamul
University of Nottingham
enamul4mm@gmail.com

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 »

I have tested in hardware all new feature of V5 like Uint, Long, ULong. None of them work in hardware although they work in simulation.
Have you tested with the print number LCD function? If not then how are you testing?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi,

Yes. I have printed the number using print number LCD macro.

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi,

I have multiplied two INT variable and divide them by 800 whose result I put into a ULong variable.

e.g., D= (Count*VA)/800

if Count =94, VA = 345, D = 32430/800 = 40
whereas if Count = 95, VA = 345, D = 32775/800= 40 should be but in LCD it shows (-40!!)

Then I have tested this eventually changing D variable to Long and UInt, I got the same result.

That's why I came to the conclusion that D can be upto 32768 not beyond that which is INT...

Thanks,

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Ben,

I have just found out the solution of the problem.. I am sending you three programs if you can test those in hardware you will be able to see the problem..

In Test1 I have used Count variable as INT and initializes that with 95 and VA as INT and initializes by 345..in this one D is ULong and displayed by LCD number function but shows negative 40.

whereas in Test2, I have calculated D= (95*345/800 ) without using Count and VA variable it works..shows 40.

I came in conclusion that if we want to use ULong we have to change all variable associated with that calculation to be ULong.

In Test3 I did so..it works.

Thanks,

Enamul
Attachments
test3.fcf
This is also fine...
(10 KiB) Downloaded 785 times
test2.fcf
This is fine..
(14 KiB) Downloaded 717 times
test1.fcf
This version shows negative result.
(10.5 KiB) Downloaded 705 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Ben,

Thanks for the immediate response of my post which encourage me to solve the problem. I need ULong for my other projects. It's nice that it really works if I am careful.

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi Ben,

I didn't notice your post while I have put my post about ULong, Long and UInt.
Benj wrote: The unsigned 32-bit variables should be working correctly but the LCD print macros may only support signed 16-bit numbers.

To get around this you can use the number to hex function to generate a hex string representation of your 32-bit variable which you can print out via the LCD.
If you have a look through the test1-3, you can see I don't try to print ULong variable at its maximum capacity not even as INT..the value of ULong could be maximum 230.

I am sure you could find out some actual solution of the problem..I think you understand what I meant. e.g, ULong= INT*INT...should work...
ULong= ULong*ULong works now..I guess you are putting intermediate results in the multiplier or multiplicand variables..

Please inform me if you have some time to have a look at the issue.

Thanks,

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Enamul »

Hi,

I am planning to use CCP for capturing the pulse width from the HC-S04 sensor. I guess it can provide maximum possible resolution.

I will post after having some play. :)

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

Enamul wrote: I have posted my program here..I have tested this program for 10cm, 20cm, 30cm, 40cm, 50cm, 60cm and 70 cm and the measured distance correspondingly 10.2cm, 20.4cm, 29.0cm, 39.2cm, 50.2cm, 60.4cm and 70.6cm.

Note: PIC 16f877a, LCD>>PORTD, RB0>ECHO, RB1>Pulse,Xtal>20MHz
C code :
tmr1l=0x34;
tmr1h=0xFE;
Hi,

I’m trying to get this program “Distance Sensor 16F877 V1.1.fcf” to work on a PIC16F690.

I have changed in this program the device to 16F690 and connected the ports to the 16F690 pins.
LCD:
D4 – RC0
D5 – RC1
D6 – RC2
D7 – RC3
RS – RC4
EN – RC5

HC-SR04
Pulse - RB6
Echo – RB7

All is working, except it display’s the wrong distance:

For example: 20 cm is displayed as 5.6 cm , and so on. Can this program work with a
16F690?

I think it has something to do with the calculation, but I dont know how.

EDIT. This comes directly from the eBay seller:

•Module Working Principle:
1.Adopt IO trigger through supplying at least 10us sequence of high level signal.
2.The module automatically send eight 40khz square wave and automatically detect whether receive the returning pulse signal.
3.If there is signals returning, through outputting high level and the time of high level continuing is the time of that from the ultrasonic transmitting to receiving. Test distance =
(high level time * sound velocity (340M/S) / 2.


Attached is the program what I use and only modified for a 16F690.

Frank.
Attachments
Distance Sensor 16F690 V1-1.fcf
(15.64 KiB) Downloaded 675 times

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

Hi,

I tried now all the flowcharts in this topic en modified them for a PIC16F690. None of them are working on this 16F690. Wrong distance (CM) is showing up.

I need this to work because my project requires to switch on a LED on X cm. ( X= 10, 20, 30, 40 and 50 cm , or different)

Can someone please help me with this?

Thanks,

Frank

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

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi Frank,
I will set my HC-SR04 back up tonight, and test with 16F690.
I will post a working version.

Martin
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

medelec35 wrote:Hi Frank,
I will set my HC-SR04 back up tonight, and test with 16F690.
I will post a working version.

Martin
Hi Martin,

Thanks, i'm looking forward to it. :-)

Frank

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

Re: Ultrasonic Distance sensor using HC-SR04

Post by medelec35 »

Hi Frank,
Mine works fine with 16F690. Showing correct distance,
I'm a bit confused with your configuration settings.

Clock is set for internal, yet speed is set to 20MHz (which if using External crystal should be set to HS) and your not using osccon.
What are your settings and hardware. please

E.g external 19,6608 crystal
or 8MHz internal oscillator etc.
You could just have a timing issue?

Also are you using Eb006 Programmer or your own hardware?
Martin

Frank607
Posts: 192
Joined: Mon Mar 04, 2013 8:07 pm
Has thanked: 29 times
Been thanked: 15 times
Contact:

Re: Ultrasonic Distance sensor using HC-SR04

Post by Frank607 »

Hi Martin,

I'm using now your version 1.0 and use 8MHz internal oscillator and add C Code osccon=0x70; to the beginning of the flowchart. In the configuration from the chip I have set the clock speed to 8000000 Hz.

I have also bought a few 19,6608 MHz Crystal’s, delivery will be next week.

Now it is much more better 10 cm = 10 cm on display, 20 cm = 18 cm on display, 30 cm = 24 cm on display.

How bigger the distance how more inaccurate it shows. How can I make this better? Is the 19,6608 MHz Crystal the better solution ?

Thanks,

Frank

Post Reply