Simple Social Distance Detector
Find out how far away something is using SOUND?!?! Recently, I've been playing around a lot with the Raspberry Pi Pico, and I thought it'd be cool to create a full project from start to finish and show each step. The project? Well, the current pandemic begs for a Social Distance Bot that alerts you when someone is within 2 meters (6 feet). How does it alert you? In the most annoying way possible…a piezo buzzer. I've tried to document everything so you can make one of these yourself.
Chapter 1: What you will need
- Pi Pico
- SSD1306 displayk
- Piezo buzzer
- HC-SR04 Ultrasonic Sensor
- Jumper Wires
- Soldering Equipment
- F90 Degree MicroUSB Adapter
Chapter 2: How Ultrasonic Sensors Work
How Ultrasonic Sensors Work
An Ultrasonic Sensor can be controlled to emit a high pitched sound that is undetectable by humans. If an object is in front of the sensor, the sound waves will bounce off of it and return to the sensor. If a return sound is detected, the Ultransonic Sensor knows that something is in front of it.
But how do we measure distance from using sound waves? We start by using this formula that can derive distance from time and speed.
DISTANCE = SPEED x TIME
The SPEED in this equation is the speed of sound, which is 343 meters per second. Since the Ultrasonic Sensor primarily deals in microseconds and centimeters, converting the speed of time makes it 0.0343 centimeters per microsecond.
The TIME is going to be the time it takes for the sensor to emit a sound and receive a response. This will be determined programmatically.
One last thing to note is that we are basing the distance off of the echo received from the emitted sound. This means the distance of the object would be half of the time it takes for the echo to be received. So we'll have to divide the whole equation by two.
DISTANCE = (0.0343 x echo_time) / 2
Chapter 3: Wiring
First we need to wire up all the components to the Pi Pico. You can download the Fritzing diagram, or just use the diagram image to the left. Here's how it will look:
VCC > Pin 40
GND > GND
Trig > GP3
Echo > GP2
VCC > Pin 36
GND > GND
SDA > GP0
SCL > GP1
GND > GND
Power > GP13
Chapter 4: Code
I'm using the Thonny python editor.
# IMPORT NECESSARY DEPENDENCIES # THE SSD1306 DEPENDENCY IS AN EXTERNAL DEPENDENCY from machine import Pin, I2C, Timer, PWM from ssd1306 import SSD1306_I2C import utime # INITIALIZE DISPLAY i2c = I2C(0,sda=Pin(0),scl=Pin(1),freq=40000) oled = SSD1306_I2C(128,64,i2c) # INITIALIZE HC-SR04 SENSOR trigger = Pin(3, Pin.OUT) echo = Pin(2, Pin.IN) # INITITALIZE BUZZER # buzzer = PWM(Pin(13)) # set audio frequency buzzer.freq(500) # set audio volume from 0 to 1000 buzzer.duty_u16(1000) # INITIALIZE VARIABLES # target_distance = 2 #meters # CREATE A FUNCTION TO CHECK FOR DISTANCE # def sensor(): # turn off trigger, and wait for 2 microseconds trigger.low() utime.sleep_us(2) # turn on trigger, and wait for 5 microseconds trigger.high() utime.sleep_us(5) # turn off trigger trigger.low() # count time it takes to receive an echo while echo.value() == 0: signaloff = utime.ticks_us() while echo.value() == 1: signalon = utime.ticks_us() timepassed = signalon - signaloff #convert the time to measurements #measurement in cm #distance = (timepassed * 0.0343) / 2 #measurement in m distance = (timepassed * 0.000343) / 2 #measurement in feet #distance = (timepassed * 1125) / 2 return distance # ADD MAIN LOOP TO 'TRY' STATEMENT IN CASE OF ERRORS # try: # CREATE MAIN LOOP # while True: # WIPE DISPLAY SCREEN # oled.fill(0) # GET RESULT FROM SENSOR # result = sensor() if result < target_distance: buzzer.duty_u16(1000) oled.text("TOO CLOSE!",0,20) else: buzzer.duty_u16(0) oled.text("Distance:",0,0) oled.text(str(result) + " m",0,10) oled.text("",0,20) oled.show() utime.sleep(1) except KeyboardInterrupt: pass
This post currently has no responses.