This document discusses using an ultrasonic distance sensor with a Raspberry Pi to measure distance. It describes the necessary components, including a Raspberry Pi, breadboard, resistors, and ultrasonic sensor. It explains how to configure the circuit by connecting the sensor to specific Raspberry Pi pins. The document then provides Python code to set up GPIO pins for the trigger and echo signals, send ultrasonic pulses, measure the time until the echo is received, and calculate the distance based on the time elapsed and speed of sound.
1 of 8
Downloaded 19 times
More Related Content
Raspberry pi tutorial #3
1. 1
Raspberry Tutorial #3
- Measuring the distance -
Dong Ho Son
POSTECH
Pohang, Korea
donghoson@postech.ac.kr
2. 2
Measuring the distance - Overview
Lets 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
Lets measure the distance
Necessary Components
1 x Raspberry Pi
1 x Breadboard
2 x Resistor
1 x Ultrasonic sensor
Jumper cables
4. 4
Measuring the distance Circuit Configuration
VCC 5V pin
Trig the pin GPIO 18
Echo resistors
GND Ground pin
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