Mittwoch, 15. Januar 2014

Ultrasonic Sensor HC-SR04 Microcontroller Test

I'm trying out the Sensor HC-SR04 Modul.
First of all I had to figure out, how it works.




As you can see it has 4 pins.

VCC 5V
Trigger
Echo
GND

Here is how it should be controlled:



Send a 10us impulse to trigger pin.
The module will send 8 ultrasonic impulses.
The echo pin goes high for 100us to 25ms, depending on the distance to the object.
If there is no reflecting surface, the echo pin goes down after max. 30ms


Here my test setup:


A little video:



And the Bascom code:



Config Pinc.0 = Output
Config Pinc.1 = Input
Dim A As Long
Cls

Do
 Pinc.0 = 0     'Trigger 0'
 Waitus 2
 Pinc.0 = 1     'Trigger 1'
 Waitus 10      'wait 10us'
 Pinc.0 = 0      'Trigger 0'

 If Pinc.1 = 1 Then Gosub Outlcd                            'when echo goes high'


Loop



Outlcd:
 A = 0
 Do
 A = A + 1
 Loop Until Pinc.1 = 0                                      'count until echo 0'

 Cls
 A = A / 30                                                 'divide value by 30 to get it nearly in cm'
 Lcd A
 Waitms 500

 Return

End