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. 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.
Connecting electronic modules
1. Connect the motors to the M1 and M2 ports of the LOFI Brain. Make sure the small metal walls inside the pins of the motor cables are facing the plastic walls of the pins on the controller.
2. Connect the powerbank to the micro USB slot on the Adapter.
3. Connect the distance sensor to the corresponding slot on the Adapter. Make sure you have connected the pins VCC, TRIG, ECHO, and GND as shown in the picture below.
4. If you are using a Bluetooth module, connect it to the corresponding slot on the adapter. Make sure you put the module in the right direction, see the picture below.
The code
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.