Distance sensor is one of the most popular electronic components used in robotics. It’s highly useful and also a bit “magical”, because at first sight, you can’t tell how exactly it works.
The distance sensor that we use is the popular Arduino HC-SR04. It works based on ultrasound and echolocation. The sensor has two probes (that look like robot eyes in our case): one is a speaker that sends series of short “beeps”, the other is a microphone that records ultrasound signals bouncing from objects in front of the sensor.
We can’t hear the sounds sent by the sensor, because their frequency is higher than the spectrum of human hearing. The “beeps” frequency is 40 kHz while people can hear sounds up to 20 kHz.
Because the speed of sound is constant (1235 km/h or 767 mph), the sensor can measure distance by measuring the time from sending a “beep” to receiving it back, bounced from an object. That’s how sonars on ships operate. A similar mechanism can be also observed in nature with bats, dolphins and other cetaceans.
Distance sensor will allow our robot to “see” what’s in front of him. We can use the sensor to program algorithms for reaction to signals from the distance sensor.
Preparations
- You will need the Rover.
- Connect the Rover with LOFI Blocks.
Programming
In order to read the distance measured by the distance sensor in LOFI Blocks, we will use the DISTANCE SENSOR block from ROBOT category.
The block returns distance measured in centimeters, and if the sensor doesn’t sense any obstacle, the block returns value of 100. Before programming, let’s see if our distance sensor works properly. Put your hand close to the sensor, and if the sensor monitor reacts by moving the slider, that means the sensor works fine.
Excercise 1 – Move when you see something.
This program will instruct the robot to move forward when it sees something close enough, and stay in place when there’s nothing close. Let’s assume that we want our robot to react when there’s something closer than 20 cm. That means the measurement from the sensor needs to be less than 20.
And…
- If the reading from the sensor is less than 20, go forward.
- IF the reading from the sensor is more than 20, stay.
We will use a conditional instruction to translate all of the above into instructions:
Now if we play our program, the robot will move forward if we put our hand in front of him closer than 20 cm, otherwise, it will stop.
Excercise 2 – Move if you see something. Move back when you are too close.
Let’s upgrade our program, and instruct our robot to be able to get close to an object without running into it, and even move back when the distance is too close.
Breaking down the idea behind the program, the instructions will go like this:
- If you don’t see anything, stay in place.
- If you see something closer than 30 cm and more than 20 cm, go forward.
- If your distance is less than 20 cm and more than 10 cm, stop.
- If your distance is less than 10 cm, move back.
These double conditions can be programmed with a special block from the LOGICS category.
This block will return value TRUE only when both its conditions will be met.
Let’s go ahead and put the new instructions into our program.
So now our robot will behave like this:
Notice. If your robot doesn’t react, check if the power set for the motors isn’t too low. We recommend setting the power higher than 55.
As you can see, with the help of our distance sensor, we can write algorithms that make our robots behave similarly to living organisms. In the next lesson, we will upgrade our program even more and teach our robot to be more autonomous.