Ultrasonic sensors are used to accurately measure the distance to an obstacle. They work by calculating the time it takes the sound to travel through air, hit the object and bounce back to the sensor. You can use the Breadboard read this signal and with a simple formula calculate accurately the exact distance.
Add the Breadboard Block to the canvas and open its Properties Panel:
Click on the ‘Visual Map’ button and configure pins 1, 2, 3 and 4 as follows:
Ultrasonic sensors normally have 4 to 5 pins, the one used in this article has 5, but only 4 are required since the last pin (GND) is duplicated. Regardless of the number of pins, always make sure the sensor you are using works at 3.3V. There are multiple alternatives to connect the sensor to your Breadboard, depending on the type of connectors you have; for instance, you could use a 4 wire male to female connector, or two 3 wire male to female cable:
Connect the cable to your Breadboard Block making sure the line from the VCC pin on the ultrasonic board is connected to pin 1:
We are now ready to create our program. First, we need to convert the data the sensor produces into something we can understand. Ultrasonic sensors measure the time (in microseconds) a sound signal takes to travel from the sensor to the object/obstacle and bounce back to the sensor, the Breadboard reads this information through the ‘Echo’/’Rx’ pin of the ultrasonic board. Our program needs to convert these microseconds into centimetres by multiplying the value by 0.017 which is the speed of sound in air (in centimetres per microsecond) divided by two, for this we need to go inside the Breadboard’s Logic Maker and add the following gates:
Input -> Multiplication -> Lower Than -> Display
NOTE: The ‘Lower Than’ gate is there to filter noise which sometimes produces very large numbers.
Hit ‘Apply’ and the program should come to life.
Move your hand close to the sensor to see the values change:
If you don’t want to see the decimal places, use a “Parser” gate to round the value:
If you want to see only 1 decimal place, the “Parser” gate has the ability to do this; from the dropdown, select ‘To Float’ and type the number of decimal places you wish to display, then hit Apply:
Visualising The Distance
If you want to graph the calculated distance, you can use an ‘Output’ gate to make this value available to widgets in the Dashboard.
Add an Output gate and give it a name:
Hit ‘Apply’ before closing the Logic Maker and go to the Dashboard:
Add a Data Graph widget and from its Properties Panel select ‘distance’ as the Data Source:
Move your hand close to the Ultrasonic Sensor and you should see the distance displayed: