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

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search ABSTRACT: In this paper, With advances in geo-positioning technologies and geo-location services, there are a rapidly growing amount of spatiotextual objects collected in many applications such as location based services and social networks, in which an object is described by its spatial location and a set of keywords (terms). Consequently, the study of spatial keyword search which explores both location and textual description of the objects has attracted great attention from the commercial organizations and research communities. In the paper, we study two fundamental problems in the spatial keyword queries: top k spatial keyword search (TOPK-SK), and batch top k spatial keyword search (BTOPK-SK). Given a set of spatio-textual objects, a query location and a set of query keywords, the TOPK-SK retrieves the closest k objects each of which contains all keywords in the query. BTOPK-SK is the batch processing of sets...

A simple and reliable touch sensitive security system CODING

#include <REGX51.H> #include "lcd.c" #define MAX_DELAY() delay(65000) sbit Vibra_Sense=P3^1; sbit Buz=P1^0; void intro() {  lcd_init();  lcd_str("Touch Sensitive ",0x80);  lcd_str("Security System ",0xc0);  MAX_DELAY();MAX_DELAY();  lcd_clr();  }  void main()  { unsigned int i = 0, j= 0; intro();    while(1)    { lcd_str("Security Syst On",0x80); lcd_str("No Vibra Detectd",0xc0); Buz = 1; if(Vibra_Sense == 1) { while(Vibra_Sense == 1) delay(1000); } else { while(Vibra_Sense == 0) delay(1000); } Buz = 0; lcd_str("Vibraton Detectd",0xc0);delay(65000); while(1);    }  }

A Time Efficient Approach for Detecting Errors in Big Sensor Data on Cloud

A Time Efficient Approach for Detecting Errors in Big Sensor Data on Cloud Abstract                                                                                                                                                      ...