Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:examples:sensor:ultrasonic_distance [2015/11/10 12:24] – heikopikner | en:examples:sensor:ultrasonic_distance [2020/07/20 09:00] (current) – external edit 127.0.0.1 |
---|
====== Ultrasonic distance sensor ====== | ====== Ultrasonic distance sensor ====== |
| |
//Necessary knowledge: [HW] [[en:hardware:homelab:controller]], [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:timers]], [LIB] [[en:software:homelab:library:timer]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]// | //Necessary knowledge: |
| [HW] [[en:hardware:homelab:digi]], |
| [AVR] [[en:avr:timers]], |
| [LIB] [[en:software:homelab:library:timer]], \\ |
| [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]] // |
| |
===== Theory ===== | ===== Theory ===== |
<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 |
</code> | </code> |
| |
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. Using the function with other clock-rates, it would give incorrect result. To use the function with other clock-rates, it should be written in the program manually. Following code of program demonstrates the use of SRF04/SRF05 ultrasonic sensor with the library 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. Using the function with other clock-rates, it would give incorrect result. To use the function with other clock-rates, it should be written in the program manually. Following code of program demonstrates the use of SRF04/SRF05 ultrasonic sensor with the library of the HomeLab. When connecting the sensor it is very important to observe the power polarity. Incorrectly connected sensor becomes broken. |
| |
<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 <stdio.h> | #include <stdio.h> |
#include <homelab/pin.h> | #include <homelab/pin.h> |
#include <homelab/module/lcd_gfx.h> | #include <homelab/module/lcd_gfx.h> |
| |
// | |
// 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 selection | // Line selection |
lcd_gfx_goto_char_xy(1,2); | lcd_gfx_goto_char_xy(1,1); |
| |
// Name of the program | // Name of the program |
| |
// Endless loop. | // Endless loop. |
while (true) | while (1) |
{ | { |
// Measuring | // Measuring |
{ | { |
// converting the distance to text. | // converting the distance to text. |
sprintf(text, "%d cm ", distance); | sprintf(text, "%3d cm ", distance); |
} | } |
// Were there errors during the measuring ? | // Were there errors during the measuring ? |
} | } |
</code> | </code> |
If older type sensor (SRF04) is used, then see page: \\ http://home.roboticlab.eu/en/examples/sensor/ultrasonic_distance_srf04 | 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. |