Radu Mihai Rotariu
Published

Raspberry Pi 4B Ultrasonic Distance Measurements & Display

Learn how to make a ultrasonic distance measurement project using the Raspberry Pi 4B.

IntermediateFull instructions provided20 hours3,708
Raspberry Pi 4B Ultrasonic Distance Measurements & Display

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED Bar Graph Array, Green
LED Bar Graph Array, Green
×1
High Brightness LED, White
High Brightness LED, White
×1
4 digit 7 segment LED display
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1
Resistor 100 ohm
Resistor 100 ohm
×8
Through Hole Resistor, 150 ohm
Through Hole Resistor, 150 ohm
×10
Resistor 330 ohm
Resistor 330 ohm
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1

Story

Read more

Schematics

Full Schematic

Raspberry Pi 4B Pinout Diagram

Code

script.py

Python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()

#pins HC-SR04
ECHO = 2
TRIG = 3

#pin ON/OFF LED
STATUS = 4

#pins LED bar
LED1 = 17
LED2 = 27
LED3 = 22
LED4 = 10
LED5 = 9
LED6 = 11
LED7 = 0
LED8 = 5
LED9 = 6
LED10 = 13

#pins LED display
DIS1 = 14	#bottom left
DIS2 = 15	#bottom centre
DIS3 = 18	#decimal point
DIS4 = 23	#bottom right
DIS5 = 24	#centre centre
DIS6 = 25	#digit 4
DIS7 = 8	#top right
DIS8 = 7	#digit 3
DIS9 = 1	#digit 2
DIS10 = 16	#top left
DIS11 = 20	#top centre
DIS12 = 21	#digit 1

#display segments
GPIO.setup(DIS3, GPIO.OUT)
GPIO.output(DIS3, 0)
segments = [DIS1, DIS2, DIS4, DIS5, DIS7, DIS11, DIS10]
for segment in segments:
	GPIO.setup(segment, GPIO.OUT)
	GPIO.output(segment, 0)

#display digits
digits = [DIS12, DIS9, DIS8, DIS6]
for digit in digits:
  GPIO.setup(digit, GPIO.OUT)
	GPIO.output(digit, 1)

print "Setting up Pins"

#setup pins ultrasound
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

#setup pin status LED
GPIO.setup(STATUS, GPIO.OUT)
GPIO.output(STATUS, True)
time.sleep(0.5)
GPIO.output(STATUS, False)

#setup pins LEDbar
leds = [LED10, LED9, LED8, LED7, LED6, LED5, LED4, LED3, LED2, LED1]
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
GPIO.setup(LED4, GPIO.OUT)
GPIO.setup(LED5, GPIO.OUT)
GPIO.setup(LED6, GPIO.OUT)
GPIO.setup(LED7, GPIO.OUT)
GPIO.setup(LED8, GPIO.OUT)
GPIO.setup(LED9, GPIO.OUT)
GPIO.setup(LED10, GPIO.OUT)
for led in leds:
	GPIO.output(led, True)
	time.sleep(0.5)
	GPIO.output(led, False)
	
#setup pins display
for digit in digits:
	GPIO.output(digit, 0)
	for segment in segments:
		GPIO.output(segment, 1)
		time.sleep(0.5)
		GPIO.output(segment, 0)
	GPIO.output(digit, 1)

num = {' ':(0, 0, 0, 0, 0, 0, 0),
	'0':(1, 1, 1, 0, 1, 1, 1),
	'1':(0, 0, 1, 0, 1, 0, 0),
	'2':(1, 1, 0, 1, 1, 1, 0),
	'3':(0, 1, 1, 1, 1, 1, 0),
	'4':(0, 0, 1, 1, 1, 0, 1),
	'5':(0, 1, 1, 1, 0, 1, 1),
	'6':(1, 1, 1, 1, 0, 1, 1),
	'7':(0, 0, 1, 0, 1, 1, 0),
	'8':(1, 1, 1, 1, 1, 1, 1),
	'9':(0, 1, 1, 1, 1, 1, 1)
}

#iterations  = number of measurements that we want
iterations = 10

GPIO.output(STATUS, 1)
while iterations > 0:
	distance = 0
	print "Distance measurement in progress"
	GPIO.output(TRIG, 0)
	print "Waiting for sensor to settle"
	time.sleep(1)

	GPIO.output(TRIG, 1)
	time.sleep(0.00001)
	GPIO.output(TRIG, 0)

	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 = pulse_duration * 17150
	distance = round(distance, 2)
	
	if distance >= 100:
		distance = 99.99;
	if distance < 5:
		distance = 5;

	print "Distance: ", distance, "cm"

	#lights
	if distance <= 40.00:
		GPIO.output(leds[0], 1)
		if distance <= 36.66:
			GPIO.output(leds[1], 1)
			if distance <= 33.33:
				GPIO.output(leds[2], 1)
				if distance <= 30.00:
					GPIO.output(leds[3], 1)
					if distance <= 26.66:
						GPIO.output(leds[4], 1)
						if distance <= 23.33:
							GPIO.output(leds[5], 1)
							if distance <= 20.00:
								GPIO.output(leds[6], 1)
								if distance <= 16.66:
									GPIO.output(leds[7], 1)
									if distance <= 13.33:
										GPIO.output(leds[8], 1)
										if distance <= 10.00:
											GPIO.output(leds[9], 1)
	
	#distance = n1n2.m1m2
	tmp1 = int(distance)
	n1 = int(tmp1 / 10)
	n2 = int(tmp1 - n1 * 10)
	tmp2 = int(distance * 100 - n1 * 1000 - n2 * 100)
	m1 = int(tmp2 / 10)
	m2 = int(tmp2 - m1 * 10)
	
	#timeToDisplay is used to keep the 7-digit display on for a while, so we can easily read the value
	for timeToDisplay in range(0, 3000):								
		timeToDisplay = timeToDisplay + 1
		for digit in range(0, 4):
			if digit == 1:
				GPIO.output(DIS3, 1)
			for loop in range(0, 7):
				if digit == 0:
					GPIO.output(segments[loop], num[str(n1)][loop])
				if digit == 1:
					GPIO.output(segments[loop], num[str(n2)][loop])
				if digit == 2:
					GPIO.output(segments[loop], num[str(m1)][loop])
				if digit == 3:
					GPIO.output(segments[loop], num[str(m2)][loop])
			GPIO.output(digits[digit], 0)
			time.sleep(0.0001)
			GPIO.output(digits[digit], 1)
			if digit == 1:
				GPIO.output(DIS3, 0)
	
	for led in leds:
		GPIO.output(led, 0)

	iterations -= 1

GPIO.output(STATUS, 0)

GPIO.cleanup()
print "End of measurements"

Credits

Radu Mihai Rotariu

Radu Mihai Rotariu

2 projects • 0 followers
household engineer

Comments