Measuring Distance Using Sound

Similar documents
Timer System Applications. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Sonic Distance Sensors

Electronic Buzzer for Blind

Scope. Here are the times schedule of the pulse-echo technique detect method. Reflect pulse. Emit detect pulse (Ultrasound)

Bohunt School (Wokingham) Internet of Things (IoT) and Node-RED

Blind Spot Monitor Vehicle Blind Spot Monitor

Floating Ball Using Fuzzy Logic Controller

ME 461 Laboratory #3 Analog-to-Digital Conversion

HIGH LOW Astable multivibrators HIGH LOW 1:1

Portland State University MICROCONTROLLERS

Exercise 2: Distance Measurement

Grundlagen Microcontroller Counter/Timer. Günther Gridling Bettina Weiss

Ultrasonics. Introduction

Available online Journal of Scientific and Engineering Research, 2018, 5(4): Research Article

Instrument Cluster Display. Grant Scott III Erin Lawler Mike Carlson

Distance Measurement of an Object by using Ultrasonic Sensors with Arduino and GSM Module

Lab 5 Timer Module PWM ReadMeFirst

Boe-Bot robot manual

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

SCHOOL OF TECHNOLOGY AND PUBLIC MANAGEMENT ENGINEERING TECHNOLOGY DEPARTMENT

PING))) Ultrasonic Distance Sensor (#28015)

Houngninou 2. Abstract

Electronic Counters. Sistemi Virtuali di Acquisizione Dati Prof. Alessandro Pesatori

CHAPTER 6 DIGITAL INSTRUMENTS

OBJECTIVE The purpose of this exercise is to design and build a pulse generator.

GCSE (9-1) WJEC Eduqas GCSE (9-1) in ELECTRONICS ACCREDITED BY OFQUAL DESIGNATED BY QUALIFICATIONS WALES SAMPLE ASSESSMENT MATERIALS

Designing of a Shooting System Using Ultrasonic Radar Sensor

Introduction: Components used:

EE 209 Lab Range Finder

ASTABLE MULTIVIBRATOR

ULTRASONIC TRANSMITTER & RECEIVER

νµθωερτψυιοπασδφγηϕκλζξχϖβνµθωερτ ψυιοπασδφγηϕκλζξχϖβνµθωερτψυιοπα σδφγηϕκλζξχϖβνµθωερτψυιοπασδφγηϕκ χϖβνµθωερτψυιοπασδφγηϕκλζξχϖβνµθ

Standalone Instrument Cluster Display

ESE 350 Microcontroller Laboratory Lab 5: Sensor-Actuator Lab

S. Square Enterprise Company Limited Pro-Wave Electronics Corporation

4: EXPERIMENTS WITH SOUND PULSES

). The THRESHOLD works in exactly the opposite way; whenever the THRESHOLD input is above 2/3V CC

Chapter 13: Comparators

Draw in the space below a possible arrangement for the resistor and capacitor. encapsulated components

Lab M6: The Doppler Effect

DIGITAL ELECTRONICS: LOGIC AND CLOCKS

EE445L Fall 2015 Quiz 2 Page 1 of 5

Sensor and. Motor Control Lab. Abhishek Bhatia. Individual Lab Report #1

A Model Based Approach for Human Recognition and Reception by Robot

Design and Implementation of Ultrasonic Based Distance Measurement Embedded System with Temperature Compensation

EUP V/12V Synchronous Buck PWM Controller DESCRIPTION FEATURES APPLICATIONS. Typical Application Circuit. 1

ATmega16A Microcontroller

Object Detection for Collision Avoidance in ITS

Electronic Instrumentation

Product Information Using the SENT Communications Output Protocol with A1341 and A1343 Devices

Devantech SRF04 Ultra-Sonic Ranger Finder Cornerstone Electronics Technology and Robotics II

SL300 Snow Depth Sensor USL300 SNOW DEPTH SENSOR. Revision User Manual

Microprocessor & Interfacing Lecture Programmable Interval Timer

Using the Z8 Encore! XP Timer

AVR PWM 11 Aug In the table below you have symbols used in the text. The meaning of symbols is the same in the entire guide.

Pulse-Width Modulation (PWM)

ECE Senior Design Final Report For. Scalable Regulated Three Phase Power Rectifier. May 10, 2004 Rev. 1.0

Electronic Instrumentation

UNISONIC TECHNOLOGIES CO., LTD CD4541

CSCI1600 Lab 4: Sound

C++ PROGRAM FOR DRIVING OF AN AGRICOL ROBOT

Chapter 2 Signal Conditioning, Propagation, and Conversion

EE445L Fall 2015 Quiz 2A Solution Page 1

TAPR TICC Timestamping Counter Operation Manual. Introduction

Momentum and Impulse. Objective. Theory. Investigate the relationship between impulse and momentum.

Answer:- School bell starts vibrating when heated which creates compression and rarefaction in air and sound is produced.

Momentum and Impulse

CprE 288 Introduction to Embedded Systems (Output Compare and PWM) Instructors: Dr. Phillip Jones

Physics 2310 Lab #2 Speed of Sound & Resonance in Air

Sten BOT Robot Kit 1 Stensat Group LLC, Copyright 2016

HVW Technologies Analog Infra-Red Ranging System (AIRRS )

Application description AN1014 AM 462: processor interface circuit for the conversion of PWM signals into 4 20mA (current loop interface)

Lesson4 Obstacle avoidance car

Microcontrollers and Interfacing

EE283 Electrical Measurement Laboratory Laboratory Exercise #7: Digital Counter

Single Device Combines Pushbutton On/Off Control, Ideal Diode PowerPath and Accurate System Monitoring

Ultrasonic Multiplexer OPMUX v12.0

ZSCT1555 PRECISION SINGLE CELL TIMER ISSUE 2 - MAY 1998 DEVICE DESCRIPTION FEATURES APPLICATIONS SCHEMATIC DIAGRAM

Timer 0 Modes of Operation. Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Mode Phase Corrected PWM Mode

IT.MLD900 SENSORS AND TRANSDUCERS TRAINER. Signal Conditioning

CD4541BC Programmable Timer

CamJam EduKit Robotics Worksheet Six Distance Sensor camjam.me/edukit

Hardware Flags. and the RTI system. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Course Introduction. Content 20 pages 3 questions. Learning Time 30 minutes

Pin Symbol Wire Colour Connect To. 1 Vcc Red + 5 V DC. 2 GND Black Ground. Table 1 - GP2Y0A02YK0F Pinout

IRTC Clock Compensation Mechanism in the MCF51EM Family

Lesson 02: Sound Wave Production. This lesson contains 24 slides plus 11 multiple-choice questions.

INTEGRATED CIRCUITS. For a complete data sheet, please also download:

Controlling a Sprite with Ultrasound

Electronics Design Laboratory Lecture #9. ECEN 2270 Electronics Design Laboratory

Advanced Regulating Pulse Width Modulators

M-991 Call Progress Tone Generator

Analog to Digital Conversion

ELG3331: Digital Tachometer Introduction to Mechatronics by DG Alciatore and M B Histand

EE 221 L CIRCUIT II. by Ming Zhu

UNIVERSITY OF VICTORIA FACULTY OF ENGINEERING. SENG 466 Software for Embedded and Mechatronic Systems. Project 1 Report. May 25, 2006.

Exercise 5: PWM and Control Theory

G320X MANUAL DC BRUSH SERVO MOTOR DRIVE

The Marauder Map Final Report 12/19/2014 The combined information of these four sensors is sufficient to

Design and Control of a Crawling Robot

Transcription:

Measuring Distance Using Sound Distance can be measured in various ways: directly, using a ruler or measuring tape, or indirectly, using radio or sound waves. The indirect method measures another variable (e.g. time), in order to calculate the distance. Here we will review a practical way to measure short distances (2 cm up to 4 m) using ultrasonic waves. Sound waves travel through different materials by compressing and expanding the molecular arrangements (figure 1). FIGURE 1 Sound waves traveling from a source (speaker) to a receiver (microphone) Therefore, sound waves: Can only travel through a physical medium (no sound in vacuum). Will travel at a speed that varies with the material, based on the various molecular structures. We will be using sound waves traveling through the air, so all the calculations will be based on this fact. These waves can be characterized by 2 parameters: amplitude and frequency. Sound waves, in the most common definition, have a frequency range varying from 20 Hz to 20 khz, which is the human audible range. However, it is possible to generate waves below or above this frequency range; the latter are called ultrasonic waves. The use of ultrasonic waves is quite practical since they show the same behavior of any sound wave but do not annoy the user since they are outside the audible range. Be careful, however, since your pets (particularly dogs) may be quite sensitive to these waves. Time as a measure of distance There is a fundamental equation that relates Speed, Distance and Time: SPEED = DISTANCE / TIME C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 1

So the speed at which any object is moving is directly proportional to the distance traveled and inversely proportional to the time consumed to reach the destination. In brief, an object moves faster as it covers more distance in less time. The same equation applies to sound waves; in this sense, knowing 2 of the variables would be enough to calculate the third. The speed of sound in the different materials can be calculated knowing the various properties of the material; in case of dry air, the speed in meters per second (m/s) can be obtained using the following equation: SPEED = 331.4 + 0.6 x Tc Tc is the temperature measured in Celsius. This equation is quite precise for temperatures close to the ambient temperature (25 C), and a more complete formula should be used for much lower or higher temperatures (e.g. 200 C). For our purposes, we will consider a living environment range (10 C to 40 C), so the previous equation will be used. So, we have the speed, how do we obtain the time? This is quite simple. In figure 1, we just need to start the clock when the sound leaves the speaker, and stop when it arrives to the microphone. This setup, however, requires that we have synchronized circuits at both ends of the distance to measure, which is not very practical; figure 2 shows a more usable setup. FIGURE 2 The microphone captures the sound waves reflected by the object located at B With the sound source and receiver on the same side, it is much easier to control and monitor both with a single circuit, simplifying the time measurement task. C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 2

Now, with the speed and time known, the distance between A and B can be calculated as: DISTANCE = SPEED x TIME / 2 We need to divide by 2 the result since we are measuring the time from A to B and back to A, which is twice the distance we are trying to determine. Therefore, we have demonstrated that the distance between 2 points can be calculated by measuring the time it takes for a sound wave to travel between them. Practical implementation At this point we may start gathering a speaker, a microphone, some amplifiers and control circuits to simulate the scenario shown in figure 2. Fortunately, there is no need to do this, since there are inexpensive solutions already available that we may use to simplify the task. Figure 3 shows an ultrasonic distance sensor module that incorporates the speaker, the microphone and the required electronics to measure the sound travel time. FIGURE 3 Ultrasonic distance sensor module, HC-SR04 This device has 4 terminals; there are other modules with 3 terminals, with similar performance. However, the price of the 4-terminal units is only a fraction of the others; therefore I will be using these modules. If you prefer to use a 3-terminal module, the concepts are the same and just minor changes should be made in the setup and program. Besides Vcc and GND (these devices work on 5.0 V), there are 2 other terminals named Trig and Echo. The names are quite indicative of their function: Trig (trigger) receives a pulse to initiate the generation of the sound wave, while Echo outputs a pulse equal to the duration of the wave travel. Figure 4 shows the timing requirements and the working principles of the module. C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 3

FIGURE 4 HC-SR04 timing requirements The operation is initiated by sending a 10 µs pulse to the Trigger terminal. The module internally generates a burst of 8 cycles at 40 khz (ultrasonic) sent by one transducer (T - speaker). The Echo pin goes high until the burst is received back on the other transducer (R - microphone). The operation can be repeated indefinitely, provided there is a separation of at least 50 ms between triggers. The process control and signal processing can be achieved with a microcontroller, as shown in figure 5. FIGURE 5 A PIC 16F1825 can be used to control the HC-SR04 Using a PIC 16F1825, configured with the internal clock, there is no need to add any other component to perform the module test. The communication is achieved with 2 pins, RC2 connected to the Trigger and RA2 receiving the signal from Echo. The selection of RA2 is not random; this pin is the External Interrupt Input of this PIC, and we will be using it in our program. C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 4

Basic program In order to control the ultrasonic module and measure the resulting propagation delay, the program must: Send a pulse (10 µs or longer) through RC2. When RA2 becomes HIGH (Echo initiated the pulse) start a counter. When RA2 becomes LOW (Echo terminated the pulse) read the counter contents. Restart the process after 50 ms or more. This basic routine will give the total travel time of the sound wave (going and returning back). Knowing the speed of sound, as discussed above, it is just a matter of a simple calculation to determine the distance to the object where the wave bounced. Here is the program in CCS C: C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 5

And this is the configuration file (ultrasonic_range.h): The first thing to notice is that the PIC is using the internal oscillator at a relatively low speed (4 MHz); this is on purpose, to achieve very long time periods with the Timer0 and Timer1. In the main program, Timer0 (8 bits) is set to increase 1 count every 256 instruction clock pulses. Given that the instruction clock period is 1 µs (4 times the oscillator period) then Timer0 is increased every 256 µs. Therefore, Timer0 will overflow at 256 increments x 256 µs/increment = 65536 µs (approximately 65.5 ms as noted in the program comments). Timer1 (16 bits) is increased with every instruction clock pulse, so it will overflow at 65536 increments x 1 µs/increment = 65536 µs. With this configuration, both timers overflow with the same interval. Two interrupts are enabled: Timer0 and External. Timer0 interrupt is triggered every time the timer overflows, therefore the ISR (Interrupt Service Routine) will be called every 65.5 ms. We will use this ISR to trigger the ultrasound module; as you may recall, there should be at least 50 ms separation between cycles, so 65.5 ms is perfect for our purposes. Inside the Timer0 ISR the TRG pin (RC2) is raised, wait 6 µs and then lowered. You may wonder why only 6 µs if the trigger pulse must be 10 µs or more? The other instructions take time as well, so the actual pulse duration is in fact 10 µs. After this, the External Interrupt Edge is set, waiting for a LOW to HIGH transition from the Echo pin on RA2. The program leaves the ISR and continues inside the while(true) loop. When Echo starts the measuring pulse, so a LOW to HIGH transition appears in RA2, the External Interrupt ISR is called. Since RA2 is now HIGH, then Timer1 is reset (starts in 0), and the Interrupt Edge is now set from HIGH to LOW. The program leaves the ISR. The sound wave will eventually reach the module after bouncing with the object, so Echo will go LOW. This transition triggers the External Interrupt again, but now RA2 will be LOW. At this point, the variable time will receive the contents of Timer1: this is the total travel time in µs. If the sound wave never returns, i.e. there is no object to bounce, the Echo pin will return to LOW after 38 ms, so the program will always update the variable time, but we must be aware that any time longer than 25 ms should not be considered as a valid measure. To understand this previous statement, let s review the next steps in the program. Within the while(true) loop, speed is calculated using the equation previously presented: SPEED = 331.4 + 0.6 x Tc C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 6

The only difference is that instead of meters per second (m/s) we are using centimeters per second (cm/s), so both terms of the addition are multiplied by 100. This has an obvious advantage: we keep precision without using floating point numbers, so everything is faster and uses less memory. The variables must be defined as int32. The temperature has been set as 25 C, and may be changed if desired. The ideal setup should include a temperature sensor connected to the PIC, so the actual ambient temperature is used. The distance can now be calculated by multiplying speed and time, divided by 2 (to measure only one way of the sound trip). Six zeros are added to convert the time from µs to s. The result is given in centimeters (cm). Back to the previous statement, that any time longer than 25 ms (or 25,000 µs) should not be considered a valid measure, let s calculate the distance for this time (at 25 C): Distance = (33,140 + (60 x 25)) x 25,000 / 2,000,000 = 433 cm The HC-SR04 datasheet indicates a maximum range of around 4 m (400 cm), so 433 cm may be possible, but the results are not guaranteed beyond that point. What s next? With the distance already calculated and stored in the variable distance, the next step would be to do something with the information. This is limited only by the imagination (or actual needs) of the user, and it can go from showing the results in an LCD alphanumeric or graphic display, to transmitting the results via radio signals to a remote location or even showing them in a web page. The sensor could be mounted in a servo mechanism that oscillates covering half a circle, thus creating a sonar device. The applications are endless, using this low cost module and the simple program described above. C o p y r i g h t 2 0 1 6 M a r c e l o M a g g i Page 7