In this tutorial we will learn how to use HC-SR04 Ultrasonic sensor with Raspberry Pi Pico to measure the obstacle distance and use integrated LED as a warning. Once the distance is smaller then 10cm the LED will activate and the shorter the distance the faster the LED will blink.
Watch the video!
"Map Range Analog" component will transform our distance from the ultrasonic sensor to the Frequency of the LED blinking
Select "MapRange1" and in the properties window set "Input Range" > "Max" to 10 <<this means that we will monitor the distance from 0 cm to 10 cmand "Output Range" > "Max" to 1 and "Output Range" > "Minn" to 5 << this means that the blinking frequency of the LED will be from 1Hz to 5HzAdd "Compare Analog Value" componentSelect "Compare1" and in the properties window set "Compare Type" to CtSmaller and "Value" to 10 <<This means if the Input value is smaller then 1o the output will be TrueAdd "Pulse Generator" componentSelect "PulseGenerator1" and in the properties window select "Enabled" and click on the pin icon and select "Boolean SinkPin"Select "PulseGenerator1" and in the properties window select "Frequency" and click on the pin icon and select "Float SinkPin"Add "OLED I2C" componentDouble click on the "DisplayOLED1" and in the "Elements" window drag "Draw Text" to the left side and in the Properties window set "Size" to 2 and "Text" to DistanceDrag "Text Field" to the left side and in the Properties window set "Size" to 2 and "Y" to 30Close the "Elements" window![](https://dfimg.dfrobot.com/65734cc9aa9508d63a434f3e/community/5265e7ef48e49ebea6bdfe6c836947bf.jpg)
![](https://dfimg.dfrobot.com/65734cc9aa9508d63a434f3e/community/9f7af8bee4fb341f606fdf4caede09e0.jpg)
![](https://dfimg.dfrobot.com/65734cc9aa9508d63a434f3e/community/7963851ffce5ce418973daa3cb6de551.png)
If you power the Raspberry Pi Pico/RP2040 the OLED Display will start to show the distance of the obstacle detected by ultrasonic sensor and if the distance is smaller then 10cm the integrated LED on the Raspberry Pi Pico/RP2040 will be activated. The LED will blink faster if the distance is smaller.
Congratulations! You have completed your project with Visuino. Also attached is the Visuino project, that I created for this tutorial, you can download it and open it in Visuino: https://www.visuino.com
![licensBg](/images/license_bg.png)