chchchchavez
Posts: 1
Joined: Thu Mar 05, 2015 5:07 am

3 Pin Ultrasonic Range Sensor

Tue Mar 10, 2015 11:05 pm

Hello i am trying to use an Ultrasonic Range Sensor with the latest version of raspbian.

Its a 3 pin sensor SIG, VCC , and GND

Things ive done :

1) Update Raspbian
2) Install python-Dev
3) Connected Sensor SIG to pin 11 VCC to 5v and GND to GND
4) I found this script on a forum but its not working properly and i have no clue why

Code: Select all

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
timeout = 0.020

while 1:
        GPIO.setup(11, GPIO.OUT)
        #cleanup output
        GPIO.output(11, 0)

        time.sleep(0.000002)

        #send signal
        GPIO.output(11, 1)

        time.sleep(0.000005)

        GPIO.output(11, 0)

        GPIO.setup(11, GPIO.IN)
        
        goodread=True
        watchtime=time.time()
        while GPIO.input(11)==0 and goodread:
                starttime=time.time()
                if (starttime-watchtime > timeout):
                        goodread=False

        if goodread:
                watchtime=time.time()
                while GPIO.input(11)==1 and goodread:
                        endtime=time.time()
                        if (endtime-watchtime > timeout):
                                goodread=False
        
        if goodread:
                duration=endtime-starttime
                distance=duration*34000/2
                print distance

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 3 Pin Ultrasonic Range Sensor

Wed Mar 11, 2015 7:59 am

There is more than one model of acoustic ranger.

vkibira
Posts: 2
Joined: Thu Jul 16, 2015 6:49 am

Re: 3 Pin Ultrasonic Range Sensor

Thu Jul 16, 2015 6:57 am

I got this to work today.
Only thing i did different was
1. used pin 1 (3.3v)
2. Increased the first sleep time from 0.000005 to 0.5

This is using the 3 pin range sensor from radio shack

GreatLeader_Technus
Posts: 25
Joined: Thu May 29, 2014 7:43 am

Re: 3 Pin Ultrasonic Range Sensor

Wed Nov 14, 2018 2:30 am

vkibira wrote:
Thu Jul 16, 2015 6:57 am
I got this to work today.
Only thing i did different was
1. used pin 1 (3.3v)
2. Increased the first sleep time from 0.000005 to 0.5

This is using the 3 pin range sensor from radio shack
Ah the Parallax ones! I have one too! How where you able to make yours work with your Pi?

vkibira
Posts: 2
Joined: Thu Jul 16, 2015 6:49 am

Re: 3 Pin Ultrasonic Range Sensor

Sun Apr 28, 2019 9:58 pm

You can also try to change GPIO.setmode(GPIO.BOARD) to
GPIO.setmode(GPIO.BCM)
if you are getting a ...channel set to invalid... error

mcnpower
Posts: 5
Joined: Tue Nov 01, 2016 5:41 am

Re: 3 Pin Ultrasonic Range Sensor

Mon Apr 29, 2019 5:31 am

I think you need bidirectional voltage level converter. Because your sensor has 5V logic level. RPi pins has 3.3V logic level.

Return to “Advanced users”