Advanced search FAQ Register Login

It is currently Sat Jan 20, 2018 7:55 pm

 

Postby ms.CG » Fri Jul 22, 2016 6:41 am

Hello! Can anyone help me on my project.. this is about ultrasonic sensor. Im doing a automatic counter of an exercise whenever it reach the desired distance it will count as 1 and so on. The starting reading of the sensor is at 15cm and the end reading is at >=39cm is fine. If the >=39cm reached the output should count as 1 and so on until it reached the maximum count as 30. How can i make the output like a counter. Thank you for the response, it will be a great help. Godbless you all.

ms.CG
n00b

Posts: 1

Joined: Fri Jul 22, 2016 6:29 am

Postby Jake » Mon Aug 01, 2016 11:38 am

Hi,

We have a tutorial on ultrasonic range sensors below:

http://www.modmypi.com/blog/hc-sr04-ult ... spberry-pi

I think that should give you everything you need?

-- Jake

Jake
ModMyPi

User avatar

Posts: 1396

Joined: Thu Aug 27, 2015 3:57 pm

Forum Administrator & Technical Support

Postby BMS Doug » Mon Aug 01, 2016 12:11 pm

the Sample HC-SR04 code that Jake mentions is a good example for measuring the distance but will need some modification to make into a working project for the counter you mention.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

print "Distance Measurement In Progress"

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
time.sleep(2)

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO)==0:
  pulse_start = time.time()

while GPIO.input(ECHO)==1:
  pulse_end = time.time()

pulse_duration = pulse_end - pulse_start

distance = pulse_duration * 17150

distance = round(distance, 2)

print "Distance:",distance,"cm"

GPIO.cleanup()


The first thing to do is define the rangefinding as a function that you can call from the main program

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

#Define the Rangefinder Function
def FindRange():
  print "Distance Measurement In Progress"

  GPIO.setup(TRIG,GPIO.OUT)
  GPIO.setup(ECHO,GPIO.IN)

  GPIO.output(TRIG, False)
  print "Waiting For Sensor To Settle"
  time.sleep(2)

  GPIO.output(TRIG, True)
  time.sleep(0.00001)
  GPIO.output(TRIG, False)

  while GPIO.input(ECHO)==0:
    pulse_start = time.time()

  while GPIO.input(ECHO)==1:
    pulse_end = time.time()

  pulse_duration = pulse_end - pulse_start

  distance = pulse_duration * 17150

  distance = round(distance, 2)


  return distance
  # The return command sends the distance found by the rangefinder back to the main program

#Main program below
FindRange()
print "Distance:",distance,"cm"

GPIO.cleanup()

BMS Doug
31337

Posts: 273

Joined: Tue Sep 08, 2015 3:55 pm

Location: London

Doug.

Volunteer Moderator.

Postby BMS Doug » Mon Aug 01, 2016 12:22 pm

Next you need to add your count reference and a loop, you also need to add some control logic into the loop to determine what happens.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24
Count = 0   # Have to start somewhere

#Define the Rangefinder Function
def FindRange():
  print "Distance Measurement In Progress"

  GPIO.setup(TRIG,GPIO.OUT)
  GPIO.setup(ECHO,GPIO.IN)

  GPIO.output(TRIG, False)
  print "Waiting For Sensor To Settle"
  time.sleep(2)

  GPIO.output(TRIG, True)
  time.sleep(0.00001)
  GPIO.output(TRIG, False)

  while GPIO.input(ECHO)==0:
    pulse_start = time.time()

  while GPIO.input(ECHO)==1:
    pulse_end = time.time()

  pulse_duration = pulse_end - pulse_start

  distance = pulse_duration * 17150

  distance = round(distance, 2)


  return distance
  # The return command sends the distance found by the rangefinder back to the main program

#Main program below
while true:
  # do something or time delay
  if FindRange() < 39:
    # continue program
  Else:
    Count += 1
    # do something else

  # print "Count:",Count

GPIO.cleanup()


Please note that this code is untested, It may contain errors.

BMS Doug
31337

Posts: 273

Joined: Tue Sep 08, 2015 3:55 pm

Location: London

Doug.

Volunteer Moderator.

Who is online

Users browsing this forum: No registered users and 1 guest

Board index

The teamDelete all board cookies • All times are UTC [ DST ]