This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:sensor:ultrasonic_distance [2015/11/02 11:57] – raivo.sell | en:examples:sensor:ultrasonic_distance [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Ultrasonic distance sensor ====== | ====== Ultrasonic distance sensor ====== | ||
- | //Necessary knowledge: | + | //Necessary knowledge: |
+ | [HW] [[en: | ||
+ | [AVR] [[en: | ||
+ | [LIB] [[en: | ||
+ | [LIB] [[en: | ||
===== Theory ===== | ===== Theory ===== | ||
Line 15: | Line 19: | ||
===== Practice ===== | ===== Practice ===== | ||
- | HomeLab is equipped with Devantech SRF04/SRF05 ultrasonic distance sensor. SRF04/SRF05 is just a sensor and it does not give direct information about the distance. Besides the power supply pins the sensor has also a triggering pin and a echo pin. When the triggering pin is set high the sensor generates 40 kHz ultrasonic wave which is 8 periods long. At that moment the echo pin becomes high and remains high until the reflected sound is reached back to the sensor. So the echo signal reflects basically the time during which the sound reaches to the object and comes back from the object. By measuring that time and multiplying it by the speed of sound and then divide it by two, is calculated the distance to the object. | + | HomeLab is equipped with Devantech SRF04/SRF05 ultrasonic distance sensor. SRF04/SRF05 is just a sensor and it does not give direct information about the distance. Besides the power supply pins the sensor has also a triggering pin and a echo pin. One important difference between the sensor SRF04 and SRF05 is that it is possible in case of SRF05 use a single physical connector pins for the trigger and the echo signal. This allows the sensor to use the standard three-wire connector (power, ground and signal). When the triggering pin is set high the sensor generates 40 kHz ultrasonic wave which is 8 periods long. At that moment the echo pin becomes high and remains high until the reflected sound is reached back to the sensor. So the echo signal reflects basically the time during which the sound reaches to the object and comes back from the object. By measuring that time and multiplying it by the speed of sound and then divide it by two, is calculated the distance to the object. |
- | A new operating mode (tying the mode pin to ground) allows the SRF05 to use a single pin for both trigger and echo. | + | To use the SRF04/SRF05 with the AVR, its trigger pin and echo pin should be connected to some of the AVR pins. For measuring time, it is suitable to use a 16-bit timer, for example //timer3//. Following is a function which is based on the Atmega2561 controller and executes all measuring procedures – it generates the signal of the trigger, starts the timer, measures the length of the echo signal and converts it to the distance in centimeters. The function is blocking, meaning the processor is occupied by it until the result of measuring is received or the measuring takes too long time. The faster the echo arrives the faster the result is returned. If the echo does not arrive, the function waits for it for 36 ms and returns to 0. It is important to leave approximately 20 ms break between each measuring, to able the sound generated during the previous measuring to die out and the new measuring will not be affected. It is important to notice that the sound waves don’t disturb each other, when several ultrasonic sensors are used simultaneously |
- | When the mode pin is left unconnected, | + | |
- | The following graph represents the relationship of time and the signals of sound transmitter, | + | |
- | + | ||
- | [{{ : | + | |
- | + | ||
- | To use the SRF04/SRF05 with the AVR, its trigger pin and echo pin should be connected to some of the AVR pins. For measuring time, it is suitable to use a 16-bit timer, for example //timer3//. Following is a function which executes all measuring procedures – it generates the signal of the trigger, starts the timer, measures the length of the echo signal and converts it to the distance in centimeters. The function is blocking, meaning the processor is occupied by it until the result of measuring is received or the measuring takes too long time. The faster the echo arrives the faster the result is returned. If the echo does not arrive, the function waits for it for 36 ms and returns to 0. It is important to leave approximately 20 ms break between each measuring, to able the sound generated during the previous measuring to die out and the new measuring will not be affected. It is important to notice that the sound waves don’t disturb each other, when several ultrasonic sensors are used simultaneously | + | |
<code c> | <code c> | ||
#define ULTRASONIC_SPEED_OF_SOUND 33000 // cm/s | #define ULTRASONIC_SPEED_OF_SOUND 33000 // cm/s | ||
- | // | ||
// Ultrasonic distance measuring | // Ultrasonic distance measuring | ||
- | // | + | unsigned short ultrasonic_measure_srf05(pin |
- | + | ||
- | unsigned short ultrasonic_measure_srf05(pin | + | |
{ | { | ||
// Pin setup | // Pin setup | ||
- | pin_setup_output(srf05); | + | pin_setup_output(triggerecho); |
// Set timer 3 to normal mode | // Set timer 3 to normal mode | ||
Line 42: | Line 37: | ||
// Create trigger pulse | // Create trigger pulse | ||
- | pin_set(srf05); | + | pin_set(triggerecho); |
| | ||
// Reset timer | // Reset timer | ||
Line 52: | Line 47: | ||
| | ||
// End trigger pulse | // End trigger pulse | ||
- | pin_clear(srf05); | + | pin_clear(triggerecho); |
| | ||
//short delay | //short delay | ||
Line 58: | Line 53: | ||
| | ||
//set the pin as input | //set the pin as input | ||
- | pin_setup_input_with_pullup(srf05); | + | pin_setup_input_with_pullup(triggerecho); |
| | ||
// Wait for echo start | // Wait for echo start | ||
- | while (!pin_get_value(srf05)) | + | while (!pin_get_value(triggerecho)) |
{ | { | ||
// Timeout ? | // Timeout ? | ||
Line 74: | Line 69: | ||
| | ||
// Wait for echo end | // Wait for echo end | ||
- | while (pin_get_value(srf05)) | + | while (pin_get_value(triggerecho)) |
{ | { | ||
// Timeout ? | // Timeout ? | ||
Line 90: | Line 85: | ||
</ | </ | ||
- | Presented function allows the user to choose the echo/ | + | Presented function allows the user to choose the echo/ |
<code c> | <code c> | ||
- | // | ||
// The example program of the ultrasonic distance sensor of the HomeLab | // The example program of the ultrasonic distance sensor of the HomeLab | ||
// Measuring the distance is blocking. | // Measuring the distance is blocking. | ||
- | // | ||
#include < | #include < | ||
#include < | #include < | ||
Line 103: | Line 96: | ||
#include < | #include < | ||
- | // | ||
// Pins of ultrasonic sensor | // Pins of ultrasonic sensor | ||
// Robotic HomeLab II | // Robotic HomeLab II | ||
- | pin pin_pin_trigger_echo = PIN(F, 2); | + | //pin pin_pin_trigger_echo = PIN(F, 2); |
// Robotic HomeLab III | // Robotic HomeLab III | ||
Line 132: | Line 124: | ||
// Line selection | // Line selection | ||
- | lcd_gfx_goto_char_xy(1, | + | lcd_gfx_goto_char_xy(1, |
// Name of the program | // Name of the program | ||
Line 141: | Line 133: | ||
// Endless loop. | // Endless loop. | ||
- | while (true) | + | while (1) |
{ | { | ||
// Measuring | // Measuring | ||
Line 150: | Line 142: | ||
{ | { | ||
// converting the distance to text. | // converting the distance to text. | ||
- | sprintf(text, | + | sprintf(text, |
} | } | ||
// Were there errors during the measuring ? | // Were there errors during the measuring ? | ||
Line 168: | Line 160: | ||
} | } | ||
</ | </ | ||
- | If older type sensor | + | To make sure that the ultrasonic |