I am getting garbage results trying to control a HC-SR04 sonic distance sensor from a Dagu Mini Driver board (ATmega8). The wires to the SR04 are being monitored with a scope and/or logic analyser and look ok. The TRIG and ECHO timing appear reasonable and change proportional to distance to hard flat object…
I suspect the problem is with the ISR. It often tPmodDIN1imes has the endTime set to something resembling the current time in usec while the startTime variable remains at 0. To me this indicates the the ISR reacted to just the negative going ECHO pulse.
Update 4/2/15:
It appears that the root cause of my problem is a defective cable connector. I used a connector that splits the input to both male and female output pins. I can the readily attach logic analyser or scope probes. Just checked and Digilent is no longer carrying the PmodDIN1 adapter.
Now that I am getting reliable connections the true nature of the beast is emerging. I bought 5 for $13US and feel cheated.
- Any movement at either sensor or target gives a series of invalid results.
- The size and shape of the target is critical. I have an 11x21 cm box as the target. At 10cm, 20cm, 40cm & 50cm it returns fairly accurate results. At 30cm it usually reports 68cm (sometimes 30cm). Flip the box to a larger target and 30cm comes back steady.
- The absence of an echo, timeout when using pulseIn(), means the ECHO line is still high for quite some time.
It is possible that there are multiple manufactures of this unit and I got the worst. Judging by the comments on the internet I am not alone. I can see no use for a distance sensor on a mobile robot that give garbled output while moving.
I had to remove the code do to this update because the paragraphs were getting merged when using “Purified HTML” and switching to Markdown" jumbled the Arduino code.
Moving forward I need to get a more reliable splitter and get better sonic sensors. I also want to examine the ECHO signal at the processor.
Thanks for the feedback.