Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 6 of 47 · Sensors · Beginner

Distance meter with an LED bar

An ultrasonic sensor measures distance. The closer your hand, the more LEDs light up, like a car parking sensor.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Distance meter with an LED bar: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
HC-SR04 sensorUltrasonic sensor
LED barLED bar

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1RedHC-SR04 sensor - VCCPin 2 (5V)
2ColouredHC-SR04 sensor - TRIGPin 16 (GPIO23)
3ColouredHC-SR04 sensor - ECHOPin 18 (GPIO24)
4BlackHC-SR04 sensor - GNDPin 6 (GND)
5ColouredLED bar - SignalPin 29 (GPIO5)
6ColouredLED bar - SignalPin 31 (GPIO6)
7ColouredLED bar - SignalPin 33 (GPIO13)
8ColouredLED bar - SignalPin 35 (GPIO19)
9ColouredLED bar - SignalPin 37 (GPIO26)
10BlackLED bar - GNDPin 9 (GND)

Python code (gpiozero)

from gpiozero import DistanceSensor, LEDBarGraph
from time import sleep

sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)
bar = LEDBarGraph(5, 6, 13, 19, 26)

while True:
    cm = sensor.distance * 100            # metres to centimetres
    bar.value = 1 - sensor.value          # closer object = more LEDs
    print("Distance:", round(cm), "cm")
    sleep(0.5)

How the code works

You will learn

  • Reading an analog-style value from a sensor
  • Mapping a range to a bar graph
  • Units: metres and centimetres

Try this

  • Drag the slider and watch the LEDs.
  • Print "STOP!" when the distance is under 10 cm.
  • Make the buzzer beep faster as the object gets closer.

On a real Pi

  • The HC-SR04 echo pin gives 5 V. Use a voltage divider (1 k and 2 k ohm) before the Pi pin, or a 3.3 V sensor.
  • Soft or angled surfaces reflect sound badly. Expect readings to jump a little.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4330 ohm resistor, 1/4 W
One per LED
5Robu.in Amazon.in ThinkRobotics
5LED bar graph module (5 LEDs)1Robu.in Amazon.in ThinkRobotics
6HC-SR04 ultrasonic sensor1Robu.in Amazon.in ThinkRobotics
71 k and 2 k resistors (ECHO voltage divider)2Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 5. Motion alarm with a PIR sensorAll projects7. Dimmer and breathing LED (PWM) →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.