1. 1
Raspberry Tutorial #3
- Measuring the distance -
Dong Ho Son
POSTECH
Pohang, Korea
donghoson@postech.ac.kr
2. 2
Measuring the distance - Overview
Let’s use the sensor
• Ultrasonic distance sensor
− A sensor that can measure the distance to an object by using sound waves
Source: education.rec.ri.cmu.edu/content/electronics/boe/ultrasonic_sensor/1.html
3. 3
Measuring the distance - components
Let’s measure the distance
Necessary Components
• 1 x Raspberry Pi
• 1 x Breadboard
• 2 x Resistor
• 1 x Ultrasonic sensor
• Jumper cables
6. 6
Measuring the distance - Python
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
• Select GPIO #18 as output pin
• To make ultrasonic signal
GPIO.setup(GPIO_ECHO, GPIO.IN)
• Select GPIO #24 as input pin
• To receive the signal
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
• Send the ultrasonic signal
7. 7
Measuring the distance - Python
GPIO.input(GPIO_ECHO)
• High while sending signal
• Low when signal is returned
TimeElapsed = StopTime – StartTime
• Time difference between start and arrival
distance = (TimeElapsed * 34300) / 2
• Multiply with the sonic speed 34300 cm/s
• Divide by 2, because there and back