Atmel Ultrasonic Ranger
IntroductionThis page documents and design and implementation of an ultrasonic range finder. The main reason for taking on this project was to gain experience with the Atmel 8-bit microcontrollers. Before starting this project I already had plenty of experience with various microcontrollers and processors, so getting started with the Atmel microcontroller was straight forward enough. The features offered by all the different brands of microcontroller are reletivly similar e.g. USART, Timers, ADC etc etc. One of the big advantages of using an Atmel microcontroller is that it is supported by the GNU GCC compiler. This compiler has a long history and is used by many systems, some of which include Linux and Motorola systems.
Project TheoryWhen the ultrasonic transmitter is fed with a 40KHz AC signal, it will generate ultrasonic sound waves. The sound waves then travel outwards at the speed of sound. When the sound waves hit an object, they reflect back towards the source and are in turn picked up by the ultrasonic receiver. As we know the speed of sound, we can convert the time of flight to a distance.
The actual speed of sound is dependent on (among other things) the temperature. For the sake of simplicity, this project will assume the speed of sound is 1cm every 30 microseconds, which is approximatley the speed of sound at 20oC.
A 156KB schematic of the system can be viewed here (as you can see, drawing schematics isn't my strong point). The schematic excludes the 2 voltage regulators and the voltage doubler. They are instead, shown below.
The project is made up of the following hardware components:
Programming the DeviceThe microcontroller is programmed via the parallel port with a homemade SP12 cable (see the main schematic). The software used to download the hex file to the microcontroller is AVRDUDE. AVRDUDE is command line driven so it can be called directly from the makefile. nbsp;See the makefile for the paramters used.
Every now and again AVRDUDE will report a content mismatch error; see the following screen capture. Some people on the Internet have experienced the same problem, which might be down to parallel port timing issues. While the microcontroller was running off the internal oscillator the error was quite common, but now it runs off an external oscillator the problem has pretty much gone away. If you get this error , you should just keep trying until it works. I've tried to find the ISP specification so that I can write my own programmer (it can't be that hard if you support only a single device and programmer), but as of yet I cannot find it. The AVRDUDE code is open source, but reverse engineering it seems like a lot of trouble to fix a rare error.
Source CodeI've noticed a couple of mistakes in the source code. I've setup the PWM chanel incorrectly (next time I'll read all of the data sheet). Although it still produces the required 40KHz signal. From the logs I can see that quite a lot of people have downlaoded the source code. It does work exactly as it should, but if try to change the PWM frequency via the source code it might fail. I'll upload the fixed code sometime soon. If you need help with the source then send me an email.
Here's the source code as HTML.
Here's (UNDER CONSTRUCTION) the makefile as HTML.
Here's (UNDER CONSTRUCTION) the source code and makefile as a zip file.
ImplementationThe system works by generating 5 40KHz pulses and timing how long it takes to receive an echo.
ResultsThe results are quite impressive considering the simple circuit and code. The calculated distance is more accurate than I thought and noise causes no problems whatsoever (in my simple tests). The system can detect objects as close as 1cm and as far as 1.8 metetrs. A variable resistor has been placed on the comparator, which allows the switching level to be changed. This would allow detection over much further ranges (although I haven't tried it yet). The distance calculated by the device is output via the serial port. This allows the results to be displayed in HyperTerminal.
This video clip shows how sensitive the device is. The scope in the background shows the transmitted pulses (top trace) and the received pulses (bottom trace). As can be seen, even the slightest movement of the device is easily detectable (it also gave me an oppurtunity to try out my new digital camera). If you assume the speed of sound is 1cm every 30us, then sound travels 1mm every 3us (3000ns). The time of 3000ns may seem short, but the microcontrollers internal timer can increment every 62.5ns (1/16MHz).