Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

hc-sr04 ultrasonic sensor and raspberry pi

Status
Not open for further replies.
How to use hc-sr04 ultrasonic sensor with raspberry pi ? Raspberry pi is a 3.3v device while hc-sr04 is 5v....
Please help me.
I am using python programming.
 
Hello,
You can power the HC-SR04 ultrasonic sensor directly from the 5V power output of Raspberry Pi.
Connect 3.3V output of RPi directly to the TRIGGER input of HC-SR04. It can detect 3.3V as HIGH trigger.
Don't connect the 5V ECHO output directly to input pin of RPi which 3.3V tolerant... You should use voltage dividing resistors to reduce 5V down to 3.3V. You can use a 10K and a 4.7K for that.
Python Program
Code:
import RPi.GPIO as GPIO                    #Import GPIO library
import time                                #Import time library
GPIO.setmode(GPIO.BCM)                     #Set GPIO pin numbering

TRIG = 23                                  #Associate pin 23 to TRIG
ECHO = 24                                  #Associate pin 24 to ECHO

print "Distance measurement in progress"

GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in

while True:

  GPIO.output(TRIG, False)                 #Set TRIG as LOW
  print "Waitng For Sensor To Settle"
  time.sleep(2)                            #Delay of 2 seconds

  GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO)==0:               #Check whether the ECHO is LOW
    pulse_start = time.time()              #Saves the last known time of LOW pulse

  while GPIO.input(ECHO)==1:               #Check whether the ECHO is HIGH
    pulse_end = time.time()                #Saves the last known time of HIGH pulse

  pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable

  distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  distance = round(distance, 2)            #Round to two decimal points

  if distance > 2 and distance < 400:      #Check whether the distance is within range
    print "Distance:",distance - 0.5,"cm"  #Print distance with 0.5 cm calibration
  else:
    print "Out Of Range"                   #display out of range

The above program uses 23ed pin of RPi TRIGGER and 23th pin as ECHO.
Source
Interfacing HC-SR04 Ultrasonic Distance Sensor with Raspberry Pi
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top