Troubleshooting a (cheap) sonar unit

|
| Comments (0)
One of my silly dream projects is a steampunkish range finder. The key part is a sonar unit. There's lots out there - I found an HC-SR04 on eBay for $2 shipped! I couldn't go wrong (or so I thought).

After a few weeks it arrived, and I promptly wired it up to my arduino and grabbed some sample code and ..... nothing. Hmmm... The code talked about having added / removed / tweaked, so maybe the code was faulty. I tried a different code sample, another one and one more before concluding that the unit must be a dud.

But I could not let it rest. I could order another one, but that is not the hacker way.

Then I remembered my XProtolab 'scope! It took some fiddling - had to learn how to configure the trigger setting. Even added a bit of extra code to send a second pulse 1000 micro seconds after the first pulse - just to make sure I was seeing things right. Eventually I got the scope working, and here is what I saw:

clean

Both trigger AND echo were responding with the echo pulse, and both the trigger AND echo lines seemed to be seeing the trigger pulse! Some experimenting with the pins used on the arduino and even disconnecting the trigger all together confirmed that internally the sonar unit was somehow joining the two lines. And more importantly, the echo signal was very weak - too weak for the arduino to detect.

I tried to boost the signal by using a NPN transistor (2N3904), but clearly had it wired wrong. I fed the weak echo into the base, and sent the emitter into the arduino. The result was a weird capacitance coming off the trigger pulse.

amped

After consulting with a friend, I was informed "If you feed the signal into an npn base the signal voltage out of the emitter can never be higher than the input. You need to take the voltage off the collector and invert it".

schematics

My first attempt was on the left. Hmmmm... okay, so I flipped the resistors around (schematic on the right) and took the voltage off the collector and sure enough it was inverted! (I removed the second pulse at 1000uS)

fixed

Now, I suppose I could have added another transistor to invert it again, but it was much easier changing the code from:

ms = pulseIn(EchoPin, HIGH);

to:

ms = pulseIn(EchoPin, LOW);

:-)

"Fixed" sonar unit

And now my range finder works!!!! Yeah!!

I learned a bit more about transistors, and how to use my scope!

A $2 part and a couple hours of trouble shooting is not a bad price to pay for some edumacation :-)

Leave a comment