Use Ultrasonic module on DEBIX - Part 3

2024.5.28by debix.io

Write Application Test Code

#include <stdio.h>

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/ioctl.h>

#define GET_DISTANCE _IOR('R',int)

int main(int argc,char *argv[]){

    int fd;

    unsigned long distance;

    fd = open("/dev/HC_SR04",O_RDWR);

    if(fd<0){

        printf("open err..\n");

    }

    for( ;; ){

        ioctl(fd,GET_DISTANCE,&distance);

        printf("distance: %dcm..\n",distance);

        sleep(1);

    }

    close(fd);

    return 0;

}

Compile the application file, run the command aarch64-none-linux-gnu-gcc app.c -o app, copy the compiled executable file to DEBIX.

Test result

lTest the distance between the ultrasonic module and the object under test;

lPlace the object under test parallel to the ultrasonic module;

lExecute the application test code, run the command . /app;

lAdjust the distance between the object under test and the ultrasonic module can be seen:



NOTE:

lWhen using the ultrasonic module to measure the distance, try to keep the ultrasonic module parallel to the object under test.

lThe ultrasonic module measuring distance range is 2-400 cm, beyond or less than this range measurement will not be informative.

lWhen the measurement distance exceeds 400cm, print distance: -1cm.