This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:sensor:ultrasonic_distance [2013/06/03 08:02] – rellermaa | 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 ===== | ||
- | [{{ : | + | [{{ : |
Ultrasonic distance sensor determines the distance to an object by measuring the time taken by the sound to reflect back from that object. The frequency of the sound is somewhere in the range of ultrasound, this ensures more concentrated direction of the sound wave because sound at higher frequency dissipates less in the environment. A typical ultrasonic distance sensor consists of two membranes. One membrane produces sound, another catches reflected echo. Basically they are speaker and microphone. The sound generator generates short (the length is a couple of periods) ultrasonic impulses and triggers the timer. Second membrane registers the arrival of the sound impulse and stops the timer.From the timers time it is possible to to calculate the distance traveled by the sound. The distance to the object is half of the distance traveled by the sound wave. | Ultrasonic distance sensor determines the distance to an object by measuring the time taken by the sound to reflect back from that object. The frequency of the sound is somewhere in the range of ultrasound, this ensures more concentrated direction of the sound wave because sound at higher frequency dissipates less in the environment. A typical ultrasonic distance sensor consists of two membranes. One membrane produces sound, another catches reflected echo. Basically they are speaker and microphone. The sound generator generates short (the length is a couple of periods) ultrasonic impulses and triggers the timer. Second membrane registers the arrival of the sound impulse and stops the timer.From the timers time it is possible to to calculate the distance traveled by the sound. The distance to the object is half of the distance traveled by the sound wave. | ||
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. |
- | [{{ : | + | 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 |
- | + | ||
- | 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 |
- | // Instant ultrasonic | + | unsigned short ultrasonic_measure_srf05(pin triggerecho) |
- | // | + | { |
- | unsigned short ultrasonic_instant_measure(pin trigger, pin echo) | + | // Pin setup |
- | { | + | pin_setup_output(triggerecho); |
- | // Pin setup | + | |
- | pin_setup_output(trigger); | + | |
- | pin_setup_input_with_pullup(echo); | + | |
- | // Set timer 3 to normal mode | + | |
- | // with period of F_CPU / 8 | + | // with period of F_CPU / 8 |
- | timer3_init_normal(TIMER3_PRESCALE_8); | + | timer3_init_normal(TIMER3_PRESCALE_8); |
- | // Create trigger pulse | + | |
- | pin_set(trigger); | + | pin_set(triggerecho); |
- | + | ||
- | // Reset timer | + | // Reset timer |
- | timer3_overflow_flag_clear(); | + | timer3_overflow_flag_clear(); |
- | timer3_set_value(0); | + | timer3_set_value(0); |
- | + | ||
- | // Wait ~10 us | + | // Wait ~10 us |
- | while (timer3_get_value() < 18) {} | + | while (timer3_get_value() < 18) {} |
- | + | ||
- | // End trigger pulse | + | // End trigger pulse |
- | pin_clear(trigger); | + | pin_clear(triggerecho); |
- | + | ||
- | // Wait for echo start | + | //short delay |
- | while (!pin_get_value(echo)) | + | sw_delay_ms(1); |
- | { | + | |
- | // Timeout ? | + | //set the pin as input |
- | if (timer3_overflow_flag_is_set()) | + | pin_setup_input_with_pullup(triggerecho); |
- | { | + | |
- | return 0; | + | // Wait for echo start |
- | } | + | while (!pin_get_value(triggerecho)) |
- | } | + | { |
- | + | // Timeout ? | |
- | // Reset timer again | + | if (timer3_overflow_flag_is_set()) |
- | timer3_set_value(0); | + | { |
- | + | return 0; | |
- | // Wait for echo end | + | } |
- | while (pin_get_value(echo)) | + | } |
- | { | + | |
- | // Timeout ? | + | // Reset timer again |
- | if (timer3_overflow_flag_is_set()) | + | timer3_set_value(0); |
- | { | + | |
- | return 0; | + | // Wait for echo end |
- | } | + | while (pin_get_value(triggerecho)) |
- | } | + | { |
- | + | // Timeout ? | |
- | // Convert time to distance: | + | if (timer3_overflow_flag_is_set()) |
- | // | + | { |
- | return (unsigned long)timer3_get_value() * | + | return 0; |
- | ULTRASONIC_SPEED_OF_SOUND / (F_CPU / 4); | + | } |
+ | } | ||
+ | |||
+ | // Convert time to distance: | ||
+ | // | ||
+ | return (unsigned long)timer3_get_value() * | ||
+ | ULTRASONIC_SPEED_OF_SOUND / (F_CPU / 4); | ||
} | } | ||
</ | </ | ||
- | Presented function allows the user to choose the echo pin and the trigger pin, so the sensor can be connected where it is more suitable or where is more space. In addition, the freedom of choosing the pins allows to use the function also elsewhere than only in the HomeLab. Presented function belongs already to the library of the HomeLab so it is not necessary to write it. One thing must be remembered: the function in the library of the HomeLab is stiffly connected to the clock rate of the Controller module of the HomeLab. | + | Presented function allows the user to choose the echo/trigger pin, so the sensor can be connected where it is more suitable or where is more space. In addition, the freedom of choosing the pins allows to use the function also elsewhere than only in the HomeLab. Presented function belongs already to the library of the HomeLab so it is not necessary to write it. One thing must be remembered: the function in the library of the HomeLab is stiffly connected to the clock rate of the Controller module of the HomeLab. |
<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 93: | Line 96: | ||
#include < | #include < | ||
- | // | ||
// Pins of ultrasonic sensor | // Pins of ultrasonic sensor | ||
- | // | + | // Robotic HomeLab II |
- | pin pin_trigger | + | //pin pin_pin_trigger_echo |
- | pin pin_echo | + | |
+ | // Robotic HomeLab III | ||
+ | pin pin_trigger_echo | ||
- | // | ||
// Main program | // Main program | ||
- | // | ||
int main(void) | int main(void) | ||
{ | { | ||
Line 107: | Line 109: | ||
char text[16]; | char text[16]; | ||
+ | // Robotic HomeLab II | ||
// Switching to external sensors | // Switching to external sensors | ||
+ | /* | ||
pin ex_sensors = PIN(G, 0); | pin ex_sensors = PIN(G, 0); | ||
pin_setup_output(ex_sensors); | pin_setup_output(ex_sensors); | ||
pin_set(ex_sensors); | pin_set(ex_sensors); | ||
- | + | */ | |
// Initialization of LCD | // Initialization of LCD | ||
lcd_gfx_init(); | lcd_gfx_init(); | ||
Line 119: | 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 | ||
- | lcd_gfx_write_string(" | + | lcd_gfx_write_string(" |
- | // Little | + | // Little |
sw_delay_ms(100); | sw_delay_ms(100); | ||
// Endless loop. | // Endless loop. | ||
- | while (true) | + | while (1) |
{ | { | ||
// Measuring | // Measuring | ||
- | distance = ultrasonic_measure_srf04(pin_trigger, | + | distance = ultrasonic_measure_srf05(pin_trigger_echo); |
// Was the measuring successful ? | // Was the measuring successful ? | ||
Line 137: | 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 150: | Line 155: | ||
lcd_gfx_write_string(text); | lcd_gfx_write_string(text); | ||
- | // Little | + | // Little |
sw_delay_ms(500); | sw_delay_ms(500); | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | To make sure that the ultrasonic sensor actually start working, you should check whether the small LED will flash every measurement which is located on the back of the sensor. |