Ultra Sonic Sensor VCC is the pin that

  • Slides: 13
Download presentation
Ultra. Sonic Sensor VCC is the pin that powers the device, connect to 5

Ultra. Sonic Sensor VCC is the pin that powers the device, connect to 5 V. Trigger is the pin that sends out the burst. Echo is the pin that outputs when the reflected sound is received. It outputs at 5 V. This needs to be lowered to 3. 3 V. GND is the ground pin, connected to complete the circuit. The code in this section is from the Raspberry. Pi website and Simon Monk's Raspi. Robot code.

Ultra. Sonic Sensor import RPi. GPIO as GPIO from time import sleep, time GPIO.

Ultra. Sonic 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.

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

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()

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 Ultra. Sonic Sensor def main(): try: while True: print("Starting the

Main control for the Ultra. Sonic Sensor def main(): try: while True: print("Starting the main control block") distance = get_distance() print("Distance " + str(distance)) sleep(2) except Keyboard. Interrupt: print("Completing. . . ") sleep(1) finally: print("Clean up") GPIO. cleanup() main()

Circuit for the Ultra. Sonic Sensor Method to determine the values for the resistors:

Circuit for the Ultra. Sonic Sensor Method to determine the values for the resistors: R 1 = 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. 3 V VIN = 5 #The input voltage R 2 = (Vout*R 1)/(Vin-Vout) The Raspberry. Pi website recommend 1. 2 k and 2. 2 k resistors.

Circuit for the Ultra. Sonic Sensor

Circuit for the Ultra. Sonic Sensor

Ultra. Sonic Sensor Circuit Ultrasonic Sensor Pins GND Echo Trig VCC j 27 j

Ultra. Sonic Sensor Circuit Ultrasonic Sensor Pins GND Echo Trig VCC j 27 j 28 j 29 j 30 Resistor 1. 2 KOhm (Brown-Red) b 30 to b 25 Resistor 2. 2 KOhm (Red-Red) a 25 to a 20 Copyright © 2017 by Dr. E. I. Horvath

Ultra. Sonic Sensor Circuit Jumper wires f 30 to 5 V (pin 2) Connected

Ultra. Sonic Sensor Circuit Jumper wires f 30 to 5 V (pin 2) Connected to VCC f 29 to GPIO 26 (pin 37) Connected to Trigger c 25 to GPIO 4 (pin 7) f 28 to c 30 (male to male) f 27 to ground bar (male to male) c 20 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

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.

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() #

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