Learn

Measuring Distance with a JSN-SR04T Waterproof Ultrasonic Sensor on a Raspberry Pi 4B

Wire a JSN-SR04T waterproof ultrasonic sensor (control board plus separate probe) to a Raspberry Pi 4B, read back a real trigger/echo distance in Python, and protect the Pi's GPIO with the 5V→3.3V voltage divider the Echo pin requires.

Note: the JSN-SR04T Waterproof Ultrasonic Sensor is a VIP-tier component in the simulator — you’ll need a VIP plan to use it there.

Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.

What you’ll need

  • Raspberry Pi 4B
  • JSN-SR04T Control Board
  • JSN-SR04T Waterproof Probe
  • Breadboard (optional)
  • 2 Resistors (1.8kΩ and 3.3kΩ, for the Echo divider)

Step by step

  1. Drag Raspberry Pi 4B onto the Canvas.
  2. Drag the JSN-SR04T Control Board and the JSN-SR04T Waterproof Probe onto the Canvas. The real sensor is two parts: a small control board and a waterproof probe head on a cable. The board can plug into a breadboard; the probe is simply placed on the Canvas.
  3. Connect the cable: wire the board's Probe cable port (top of the board) to the probe's Cable pin. Without this cable the sensor never returns an echo.
  4. Hover over the board's 4 header pins to confirm the labels, left to right: 5V, TRIG, ECHO, GND.
  5. Wire 5V → a Pi 5V pin and GND → a Pi GND pin.
  6. Wire TRIG → a Pi GPIO pin you'll configure as an output (e.g. physical pin 37).
  7. Wire ECHO → a Pi GPIO pin you'll configure as an input (e.g. physical pin 38) — for now wire it directly, with no divider yet, so you can see what the simulator flags.
  8. Click the probe to select it, open its settings panel, and set a Distance (cm). The real sensor measures roughly 25–450 cm; anything beyond 450 returns no echo.
  9. Go to the Code tab and use the standard trigger/echo pattern:
import RPi.GPIO as GPIO
import time
import asyncio
 
GPIO.setmode(GPIO.BOARD)
TRIG = 37 # adjust to match your wiring
ECHO = 38 # adjust to match your wiring
 
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.output(TRIG, False)
 
while True:
GPIO.output(TRIG, True)
time.sleep(0.00001) # a real ~10 microsecond trigger pulse
GPIO.output(TRIG, False)
 
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
 
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
 
pulse_duration = pulse_end - pulse_start
distance = round(pulse_duration * 17150, 2)
 
print(f"Distance: {distance} cm")
await asyncio.sleep(1)
  1. Click Start.

Check the Console and the control board. You'll see a warning that ECHO is reading about 5V on an input pin, and the board shows a red "ECHO 5V!" status. This is intentional: the JSN-SR04T's Echo pin outputs a 5V signal, but a Raspberry Pi's GPIO pins only tolerate 3.3V. Wiring Echo straight to the Pi is the mistake that can damage real hardware. Nothing can be damaged here, but the simulator flags it the same way a multimeter would.

  1. Fix it the real way with a voltage divider: ECHO → R1 (1.8kΩ) → the node wired to the Pi input pin → R2 (3.3kΩ) → a Pi GND pin. This brings the 5V signal down to about 3.24V, which is safe.
  2. Click Start again.

What “working correctly” looks like

  • The Console prints a distance about once per second that tracks the Distance you set on the probe. Some run-to-run jitter is normal because timing runs on the browser's JavaScript timers.
  • Wired directly (no divider), the warning appears once per run and the board shows a red "ECHO 5V!"; the reading itself still works.
  • With the 1.8kΩ / 3.3kΩ divider, the warning and the red flag are gone and readings are unchanged.

If something’s wrong

  • The script hangs or never prints a distance → check the probe cable is wired from the board's Probe cable port to the probe, the board is powered (5V and GND wired), and TRIG/ECHO aren't swapped.
  • Board status reads NO PROBE or NO PWR → connect the cable, or wire 5V and GND to real Pi 5V/GND pins.
  • No echo at all → the probe's Distance may be set above 450 cm, which is out of range.
  • An occasional wildly off reading → expected now and then: browsers can briefly delay a timer (especially in a background tab), and that delay shows up in the math. It isn't a wiring fault.