Thursday, October 26, 2017

Using HC-SR04 Ultrasonic Range Sensor with Raspberry Pi 2

The HC-SR04 ultrasonic range finder is a sensor that provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit. It is very cheap and simple to use, however the signal output needs to be converted from 5V to 3.3V so as not to damage the Raspberry Pi. I will introduce some Physics along with Electronics in this tutorial in order to explain each step. But first, we should have the following components:

1 - Raspberry Pi 2 Model B
2 - HC-SR04 Ultrasonic Range Sensor
3 - 1x1kΩ and 1x2.2kΩ Resistors
4 - Bread Board
5 - Jumper Male to Female Wires

Ultrasonic Range Sensors and How do they works?

There are two types of ultrasonic range sensors: active and passive. An active sensors set sends out sound pulses called pings, then receives the returning sound echo. Passive sensors sets receive sound echoes without transmitting their own sound signals.

How HC-SR04 ultrasonic range sensor works? It is an active sensor and basically, it creates a pulse of sound, often called a "ping", and then listens for reflections (echo) of the pulse. If you check the following images. You will understand the concept better.



The Ultrasonic Range Sensor sends the sound waves(pings) and these sound waves hit the object. After that the waves are reflected by the object and these reflected waves(echoes) are detected by the sensor.


Distance Calculation

The distance between the sensor and the object is calculated by using the following formula:

$$distance = \dfrac {c · t}{2}$$

where c is the speed of sound = 343 m/s, t is measured running time in seconds. We divide by 2 because the sound waves are travelling two times actual the distance(from sensor to the object, from object to the sensor).

Connecting the Wires and Voltage Divider

Now, lets connect the wires between the sensor and the Raspberry Pi 2. You can see the following diagram that shows the pin connections. This is the trickiest step of this construction, but it isn’t too difficult.


We can’t simply attach the sensor’s ECHO wire to the Pi’s GPIO pin, as the sensor module would be outputting a 5V signal to a pin rated only for 3.3V, and this could result in damage to the Pi.

With resistors, we can create a voltage divider to bring the voltage down to an acceptable level. A voltage divider consists of two resistors(R1 and R2) in series connected to an input voltage(Vin), which needs to be reduced to our output voltage(Vout). In our circuit, Vin will be ECHO, which needs to be decreased from 5V to our Vout of 3.3V.


For our purposes, we will use a 1kΩ resistor for R1 and a 2.2kΩ resistor for R2. Actually building the voltage divider:

1 - Plug your ECHO wire into a blank rail
2 - Use your R1 resistor (1kΩ) to link that rail to another blank rail.
3 - Link this rail to the breadboard’s Ground with your R2 resistor (2.2kΩ), leaving at least one space between your R1 and R2 elements.
4 - In the blank space between resistors, plug in the Pi’s GPIO26(PIN 37) wire, linking ECHO to your Pi.


Sensing with Python

Now that we have hooked the Ultrasonic Sensor up to our Pi, we need to program a Python script to detect distance. The whole code is below. I also commented for you to understand it.

# External module imports
import RPi.GPIO as GPIO;
import time;

# pin definitons
ECHO = 36
TRIGGER = 38
# pulse time interval
PULSE = 0.00001
# speed of sound
SPEED_OF_SOUND = 34029
 
# close warnings
GPIO.setwarnings(False);
# setup configuration
# name all of the pins, so set the naming mode by writing
GPIO.setmode(GPIO.BOARD);
# set input/output pins
GPIO.setup(TRIGGER, GPIO.OUT);
GPIO.setup(ECHO, GPIO.IN);
GPIO.output(TRIGGER, False);

def pulse():
    # pulse the sound wave 0.00001 seconds
    GPIO.output(TRIGGER, True);
    time.sleep(PULSE);
    # stop pulsing the wave
    GPIO.output(TRIGGER, False);
    # set the starting time(pulse)
    starttime = time.time();
    stop = starttime;
    start = starttime;
    # set the stopping time(echo)
    while GPIO.input(ECHO) == 0 and start < starttime + 2:
        start = time.time();
    while GPIO.input(ECHO) == 1 and stop < starttime + 2:
        stop = time.time();
    # take the difference
    delta = stop - start;
    # measure the distance
    distance = delta * SPEED_OF_SOUND;
    # divide by 2, because, the sound wave travels two times the distance
    # from sensor to the object, from object to the sensor
    return distance / 2.0;
 
try:

    while True:
        # display the distance measured
        print("Measured: %1.1f" % pulse());
        # stop 0.25 seconds
        time.sleep(0.25);
  
except KeyboardInterrupt:
    pass;

# clean the all pins
GPIO.cleanup();

I explained the every steps. If you have any questions, feel free to shoot. See you soon in the next post.

No comments:

Post a Comment