HC-SR04 ultrasonic distance sensor Jarak
Table of Contents
Persiapan Bahan #
– Raspberry Pi Pico / Pico W
– HC SR04 Distance Sensor / Sensor Jarak
– Breadboard
– 4x Kabel Jumper Male to Female
– Kabel Micro USD
– Aplikasi Thonny IDE
Wiring HC SR04 Ke Raspberry Pi Pico #
– place the HC-SR04 sensor onto the breadbord as shown in the picture below
– connect the VCC pin of the sensor with a 5V pin (red wire)
– connect the Trig pin of the sensor to GP17 (green wire)
– connect the Echo pin of the sensor to the 1kOhm resistor (yellow wire)
– connect the other end of the 1kOhm resistor to GP16
– connect the GND of the sensor to the “-” row of the breadboard (black wire)
– connect a GND pin of the Pico to the “-” row of the breadboard (black wire)
– insert the 2kOhm resistor between GP16 and the “-” row
Sample Code #
from machine import Pin
import time
trig = Pin(17, Pin.OUT)
echo = Pin(16, Pin.IN, Pin.PULL_DOWN)
while True:
trig.value(0)
time.sleep(0.1)
trig.value(1)
time.sleep_us(2)
trig.value(0)
while echo.value()==0:
pulse_start = time.ticks_us()
while echo.value()==1:
pulse_end = time.ticks_us()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17165 / 1000000
distance = round(distance, 0)
print ('Distance:',"{:.0f}".format(distance),'cm')
time.sleep(1)