Skip to content
3. HC-SR04 Ultrasonic Distance Sensor

3. HC-SR04 Ultrasonic Distance Sensor

Soan Papdi and HC-SR04 side by side

This project reads distance from an HC-SR04 ultrasonic sensor using the Soan Papdi board and lights up the 8 onboard LEDs like a distance meter. As an object gets closer to the sensor, more LEDs turn on one by one.


What You Need

  • Soan Papdi board (iCE40 UP5K)
  • HC-SR04 ultrasonic sensor
  • 1 kΩ resistor
  • 2 kΩ resistor

Hardware Connections

Protect your FPGA pin! The HC-SR04 runs on 5 V, so its Echo pin outputs 5 V signals. The iCE40UP5K inputs only accept 3.3 V maximum. You must use a voltage divider.
HC-SR04 Echo (5V) ──[ 1 kΩ ]──┬── FPGA pin (3.3 V)
                              │
                           [ 2 kΩ ]
                              │
                             GND

Pin Mapping

Ultrasonic sensor pinout

SignalFPGA PinDescription
clk35Onboard 12 MHz clock
trig27HC-SR04 Trig pin
echo28HC-SR04 Echo pin (through voltage divider)
leds[7:0]D0–D7Onboard LEDs
VCC3V3HC-SR04 VCC
GNDGNDCommon ground

Need to check another pin? See the full Pin Diagram.

Clone the Example Project

git clone https://github.com/hardik1975/soan-papdi-examples.git
cd soan-papdi-examples/03-ultrasonic

Project Files Explained

FileWhat it does
top.vTakes the measured distance and turns the 8 LEDs on/off like a bar-graph distance meter
ultrasonic_sensor.vSends the 10 µs trigger pulse and measures how long the Echo pin stays high
pin.pcfMaps signal names to the real pins on the Soan Papdi board

Understanding the Hardware Logic

The design has two modules working together.


1. ultrasonic_sensor.v – The Sensor Driver

This module talks to the HC-SR04 and calculates distance.

How the HC-SR04 works

  1. FPGA sends a short 10 µs pulse on the trig pin.
  2. The sensor sends out an ultrasonic burst.
  3. When the echo returns, the sensor pulls the echo pin high.
  4. The time the echo pin stays high is proportional to the distance.

What the module does

  • Every 60 ms it generates a clean 10 µs trigger pulse.
  • It counts how many clock cycles the echo pin stays high.
  • It converts that count into a distance value (using the speed of sound).
  • It also has a simple debounce so the reading doesn’t flicker.

Key parameters you can change:

CLK_FREQ      = 12_000_000   // 12 MHz clock
TRIG_PULSE_US = 10           // 10 µs trigger
MAX_DIST_CM   = 40           // maximum distance we care about

2. top.v – Distance Meter Logic

This module takes the raw count from the sensor and lights the LEDs.

Distance zones (0 – 40 cm)

Distance RangeLEDs that light up
0 – 5 cmAll 8 LEDs
5 – 10 cm7 LEDs
10 – 15 cm6 LEDs
15 – 20 cm5 LEDs
20 – 25 cm4 LEDs
25 – 30 cm3 LEDs
30 – 35 cm2 LEDs
35 – 40 cm1 LED
> 40 cm or no objectAll LEDs off

It also has a timeout: if no valid echo is received for 100 ms, all LEDs turn off.

Build & Flash

  1. Connect the Soan Papdi board to your computer via USB.
  2. Put the board into Programming Mode.
  3. Build the design:
apio build
  1. Upload the bitstream:
apio upload
  1. Press the Reset button on the Soan Papdi.

Test It

LEDs lighting up as an object approaches

  • Place your hand or an object in front of the sensor.
  • As you move closer, more LEDs will light up one by one.
  • When the object is very close (under ~5 cm), all 8 LEDs should be on.
  • When nothing is in range, all LEDs turn off.

Bonus: Visualize Your Logic

apio graph --pdf

This creates a diagram of the logic in the _build folder so you can see the counters and state machines.

Ultrasonic Logic Graph