embeded/raspberry pi2015. 7. 13. 23:14

3.3-5V 이나 5V추천

탐지거리 3cm-4m

[링크 : http://www.seeedstudio.com/depot/Grove-Ultrasonic-Ranger-p-960.html]


다른 소스들의 예제와 다르게 이녀석...

Trigger / Echo가 아닌 Signal 하나 뿐이다


그래서 Trigger로 output 모드 설정후 전송하고 재빠르게

Echo로 input 모드로 설정후 받아 시간을 재도록 수정해야 한다.


포트는 만만한(?) GPIO18번 PWM 포트(wiring pi 기준 1번)으로 설정

흰색 - 5V

검정 - GND

보라 - GPIO18 ->A0




$ cat ultrasonic_1.py

#!/usr/bin/python

#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|

#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

#

# ultrasonic_1.py

# Measure distance using an ultrasonic module

#

# Author : Matt Hawkins

# Date   : 09/01/2013

# -----------------------


# Import required Python libraries

import time

import RPi.GPIO as GPIO


# Use BCM GPIO references

# instead of physical pin numbers

GPIO.setmode(GPIO.BCM)


# Define GPIO to use on Pi

GPIO_TRIGGER = 18

GPIO_ECHO    = 18


print "Ultrasonic Measurement"


# Set pins as output and input

GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger


# Set trigger to False (Low)

GPIO.output(GPIO_TRIGGER, False)


# Allow module to settle

time.sleep(0.5)


# Send 10us pulse to trigger

GPIO.output(GPIO_TRIGGER, True)

time.sleep(0.00001)

GPIO.output(GPIO_TRIGGER, False)

start = time.time()


GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

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


print "Distance : %.1f" % distance


# Reset GPIO settings

GPIO.cleanup() 


자가 없어서 대충 한뼘(21cm)로 해봤는데 얼추 맞는 기분

pi@raspberrypi ~/src/sonic $ sudo python ultrasonic_1.py

Ultrasonic Measurement

Distance : 154.8

pi@raspberrypi ~/src/sonic $ sudo python ultrasonic_1.py

Ultrasonic Measurement

Distance : 3.0

pi@raspberrypi ~/src/sonic $ sudo python ultrasonic_1.py

Ultrasonic Measurement

Distance : 9.1

pi@raspberrypi ~/src/sonic $ sudo python ultrasonic_1.py

Ultrasonic Measurement

Distance : 26.3 


대충 소스 받아서 수정

[링크 : http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/]

Posted by 구차니