Proximity Sensor With Raspberry Pi Pico W and HC-SR04
Hello, with this post i’m going to make a proximity sensor with Raspberry Pi Pico W and HCSR-04. Before this post i made several HC-SR04 projects like height measurement. And I used Arduino with HC-SR04. But now we will use Raspberry Pi Pico W. You know Raspberry Pi Pico W is a new microcontroller created by Raspberry. It is so small, cheap and has great specifications(Wifi, temp sensor etc.) . Everybody can easily code Raspberry Pi Pico W with MicroPython. And I decided make a parking sensro with Raspberry Pi Pico W.
In the beginning we need to collect some components. I used these components in this project:
1- Raspberry Pi Pico W
2- HC-SR04
3-Color LEDS
4-Buzzer(optional)
5-1k Ohm Resistor
6-Breadboard
7-Jumper Cable.
First we need to connect HC-SR04 to Raspberry Pi Pico W. I connected Raspberry Pi Pico W GPI2 pin to HC-SR04 echo and Raspberry Pi Pico W GPI1 pin to HC-SR04 trigger. And Raspberry Pi Pico W VSYS to HC-SR04 VCC and Raspberry Pi Pico W GND to HC-SR04 gnd. Now we done connection between Raspberry Pi Pico W and HC-SR04. You can see connection as seen diagram below.

After that i connected LEDs and buzzer. Buzzer to Raspberry Pi Pico W GPI15 pin, red LED to Raspberry Pi Pico W GPI16, yellow LED to Raspberry Pi Pico W GPI17 and finally yellow LED to Raspberry Pi Pico W GPI18. Also I connect LEDs’ katot to GND with 1k ohm resistor.
All the connections done. Now we will write code. First u have to install Thonny Python Editor to coding Raspberry Pi Pico W. For using Raspberry Pi Pico W you have to install MicroPython firmware to Raspberry Pi Pico W. For that press BOOTSEL button without connect Raspberry Pi Pico W to computer. After clicking BOOTSEL button connect Raspberry Pi Pico W via microusb cable to computer. A window with USB flash disk will appear on your computer secreen. At this point now open Thonny Editor and install micropython with Raspberry Pi Pico W libraries. I recommend don’t install unstable versions of MicroPython. After installation Raspberry Pi Pico W will automatically open in coding mode. Now you upload your code Raspberry Pi Pico W with Thonny Editor.
In this project i used special library which is easy to use with HC-SR04. So click this link and open MicroPython-HCSR04 github and download library. After that upload hcsr04.py to Raspberry Pi Pico W in lib folder. Now you are ready. You can copy and paste code below to Thonny Editor and click run.

from machine import Pin
from hcsr04 import HCSR04
from time import sleep
sensor = HCSR04(trigger_pin=1, echo_pin=2, echo_timeout_us=10000)
buzzer=machine.Pin(15,machine.Pin.OUT)
red=machine.Pin(16,machine.Pin.OUT)
yellow=machine.Pin(17,machine.Pin.OUT)
blue=machine.Pin(18,machine.Pin.OUT)
while True:
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
if distance < 50 :
buzzer.value(1)
red.value(1)
yellow.value(0)
blue.value(0)
elif 50< distance < 100 :
buzzer.value(0)
red.value(0)
yellow.value(1)
blue.value(0)
elif 100 < distance < 200 :
buzzer.value(0)
red.value(0)
yellow.value(0)
blue.value(1)
else :
buzzer.value(0)
red.value(0)
yellow.value(0)
blue.value(0)
sleep(0.2)
If you have any question you can write it comments below. Hopefully you enjoy this project.

2.satırda hata veriyor hcsr04 module yok gibi
dediğiniz siteden indirdim hcsr04 kütüphanesini ama lib klasöründe rasberry pico w diye bir klasör bulamadım içine atmak için
Thonny Editor içerisinden HCSR04 kütüphanesini yükleyebilirsin.