Skip to main content

ULTRASONIC DISTANCE SENSOR ON RASPBERRY PI

ULTRASONIC DISTANCE SENSOR

In air, sound travels at a speed of 343 metres per second. An ultrasonic distance sensor sends out pulses of ultrasound which are inaudible to humans, and detects the echo that is sent back when the sound bounces off a nearby object. It then uses the speed of sound to calculate the distance from the object.
Ultrasonic distance sensor

WIRING

The circuit connects to two GPIO pins (one for echo, one for trigger), the ground pin, and a 5V pin. You'll need to use a pair of resistors (330Ω and 470Ω) as a potential divider:
wiring

CODE

To use the ultrasonic distance sensor in Python, you need to know which GPIO pins the echo and trigger are connected to.
  1. Open Python 3.
  2. In the shell, enter the following line to import DistanceSensor from the GPIO Zero library:
    from gpiozero import DistanceSensor
    After each line, press Enter and the command will be executed immediately.
  3. Create an instance of DistanceSensor using your echo and trigger pins:
    ultrasonic = DistanceSensor(echo=17, trigger=4)
  4. See what distance it shows:
    ultrasonic.distance
    You should see a number: this is the distance to the nearest object, in metres.
  5. Try using a loop to print the distance continuously, while waving your hand in front of the sensor to alter the distance reading:
    while True:
        print(ultrasonic.distance)
    The value should get smaller the closer your hand is to the sensor. Press Ctrl + C to exit the loop.

RANGES

As well as being able to see the distance value, you can also get the sensor to do things when the object is in or out of a certain range.
  1. Use a loop to print different messages when the sensor is in range or out of range:
    while True:
        ultrasonic.wait_for_in_range()
        print("In range")
        ultrasonic.wait_for_out_of_range()
        print("Out of range")
    Now wave your hand in front of the sensor; it should switch between showing the message "In range" and "Out of range" as your hand gets closer and further away from the sensor. See if you can work out the point at which it changes.
  2. The default range threshold is 0.3m. This can be configured when the sensor is initiated:
    ultrasonic = DistanceSensor(echo=17, trigger=4, threshold_distance=0.5)
    Alternatively, this can be changed after the sensor is created, by setting the threshold_distanceproperty:
    ultrasonic.threshold_distance = 0.5
  3. Try the previous loop again and observe the new range threshold.
  4. The wait_for functions are blocking, which means they halt the program until they are triggered. Another way of doing something when the sensor goes in and out of range is to use whenproperties, which can be used to trigger actions in the background while other things are happening in the code.
    First, you need to create a function for what you want to happen when the sensor is in range:
    def hello():
        print("Hello")
    Then set ultrasonic.when_in_range to the name of this function:
    ultrasonic.when_in_range = hello
  5. Add another function for when the sensor goes out of range:
    def bye():
        print("Bye")
    
    ultrasonic.when_out_of_range = bye
    Now these triggers are set up, you'll see "hello" printed when your hand is in range, and "bye" when it's out of range.
  6. You may have noticed that the sensor distance stopped at 1 metre. This is the default maximum and can also be configured on setup:
    ultrasonic = DistanceSensor(echo=17, trigger=4, max_distance=2)
    Or after setup:
    ultrasonic.max_distance = 2
  7. Try different values of max_distance and threshold_distance.

Comments

Popular posts from this blog

Jio

Reliance Jio planning its own  cryptocurrency called JioCoin  elder son Akash Ambani leading the JioCoin project, Reliance Jio plans to build a 50-member team of young professionals to work on blockchain technology, which can also be used to develop applications such as smart contracts and supply chain management logistics

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...