Ultrasonic Sensor Hc-sr04 With Warning Led Raspberry Pi Pico
About the project
In this tutorial we will learn how to use HC-SR04 Ultrasonic sensor with Raspberry Pi Pico to measure the obstacle distance.
Project info
Difficulty: Easy
Platforms: Adafruit, Arduino, Raspberry Pi, Seeed Studio, Visuino
Estimated time: 1 hour
License: GNU General Public License, version 3 or later (GPL3+)
Items used in this project
Hardware components
Story
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!
Step 1: Raspberry Pi Pico- Raspberry Pi Pico RP2040
- HC-SR04 Ultrasonic sensor
- OLED I2C Display
- Breadboard
- Jumper wires
- Visuino program: Download Visuino
- Connect OLED Display pin [VCC] to Pi Pico RP2040 pin [VBUS]
- Connect OLED Display pin [GND] to Pi Pico RP2040 pin [GND]
- Connect OLED Display pin [SDA] to Pi Pico RP2040 pin [4]
- Connect OLED Display pin [SCL] to Pi Pico RP2040 pin [5]
- Connect HC-SR04 pin [VCC] to Pi Pico RP2040 pin [VBUS]
- Connect HC-SR04 pin [GND] to Pi Pico RP2040 pin [GND]
- Connect HC-SR04 pin [Trigger] to Pi Pico RP2040 pin [3]
- Connect HC-SR04 pin [Echo] to Pi Pico RP2040 pin [2]
Start Visuino as shown in the first picture Click on the "Tools" button on the Arduino component (Picture 1) in Visuino When the dialog appears, select "Raspberry Pi Pico (RP2040)" as shown on Picture 2
Step 4: In Visuino Add & Set Components- Add "Ultrasonic Ranger" component
- Add "Analog Multi Source" component
- Select "MultiSource1" and in the properties window set "Output Pins" to 3
- Add "Map Range Analog" component
"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 cm
- and "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 5Hz
- Add "Compare Analog Value" component
- Select "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 True
- Add "Pulse Generator" component
- Select "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" component
- Double 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 Distance
- Drag "Text Field" to the left side and in the Properties window set "Size" to 2 and "Y" to 30
- Close the "Elements" window
1 / 3
- Connect "UltrasonicRanger1" pin [Echo] to Raspberry Pi Pico/RP2040 digital pin [2]
- Connect "UltrasonicRanger1" pin [Ping-Trigger] to Raspberry Pi Pico/RP2040 digital pin [3]
- Connect "UltrasonicRanger1" pin [Out] to "MultiSource1" pin [In]
- Connect "MultiSource1" pin [0] to "DisplayOLED1" > "TextField1" pin [In]
- Connect "MultiSource1" pin [1] to "Compare1" pin [In]
- Connect "MultiSource1" pin [2] to "MapRange1" pin [In]
- Connect "Compare1" pin [Out] to "PulseGenerator1" pin [Enabled]
- Connect "MapRange1" pin [Out] to "PulseGenerator1" pin [Frequency]
- Connect "PulseGenerator1" pin [Out] to Raspberry Pi Pico/RP2040 LED pin [Digital]
- Connect "DisplayOLED1" pin I2C [Out] to Raspberry Pi Pico/RP2040 pin I2C [In]
In Visuino, at the bottom click on the "Build" Tab, make sure the correct port is selected, then click on the "Compile/Build and Upload" button.
Step 7: PlayIf 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
Leave your feedback...