Ultrasonic Sensor HC-SR04 With Warning LED Using Raspberry Pi Pico

by RonFrtek in Circuits > Arduino

61 Views, 1 Favorites, 0 Comments

Ultrasonic Sensor HC-SR04 With Warning LED Using Raspberry Pi Pico

Ultrasonic Sensor HC-SR04 With Warning LED Using Raspberry Pi Pico

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!

What You Will Need

Raspberry Pi RP2040 Pico W WiFi.png
ultrasonic-sensor-hc-sr04-500x500.jpg
oled.jpg
VisuinoAdvrtisemenESPNowESP32C3-1.jpg
FMT2X9VKEY93G5P.jpg
FMMM5N5KEY93G5M.jpg
  • Raspberry Pi Pico RP2040
  • HC-SR04 Ultrasonic sensor
  • OLED I2C Display
  • Breadboard
  • Jumper wires
  • Visuino program: Download Visuino


The Circuit

2024-07-20_9-31-05.jpg
pico_pinout.png
  • 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, and Select the Raspberry Pi Pico (RP2040) Board Type

select-board-uno.jpg
FROSP2ELNEMAEN9.png

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

In Visuino Add & Set Components

F1YINGPLSRIPD6E.jpg
2024-07-20_10-41-28.jpg
FLMGXO0LSRIPD7L.jpg
2024-07-20_10-44-52.jpg
2024-07-20_10-42-06.jpg
2024-07-20_10-50-24.jpg
2024-07-20_10-52-39.jpg
2024-07-20_10-53-33.jpg
2024-07-20_10-53-52.jpg
2024-07-20_10-56-27.jpg
2024-07-20_10-57-33.jpg
2024-07-20_10-58-37.jpg
2024-07-20_10-58-59.jpg
  • 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




In Visuino Connect Components

2024-07-20_11-01-46.jpg
2024-07-20_11-02-07.jpg
rpi-ultrasonic.png
  • 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]


Generate, Compile, and Upload the Project

FZ251URLP5HNCHG.png

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.

Play

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