Home right CV Location Work Google+ Twitter YouTube GitHub Linkedin

Alex Gryson, profile image

Alex Gryson

An idea in the hand is worth ten in the head

UX Designer / Product Management

Bordeaux, France

Sonar: Basic obstacle detection

I'm currently trying to build an autonomous robot. Idea being that it hunts around for the brightest part of the room, chills out for a few minutes, then seeks the darkest spot. My prototype worked fairly well.

However, my robot's proclivity for ramming into every lamp in the flat wasn't appreciated. I needed a solution that would work in multiple light levels and work on everything from walls to glass.

The Plan

Transducer circuit

Enter ultrasound! It is non-contact, works in all light levels and can handle a wide variety of surfaces regardless of transparency (We have a fishbowl lamp thing that gives optical obstacle detection nightmares).

Here are the steps I needed to finish this mini-project:

  • Generate a 40kHz square wave
  • Send that to a transmitter / receiver circuit
  • Amplify signal if necessary
  • Detect object based on returned signal

Of course, once finished, it would have to be put into a nice, modular package, but I'll cover that only lightly here.

40kHz Square Wave + Arduino = <3

During my research I found a great article on generating 40kHz square waves with Arduino. While long term I'll be making a timer circuit with a quartz crystal or 555 timer IC (well, I have a 558, but that's just 4×555's in a single package), for the purposes of the experiment, using the Arduino was fine.

Initially building my circuit for a signal from the Arduino but in the future hoping to upgrade to a different system, I made the connector a set of header pins. That way I can make 'shields' that simply plug in to add future functionality

Here's a sample of the code:

void startTransducer(){
		| _BV(WGM21)
		| _BV(WGM20);
	TCCR2B = _BV(WGM22)
		| _BV(CS20);
	OCR2A = B11000111;

void stopTransducer(){
	TCCR2A = 0;
	TCCR2B = 0;

Tx / Rx

transducer circuit diagram

Next up, the transmission / reception circuit. Not being one to reinvent the wheel (too often), I looked through a few circuits and found this one from micro-examples.com to be one of the simplest.

So, I popped down to my local electronics store and got all the bits I needed for a total of about €8 or €9. So I got two sets.

Mass production FTW.

Tx / Rx

oscilloscope screenshot

Now, piping the generated signal into the circuit (bread-boarded for the moment) gave me what you see above. Nicely shaped wave from the Arduino, though at 3.5V, a little weak, the piezos can take up to 20V. The weak signal isn't too much of a problem since I'm using this for obstacle detection rather than range-finding.

There's a slight mechanical 'ringing' in the circuit, but I just wait about 500 microseconds before taking my measurement (about 17cm of a 'blind spot' then). What you see in the oscilloscope readings is my hand about 15cm above the circuit.

Solderin' Time

rear view of circuit

While I had actually moved from the breadboard to protoboard first, I used my second set of components and soldered them onto a home-etched board (pictured). This was my first home etched board, but it works great. I'll be putting up a description of that experiment soon too!

So for less than the price of a night at the cinema, I now have two ultrasonic obstacle detectors. :-D