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

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM ABSTRACT Authentication based on passwords is used largely in applications for computer security and privacy. However, human actions such as choosing bad passwords and inputting passwords in an insecure way are regarded as”the weakest link” in the authentication chain. Rather than arbitrary alphanumeric strings, users tend to choose passwords either short or meaningful for easy memorization. With web applications and mobile apps piling up, people can access these applications anytime and anywhere with various devices. This evolution brings great convenience but also increases the probability of exposing passwords to shoulder surfing attacks. Attackers can observe directly or use external recording devices to collect users’ credentials. To overcome this problem, we proposed a novel authentication system PassMatrix, based on graphical passwords to resist shoulder surfing attacks. With a one-time valid login indicator and ...

ABSTRACT

MULTI USER GSM BASED DIGITAL SCROLLING LED DISPLAY WITH PASSWORD AND VOICE ABSTRACT The project mainly focuses on transmission of textual data through air interface by the use of GSM through asynchronous serial communication. The data will be processed by the microcontroller it programmed by embedded c language by using KEIL C compiler. The data will be displayed on led display on LED only after entering unique pass key. Existing System: How a reliable and an authentic wireless communication could be easily developed between a mobile phone and microcontroller using GSM (Global System for Mobile Communication) MODEM (Modulator-Demodulator). This technical paper explains GSM based e-notice board which can be widely used for multitude of applications including educational sector, traffic control, banks, and public advertisements, stoke exchanges etc. Proposed System: In this project we not only send the data, but send the data with pass code also. Which enables us to preve...