Ultrasonic distance sensor

Lecture



Ultrasonic distance sensor

SRF04 Ultrasonic Distance Sensor

An ultrasonic distance sensor detects the distance to an object by measuring the time it takes to display a sound wave from an object. The frequency of the sound wave is within the ultrasound frequency, which provides a concentrated direction of the sound wave, since the sound with a high frequency is dissipated in the environment less. A typical ultrasonic distance sensor consists of two membranes, one of which generates sound, and the other registers the displayed echo. Figuratively speaking, we are dealing with a sound column and a microphone. The sound generator creates a small, with a certain period, ultrasonic pulse and starts a timer. The second membrane registers the arrival of the displayed pulse and stops the timer. From the time of the timer on the speed of sound it is possible to calculate the distance of the sound wave. The distance of the object is approximately half the distance traveled by the sound wave.

Ultrasonic distance sensor

The principle of operation of the ultrasonic distance sensor

An ultrasonic distance sensor in everyday life has many uses. They are used instead of measuring tape in devices for measuring distance, for example in construction. Modern cars are equipped with an ultrasonic sensor and a warning signal to protect against hitting the obstacle standing behind. In addition to measuring the distance, they can also register the location of the object in the measured range, for example, in the danger zone of production machines. If the emitter and the ultrasound receiver are divided, then it is possible to measure the flow rate of the substance flowing between them, because the sound wave against the flow propagates more slowly, and with the flow faster.

Practice

The Home Sensors “Sensors” module kit contains an ultrasonic distance sensor Devantech SRF04 or SRF05. The SRF04 / SRF05 is only a sensor that does not directly provide distance information. In addition to the power supply pins, the sensor also has trigger output and echo output. When setting the trigger output high sensor generates an 8-period 40 kHz ultrasonic wave. At this point, the echo output becomes high and remains high until the displayed sound wave reaches the sensor. Thus, the echo signal indicates the time during which the sound propagates to the object and back. Measuring this time, multiplying it by the speed of propagation and dividing by two, you can get the distance to the object. The adjacent graph represents the relationship between time and signals, the emitter of the sound wave and the echo.

Ultrasonic distance sensor

SRF04 signals

To use a Devantech ultrasonic sensor with an AVR microcontroller, you need to connect the trigger and echo pins to any AVR pins. To measure time, it is desirable to use a 16-bit timer, for example, timer3 . The following is a function that performs the entire measurement procedure - it generates a trigger signal, starts a timer, measures the length of the echo signal and converts it to distance in centimeters. The function is blocking, i.e. the processor is busy with this until the measurement result is obtained or the measurement is delayed for longer than the allowed. The faster the echo arrives, the faster we get the measurement result. If the echo does not arrive, the function waits for this ~ 36 ms and returns 0. It is important between the measurement to leave a pause of several tens of milliseconds, so that the sound wave from the previous measurement has time to subside and not disrupt the new measurement. If several ultrasonic sensors are used at the same time, then it will also be necessary to ensure that the sound waves do not intersect.

 #define ULTRASONIC_SPEED_OF_SOUND 33000 // cm / s

 //
 // Ultrasound measurement by distance sensor
 //
 unsigned short ultrasonic_measure (pin trigger, pin echo)
 {	
	 // Set pins
	 pin_setup_output (trigger);
	 pin_setup_input_with_pullup (echo);

	 // Timer 3 to normal mode
	 // with a period of F_CPU / 8
	 timer3_init_normal (TIMER3_PRESCALE_8);

	 // Output trigger high
	 pin_set (trigger);

	 // Reset timer
	 timer3_overflow_flag_clear ();
	 timer3_set_value (0);	

	 // Waiting ~ 10 us
	 while (timer3_get_value () <18) {}

	 // Output trigger low
	 pin_clear (trigger);

	 // Waiting for the start of the echo signal
	 while (! pin_get_value (echo))
	 {
		 // Expected too long?
		 if (timer3_overflow_flag_is_set ())
		 {
			 return 0;
		 }
	 }

	 // Reset timer
	 timer3_set_value (0);

	 // Waiting for the end of the echo signal
	 while (pin_get_value (echo))
	 {
		 // Expected too long?
		 if (timer3_overflow_flag_is_set ())
		 {
			 return 0;
		 }
	 }

	 // Converting the measured time to distance
	 // distance = time * (1 / (F_CPU / 8)) * sound speed / 2
	 return (unsigned long) timer3_get_value () *
		 ULTRASONIC_SPEED_OF_SOUND / (F_CPU / 4);
 } 

This function allows the user to select the trigger and echo pins so that the sensor can be connected to where it is more convenient or where there is a place. In addition, the freedom to choose the conclusions makes it possible to use the function not only in the complete set of the Home Lab. This function belongs to the library of the Home Lab, which allows you not to write it separately in your program. But it should be taken into account that in the library of the HomeLab this function is rigidly connected with the clock frequency of 14.7456 Mhz of the module “Controller” of the HomeLab and at other clock frequencies the function will give an incorrect result. With a different clock frequency, you will have to write this function yourself in your program. The following program code demonstrates the use of the SRF04 / SRF05 ultrasound transducer with the Home Lab library.

 //
 // Example of the program of the ultrasonic sensor of the HomeLab.
 // The distance measurement function is blocking.
 //
 #include 
 #include 
 #include 
 #include 
 #include 

 //
 // Ultrasonic Sensor Pins
 //
 pin pin_trigger = PIN (G, 1);
 pin pin_echo = PIN (G, 0);

 //
 // Main program
 //
 int main (void)
 { 		 
	 unsigned short distance;
	 char text [16];	

	 // Setting the LCD screen
	 lcd_alpha_init (LCD_ALPHA_DISP_ON);

	 // Clear LCD Screen
	 lcd_alpha_clear ();

	 // The name of the program
	 lcd_alpha_write_string ("Ultrasound");

	 // Little pause
	 sw_delay_ms (100);

 	 // Endless cycle
	 while (true)
	 {
		 // Measurement
		 distance = ultrasonic_measure (pin_trigger, pin_echo);

		 // Measurement failed?
		 if (distance> 0)
		 {			
			 // Converting distance to text
			 sprintf (text, "% d cm", distance);
		 }
		 // Has an error occurred during the measurement?
		 else
		 {
			 // Error text
			 sprintf (text, "Error");
		 }			

		 // Display text at the beginning of the second row of LCD
		 lcd_alpha_goto_xy (0, 1);
		 lcd_alpha_write_string (text);

		 // Little pause
		 sw_delay_ms (500);
	 }
 } 
Область применения: данное устройство может применяться в робототехнике, для навигации роботов, поиска преград и пути их обхождения.

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Sensors

Terms: Sensors