2.2.8 Ultrasonic Sensor Module

Introduction

The ultrasonic sensor uses ultrasonic to accurately detect objects and measure distances. It sends out ultrasonic waves and converts them into electronic signals.

Schematic Diagram

../_images/image329.png

Experimental Procedures

Step 1: Build the circuit.

../_images/image220.png

Step 2: Go to the folder of the code.

cd /home/pi/raphael-kit/c/2.2.8/

Step 3: Compile the code.

gcc 2.2.8_Ultrasonic.c -lwiringPi

Step 4: Run the executable file.

sudo ./a.out

With the code run, the ultrasonic sensor module detects the distance between the obstacle ahead and the module itself, then the distance value will be printed on the screen.

Note

If it does not work after running, or there is an error prompt: “wiringPi.h: No such file or directory”, please refer to C code is not working?.

Code

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>

#define Trig    4
#define Echo    5

void ultraInit(void)
{
    pinMode(Echo, INPUT);
    pinMode(Trig, OUTPUT);
}

float disMeasure(void)
{
    struct timeval tv1;
    struct timeval tv2;
    long time1, time2;
float dis;

    digitalWrite(Trig, LOW);
    delayMicroseconds(2);

    digitalWrite(Trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(Trig, LOW);

    while(!(digitalRead(Echo) == 1));
    gettimeofday(&tv1, NULL);

    while(!(digitalRead(Echo) == 0));
    gettimeofday(&tv2, NULL);

    time1 = tv1.tv_sec * 1000000 + tv1.tv_usec;
    time2  = tv2.tv_sec * 1000000 + tv2.tv_usec;

    dis = (float)(time2 - time1) / 1000000 * 34000 / 2;

    return dis;
}

int main(void)
{
    float dis;
    if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
        printf("setup wiringPi failed !");
        return 1;
    }

    ultraInit();

    while(1){
        dis = disMeasure();
        printf("%0.2f cm\n\n",dis);
        delay(300);
    }

    return 0;
}

Code Explanation

void ultraInit(void)
{
    pinMode(Echo, INPUT);
    pinMode(Trig, OUTPUT);
}

Initialize the ultrasonic pin; meanwhile, set Echo to input, Trig to output.

float disMeasure(void){};

This function is used to realize the function of ultrasonic sensor by calculating the return detection distance.

struct timeval tv1;
struct timeval tv2;

Struct timeval is a structure used to store the current time. The complete structure is as follows:

struct timeval
{
__time_t tv_sec;        /* Seconds. */
__suseconds_t tv_usec;  /* Microseconds. */
};

Here, tv_sec represents the seconds that Epoch spent when creating struct timeval. Tv_usec stands for microseconds or a fraction of seconds.

digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);

A 10us ultrasonic pulse is being sent out.

while(!(digitalRead(Echo) == 1));
gettimeofday(&tv1, NULL);

This empty loop is used to ensure that when the trigger signal is sent, there is no interfering echo signal and then get the current time.

while(!(digitalRead(Echo) == 0));
gettimeofday(&tv2, NULL);

This empty loop is used to ensure that the next step is not performed until the echo signal is received and then get the current time.

time1 = tv1.tv_sec * 1000000 + tv1.tv_usec;
time2  = tv2.tv_sec * 1000000 + tv2.tv_usec;

Convert the time stored by struct timeval into a full microsecond time.

dis = (float)(time2 - time1) / 1000000 * 34000 / 2;

The distance is calculated by the time interval and the speed of sound propagation. The speed of sound in the air: 34000cm/s.

Phenomenon Picture

../_images/image221.jpeg