UltraSonic Sensor - Python with Raspberry Pi

UltraSonic Sensor
VCC is the pin that powers the device, connect to 5V.
Trigger is the pin that sends out the burst.
Echo is the pin that outputs when the reflected sound is received.
It outputs at 5V. This needs to be lowered to 3.3V.
GND is the ground pin, connected to complete the circuit.
The code in this section is from the RaspberryPi website and
Simon Monk's RaspiRobot code.
UltraSonic Sensor
import RPi.GPIO as GPIO
from time import sleep,time
GPIO.setmode(GPIO.BCM)
TRIGGER_PIN = 26
ECHO_PIN = 4
GPIO.setup(TRIGGER_PIN, GPIO.OUT)
GPIO.setup(ECHO_PIN, GPIO.IN)
sleep(2) #let sensor settle.
Send the Trigger Pulse
#Create a function to send a pulse.
def send_trigger_pulse():
GPIO.output(TRIGGER_PIN, True)
sleep(0.0001)
GPIO.output(TRIGGER_PIN, False)
Wait for the echo
#As soon as the ultrasonic sensor has sent out a burst of sound,
the echo pin is set to high. You can use a while loop to detect
when this happens and then record the current time.
def wait_for_echo(value, timeout):
count = timeout
while GPIO.input(ECHO_PIN) != value and count > 0:
count -= 1
Get the distance to the object
def get_distance():
send_trigger_pulse()
wait_for_echo(True, 10000)
start = time()
wait_for_echo(False, 10000)
finish = time()
pulse_len = finish - start
print(pulse_len)
distance_cm = pulse_len *343 *100 / 2
# speed of sound; magnitude only; velocity is speed and
# direction divide by 2, since the sound waves must
travel to
# the object and back
return distance_cm
Main control for the UltraSonic
Sensor
def main():
try:
while True:
print("Starting the main control block")
distance = get_distance()
print("Distance " + str(distance))
sleep(2)
except KeyboardInterrupt:
print("Completing...")
sleep(1)
finally:
print("Clean up")
GPIO.cleanup()
main()
Circuit for the UltraSonic Sensor
Method to determine the values for the resistors:
R1 = 1200 #The smaller of the two resistors; this could be adjusted
VOUT = 3.3 #The voltage you are trying to achieve; input should
be no more that 3.3V
VIN = 5 #The input voltage
R2 = (Vout*R1)/(Vin-Vout)
The RaspberryPi website recommend 1.2k and 2.2k resistors.
Circuit for the UltraSonic Sensor
UltraSonic Sensor Circuit
Ultrasonic Sensor Pins
GND Echo Trig VCC
j27 j28 j29 j30
Resistor 1.2KOhm (Brown-Red-Red)
b30 to b25
Resistor 2.2KOhm (Red-Red-Red)
a25 to a20
Copyright © 2017 by Dr. E.I. Horvath
UltraSonic Sensor Circuit
Jumper wires
f30 to 5V (pin 2) Connected to VCC
f29 to GPIO 26 (pin 37) Connected to Trigger
c25 to GPIO 4 (pin 7)
f28 to c30 (male to male)
f27 to ground bar (male to male)
c20 to ground bar (male to male)
ground bar to GND (pin 39)
Copyright © 2017 by Dr. E.I. Horvath
Playing Sounds
Enable your audio device
Menu -> Preferences -> Audio Device Settings
mixer is the pygame module for controlling streamed audio
pygame.mixer.music.load — Load a music file for playback
pygame.mixer.music.play — Start the playback of the music
stream
pygame.mixer.music.rewind — restart music
pygame.mixer.music.stop — stop the music playback
pygame.mixer.music.pause — temporarily stop music playback
pygame.mixer.music.unpause — resume paused music
Playing Sounds
pygame.mixer.music.set_volume — set the music volume
pygame.mixer.music.get_volume — get the music volume
pygame.mixer.music.get_busy — check if the music stream is playing
pygame.mixer.music.set_pos — set position to play from
pygame.mixer.music.get_pos — get the music play time
pygame.mixer.music.queue — queue a music file to follow the current
pygame.mixer.music.set_endevent — have the music send an event
when playback stops
pygame.mixer.music.get_endevent — get the event a channel sends
when playback stops
Playing Sounds
from pygame import mixer
from time import sleep
pygame.mixer.init() # intialize the mixer module
mixer.music.load('/usr/share/scratch/Media/Sounds/Human/Footsteps
-1.wav') # specify the path to the audio file
mixer.music.play() # play the sound
while mixer.music.get_busy():
sleep(0.01) # wait until the sound finishes
Copyright © 2017 by Dr. E.I. Horvath