Ultrasonic sensor to measure object presence and proximity alert, with camera capture

Shalav Kakati
4 min readMar 1, 2021

My next activity was to build on the web camera I had tested in a earlier activity, and mate an ultrasonic sensor HC-SR04 with it to run some uses

But first a bit of physics. The sensor has 4 pins — 2 are the usual voltage and ground. 1 is for transmit and 1 for receive of signal when the ultrasonic transmitter is to be triggered on/off and when the receiver is getting some reflected sound. distance = time x speed. The speed of sound is roughly 34300cm/sec. For time, we send a narrow pulse of sound, and measure the leading (high) and trailing edge(low) of the reflected sound in the receiver to get the time used for the pulse of sound we transmitted…one last point — since the sound goes out and gets reflected back, the distance got from time x speed above is 2X the distance from transmitter to reflector, so finally we have a formula distance = (time x 34300 ) / 2 cm

The sensor is good out to about 100cm, so we can confine our test to something within that distance.

  • object presence alert like a cup held under a water dispenser, intruder alert, convenience trigger like opening a lock by using object proximity. For this a preset fixed distance is used as the benchmark and anything more than 2cm off is considered a trigger
  • collision sensor if the distance gets too low — in our test 10cm. industrial machinery, vehicles…

In both cases, the camera will capture a time stamped image

The circuit and need to convert the 5V from Pi into 3.3V used the sensor using resistors and ground is covered here

First a few pictures of how the test was setup

Next i created two python programs — one for proximity alert and one for object detection. Due to the low specs of the web camera, was a need to skip 50 frames to let it stabilize its exposure to get a decently lit pic, as seen in one example below not doing it makes the pic dark.

root@raspberrypi:/home/pi# cat sonic_camera.py#proximity alert
import RPi.GPIO as GPIO
import time
import os

MIN_DISTANCE = 10 #below 10cm proximity

TRIG=21
ECHO=20
GPIO.setmode(GPIO.BCM)
while True:
print("distance measurement in progress")
GPIO.setup(TRIG,GPIO.OUT) #tramsmitter
GPIO.setup(ECHO,GPIO.IN) #receiver
GPIO.output(TRIG,False) #turn transmitter off
#print("waiting for sensor to settle")
time.sleep(0.2)
GPIO.output(TRIG,True) #turn transmitter on
time.sleep(0.00001) #sending a short pulse
GPIO.output(TRIG,False) #turn transmitter off...sound is reflecting and coming back to receiver
while GPIO.input(ECHO)==0: #while no sound received, sit in loop
pulse_start=time.time()
#sound leading edge is received now so our last time.time() is start time
while GPIO.input(ECHO)==1: #while sound is received sit in loop
pulse_end=time.time()
#sound has ended ie trailing edge of reflected sound so proceed pulse_duration=pulse_end-pulse_start
distance=pulse_duration*17150
distance=round(distance,2)
print("distance:",distance,"cm")
if distance < MIN_DISTANCE:
os.system('fswebcam -S 20 --save /home/pi/fswebcam/shalav_%H%M%S.jpg')
print("captured a proximity image")
time.sleep(1)
root@raspberrypi:/home/pi# cat sonic_camera2.py#object presence detectorimport RPi.GPIO as GPIO
import time
import os

FIXED_DISTANCE = 24 #fixed when product built, I checked it for the wall and set it here

TRIG=21
ECHO=20
GPIO.setmode(GPIO.BCM)
while True:
print("distance measurement in progress")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG,False)
#print("waiting for sensor to settle")
time.sleep(0.2)
GPIO.output(TRIG,True)
time.sleep(0.00001)
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=pulse_duration*17150
distance=round(distance,2)
print("distance:",distance,"cm")
if distance < (FIXED_DISTANCE-2):
os.system('fswebcam -S 20 --save /home/pi/fswebcam/shalav_%H%M%S.jpg')
print("captured a object presence")
time.sleep(1)
shows a object presence detection, a proximity alert case and a underexposure case where I was not skipping frames in the webcam

Thats it for this experiment. Next I will practice my beginner soldering skills and attach pin-headers to a Pi-zero (bring that up) and a Arduino. once thats done will figure out how to connect the arduino to my Pi4 and how to control it for useful work and what are its pros and cons.

--

--

Shalav Kakati

Grade 11 IBDP student, interested in the intersections of mechanical, electrical and biomedical engineering.