Materials
- Raspberry Pi Model B rev. 2
- HC-SR04 distance sensor
- 2 x resistances, R1<R2<2*R1
- Prototype board
- Some cables
The Sensor
HC-SR04 Ultrasonic Sensor |
The HC-SR04 is a very simple ultrasonic sensor. Though it is not very delicate, it is still advised to manipulate with caution.
Basically, this sensor emits a sound pulse, that echoes on the measured object's surface and is reflected back to the sensor. The time that the pulse took from being emitted to received is the base used to calculate the distance.
Bear in mind that the surface should be parallel to the sensor, if not the reflection and the distance measurement will be affected.
Pinout:
The formula for distance is:
For more information please refer to:
Please visit and thanks to:
Correct way to operate |
Incorrect way to operate |
- Vcc: 5V power supply
- Trig: Trigger pin (input)
- Echo: Receive pin (output)
- Gnd: Power ground
The formula for distance is:
$d=\frac{v_{sound}\times t_{pulse}}{2}$
- d is the distance
- v is the speed of sound, 340 m/s
- t is the time it took the pulse to get back to the sensor
Note: The reason that it is divided by 2 is because the pulse has to travel twice the distance to get back to the source.
The Wiring
As the Raspberry Pi GPIO can only handle 3.3V input voltage, we will need to use a voltage divider. This will divide the 5V from the sensor and will keep the GPIO safe. It is important to note that the resistances values should always be R1 < R2 < 2R1.
The wiring with voltage divider |
The Software
Copy or download this code and run it in terminal, remember to use root privileges. It should look something like the image below. You can always halt the execution by pressing Ctrl+C at any time.
Copy or download this code and run it in terminal, remember to use root privileges. It should look something like the image below. You can always halt the execution by pressing Ctrl+C at any time.
# Derick DeLeon
# 2014-01
# HC-SR04_example.py
# This is an atempt to make the HC-SR04 ultrasonic distance sensor work
import RPi.GPIO as GPIO
import time
def prepare(GPIO_ECHO, GPIO_TRIGGER):
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# Allow module to settle
time.sleep(0.5)
# get the distance from the sensor, echo - the input from sensor
def get_distance(GPIO_ECHO, GPIO_TRIGGER):
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300
# That was the distance there and back so halve the value
distance = distance / 2
return distance
# MAIN
GPIO.setwarnings(False)
# Set to Board pin number mapping
GPIO.setmode(GPIO.BCM)
# Set up the GPIO channels
trigger = 17
echo = 27
prepare(echo, trigger)
try:
while True:
d = get_distance(echo, trigger)
print d
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()
Please visit and thanks to:
Regarding all aspects the blog was perfectly nice.
ReplyDeleteI am glad you like it. :D
Delete