Tuesday, 9 February 2016

Raspberry PI Ultrasonic Distance Sensor

Raspberry PI Ultrasonic Distance Sensor

 

Ultrasonic Distance Sensor is an amazing product that provides very short to long-range detection and ranging with a resolution of 1mm. The ECHO-PRO is a 5 in 1 sensor module which output the distance data as Serial ASCII, Pulse width and Analog output. The module has an onboard temperature sensor which outputs the ambient temperature via Serial ASCII. Apart from this the module is also equipped with Proximity sensing output, which will generate a high going signal if any object comes under the presented distance range (2-500cm). Since there are various interfacing options (ASCII, PWM and ANALOG) available on the module as a standard, it will be easy for a developer to select the optimum one based on the resources available on the micro controller.

Here Raspberry Pi2 is interfaced with this sensor to measure the obstacle’s distance. As the Raspberry pi could be used for high end applications, the interfacing with particular sensor will be very much useful to create an intelligent embedded system. Making of an advanced robot using Raspberry pi should meet these requirements in a proper manner, so that an intelligent robot will be developed. In this type of sensor there is an option for getting the distance in the form of serial ASCII, and we make use of this property to obtain data to the Raspberry Pi.

 NEED:

Make the connections as shown in the figure, the sensor module is powered with a power supply using 5V and the ground connection of the supply is provided to both sensor and Raspberry Pi (pin 6). The ASCII output pin is connected to the reception pin of the Raspberry Pi.  Now power on the Pi module and receive the data coming through the reception pin using Python. A sample code for this process is given below:

CODING:
import serial
import RPi.GPIO as GPIO     
import os, time
from decimal import *

# Find a character in a string
def find(str, ch):
    for i, ltr in enumerate(str):
        if ltr == ch:
            yield i

#Enable Serial communication
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.1)

while True:
    rcv = port.read(10)  # Read the data from the port

    if len(rcv)>5:
        # Data Extraction
        L1=list(find(rcv, "D"))
        L2=list(find(rcv, "T"))
        Dist=rcv[(L1[0]+1):(L2[0])]
        Dist=Decimal(Dist)
        print str(Dist) + " cm"

GPIO.cleanup()

By running this code the distance in centimeters is shown in the python shell. ASCII Serial Data output produces a TTL (Transistor Transistor Logic) output at a baud rate of 9600 including both distance and temperature sensor outputs in the format “DXXX.XTXXX” along with a ‘\r’ symbolizing the end of the string. For example “D145.6T030” where ‘D’ followed by distance in centimeters and ‘T’ followed temperature in degree Celsius. Here the user doesn’t need to calibrate the value read making it easier to interface. So as in the program, to get the distance the data in between the characters ‘D’ and ‘T’ should be extracted. 






No comments:

Post a Comment