• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Ultrasonic radar module hc-sr04

Status
Not open for further replies.

dr pepper

Well-Known Member
Most Helpful Member
Ok so I've been using one of these modules.
It has an i/p pin which initiates a ping of 8 40kc cycles, at the end of which an o/p pin goes hi until the module receives an echo.
In software I continuously send ping commands and measure the width of the o/p pulse to see if the target is within a specified range.
The software worked well when I was prototyping it, however when implemented in the main code it doesnt work, if you send continuous pings to the module it works, but if you leave the module for 0.5 seconds then come back to it and give it a ping command, the first ping gives an erroneous result, as if the target was 0mm away from the sensor, the module has power constantly and there is a wait time in the code startup.
I dont really have much time in my code to do a dummy read, as if the module works properly which it sometimes does first ping it can take a mS or so to complete the measurement.
Any ideas how I can get the module to work first ping.
 

Misterbenn

Active Member
as this seems to be a code related issue rather than hardware you might want to attach your code. We cant really suggest anything without knowing what processor and code you are using.

For example it may be helpful if I tell you to reset your timer and check that your interrupt is disabled ... then again that might be a totally useless comment. There is know way to know without seeing what you a trying to run.
 

dr pepper

Well-Known Member
Most Helpful Member
Ok then, I didnt post the code as I find it difficult to understand others assembler, but here goes.
The code looks at the ultrasonic module and if a target within range (tmro (with a prescale of 32 from a 4 mc's clk)<64) then gpio,1 goes hi for the period the condition exists, where as gpio,2 goes hi for 250 iterations of the 'main' routine, I'd rather not bother with gpio,2 and use gpio,1 only, if you look the 0.5 sec delay is remmed out.
This chip doesnt support interrupts, otherwise I'd be using them.
 
Last edited:

dr pepper

Well-Known Member
Most Helpful Member
Its ok I sussed it.
I changed the number of valid consecutive echoes to 10, works well now.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top